Description
Description
With NEP-18, numpy functions that previously converted an array-like to an ndarray may no longer do the (implicit) conversion. dask.array recently implemented __array_function__ so np.unique(dask.array.Array) now returns a dask.array.Array.
Some more details in dask/dask-ml#541
Steps/Code to Reproduce
import dask.array as da
import dask_ml.datasets
import sklearn.linear_model
X, y = dask_ml.datasets.make_classification(chunks=50)
clf = sklearn.linear_model.LogisticRegression()
clf.fit(X, y)Expected Results
No error, the same output as clf.fit(X.compute(), y.compute()),
or by setting the environment variable NUMPY_EXPERIMENTAL_ARRAY_FUNCTION='0'.
Actual Results
That raises
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-b0953fbb1d6e> in <module>
----> 1 clf.fit(X, y)
~/Envs/dask-dev/lib/python3.7/site-packages/sklearn/linear_model/logistic.py in fit(self, X, y, sample_weight)
1536
1537 multi_class = _check_multi_class(self.multi_class, solver,
-> 1538 len(self.classes_))
1539
1540 if solver == 'liblinear':
TypeError: 'float' object cannot be interpreted as an integerThis is because self.classes_ = np.unique(y) is a Dask Array with unknown length
In [2]: np.unique(da.arange(12))
Out[2]: dask.array<getitem, shape=(nan,), dtype=int64, chunksize=(nan,), chunktype=numpy.ndarray>since Dask is lazy and doesn't know the unique elements until compute time.
Versions
System:
python: 3.7.3 (default, Apr 5 2019, 14:56:38) [Clang 10.0.1 (clang-1001.0.46.3)]
executable: /Users/taugspurger/Envs/dask-dev/bin/python
machine: Darwin-18.6.0-x86_64-i386-64bit
Python deps:
pip: 19.2.1
setuptools: 41.0.1
sklearn: 0.21.3
numpy: 1.18.0.dev0+5e7e74b
scipy: 1.2.0
Cython: 0.29.9
pandas: 0.25.0+169.g5de4e55d6
I think this needs need NumPy>=1.17 and Dask>=2.0.0
Possible solution: Explicitly convert array-likes to concrete ndarrays where necessary (this is a bit hard to determine though). For example
scikit-learn/sklearn/linear_model/logistic.py
Line 1517 in 1484918
| self.classes_ = np.unique(y) |
self.classes_ = np.asarray(np.unique(y)). That may not be ideal for other libraries implementing __array_function__ (like pydata/sparse).
Activity
jnothman commentedon Aug 20, 2019
amueller commentedon Aug 20, 2019
why isn't
check_arraydealing with this? hrm... I guess it's mostly an issue ony.adrinjalali commentedon Aug 20, 2019
check_arrayshouldn't deal with it if we want to keep a dask array a dask array as long as we can. So it shouldn't convert it (#11447)I guess if we want to go down the road of supporting objects implementing
__array_function__, we kinda should delay converting to a numpy arrya as long as we can, which would also help us better supporting pandas dataframes.We probably would need to do the
np.asarray(...)once we need the actual values? Or maybe rely onlen(...)instead ofx.shape, would that work @TomAugspurger ?amueller commentedon Aug 20, 2019
I would argue we shouldn't delay conversion for now because it will get us into hell. Definitely not until we can test for this.
To test for this, we need to have something that implements
__array_function__for all possible functions and then test that things are handled correctly.Maybe I'd create a class that raises an error if any
__array_function__is called at all? That should allow us to test conservatively.amueller commentedon Aug 20, 2019
For this concrete example: what does
lenreturn? and what should it return? what operations trigger the computation for dask?TomAugspurger commentedon Aug 20, 2019
len(dask.array.Array)will hit the same issue. We're returningnan, which python will convert to a TypeError, since len is supposed to return an int.Just to be clear, I'm not sure this is a case that scikit-learn should support. At least not for arbitrary estimators. For specific estimators like ColumnTransformer handling a Dask Array without unnecessarily converting to a NumPy array makes sense.
amueller commentedon Aug 20, 2019
If
StandardScalercan be lazy or on GPU I wouldn't mind, but I think making sure that nothing breaks should be the first priority.And I feel like
lenreturningNaNseems to imply that dask doesn't actually implement the protocol fully, right?There's two options basically: convert early or convert late, and as I think I mentioned in other issues, it could become very opaque to the user which one is happening.
In this case, it seems it tries to convert late but it fails. But I would argue it fails because dask is misbehaving and not returning the thing the numpy api asks for.
TomAugspurger commentedon Aug 20, 2019
Do you mean the
__array_function__protocol, or Python's data model? Most of the time Dask arrays have a known shape, solenwill return an int. But when you do an operation where the size of the output depends on the values of the data (likenp.unique) the length is unknown.amueller commentedon Aug 20, 2019
I guess my thought was the
__array_function__protocol but I guess you're not even implementing Python's data model ;)Is there a specification of what a duck array should support? @shoyer might have thoughts?
I guess
lenis not an__array_function__.np.shapeis, though. What doesnp.shapereturn?amueller commentedon Aug 20, 2019
ok looks like
np.shapealso returnsnp.nan. Does that satisfy what implementing the__array_function__protocol requires?shoyer commentedon Aug 20, 2019
NumPy itself doesn't really impose any restrictions on what you can do with
__array_function__. I think it would be perfectly reasonable to error when length is NaN.I would definitely coerce everything to NumPy arrays in
check_array. As a first pass that's definitely the right thing to do. I'm a little surprised that wasn't happening already. Duck array support is something you want to add intentionally, not accidentally.amueller commentedon Aug 20, 2019
@shoyer well they were coerced, but apparently sometimes using numpy fuctions. So this will only be an issue on the next numpy release.
shoyer commentedon Aug 20, 2019
The next NumPy release (1.17) is already here!
__array_function__is now enabled by default.Unfortunately, this is indeed a breaking a change, because
__array_function__means that dask can now implements overrides of existing NumPy functions likenp.uniquewhich previously always did coercion.amueller commentedon Aug 20, 2019
oh but it's not available on conda? Or I'm doing something weird?
shoyer commentedon Aug 20, 2019
You can find it in the conda-forge channel.
amueller commentedon Aug 21, 2019
#14702 should do it
cmarmo commentedon Apr 14, 2022
@TomAugspurger , @thomasjpfan I'm closing this one as per dask-ml#541 comment.
The original snippet of code works with the current version of scikit-learn (once dask/dask-ml#910 is applied).
TomAugspurger commentedon Apr 14, 2022
Thanks! I'll try to take a look at dask/dask-ml#910 soon.