Skip to content

Some scikit-learn estimators no longer work with array_function enabled #541

Open

Description

@TomAugspurger

There's nothing dask-ml can do I think. Just filing this here till I can figure out the right thing to do.

In [1]: import dask.array as da

In [2]: import dask_ml.datasets

In [3]: X, y = dask_ml.datasets.make_classification(chunks=50)

In [4]: import sklearn.linear_model

In [5]: clf = sklearn.linear_model.LogisticRegression()

In [6]: clf.fit(X, y)
---------------------------------------------------------------------------
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 integer

Previously, that would have been converted to an ndarray by scikit-learn and fitted.

Activity

  1. TomAugspurger commented on Aug 19, 2019

    @TomAugspurger
    MemberAuthor

    For the record, the error is from the fact that np.unique(da.Array) no longer converts to an ndarray

    With array function disabled

    In [1]: import numpy as np; import dask.array as da
    
    In [2]: np.unique(da.arange(12))
    Out[2]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

    With array function enabled

    In [1]: import numpy as np; import dask.array as da
    
    In [2]: np.unique(da.arange(12))
    Out[2]: dask.array<getitem, shape=(nan,), dtype=int64, chunksize=(nan,), chunktype=numpy.ndarray>

    The length of that array is unknown, leading to the nan shape, which breaks _check_multi_class.


    The simplest fix, from dask-ml's point of view, is for scikit-learn to convert that array-like to a concrete ndarray. But is that appropriate for all arrays? Is it appropriate for all estimators? Some estimators, like RandomForestClassfiers accept 2d output, which may be sparse. But the unique set should presumably be small.

  2. thomasjpfan commented on Feb 21, 2020

    @thomasjpfan
    Contributor

    The code snippet in original issue now works on sklearn 0.22 because of scikit-learn/scikit-learn#14702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions