Repository navigation
MNT Add estimator check for not calling __array_function__ - #14702
Conversation
|
this is not a complete fix and is also breaking some things. |
|
hm this is actually nearly ok but I gotta run ;) |
|
now added a test for |
|
Should be good now? |
|
oh coverage fails because we don't test on new numpy I guess. Should we? I think waiting till it's on conda might be ok? |
| def __array__(self, dtype=None): | ||
| return self.data | ||
|
|
||
| def __array_function__(self, func, types, args, kwargs): |
There was a problem hiding this comment.
Returning True and raising TypeError needs to be tested?
There was a problem hiding this comment.
you mean explicitly tested? Sure, i could do that. Thought that was a bit overkill for a test helper. They are obviously used in the tests, but on CI there's no __array_function__ protocol.
There was a problem hiding this comment.
I would be more onboard if NotAnArray was private, which goes back to "public vs private utils" #6616 (comment)
On the other hand, we are depending on this raising when something is wrong. When everything is working, our tests do not run __array_function__.
There was a problem hiding this comment.
That's a good point. I'll add a test (that won't be run)
I'm not sure how many people install numpy from pypi, but my guess is "many". I'd rather have that, or conda-forge as the "test against the latest" in the CI, rather than conda. WDYT? |
| self.sparse_output_ = sp.issparse(y) | ||
|
|
||
| if not self.sparse_output_: | ||
| y = np.asarray(y) |
There was a problem hiding this comment.
do we not want to have a more complex atleast_1d instead?
There was a problem hiding this comment.
you mean column_or_1d? I honestly don't know why we would need atleast_1d. We usually want scalars to be treated differently.
There was a problem hiding this comment.
there's also check_array(ensure_2d=False) which might be more suitable here?
There was a problem hiding this comment.
yep, I tend to forget that we have both column_or_1d and atleast_1d. Ideally I think I'd rather have ensure_ndims=n in check_array maybe.
| self.n_outputs_ = y.shape[1] | ||
|
|
||
| check_consistent_length(X, y, sample_weight) | ||
| if sample_weight is not None: |
There was a problem hiding this comment.
I feel like this belongs to check_array too, doesn't it?
There was a problem hiding this comment.
not sure what you mean. Using check_array(sample_weights, ensure_2d=True) instead of asarray? We don't have any tests for NaN in sample weights, do we? I'm not sure how much I want to make this PR about adding way more checks to sample weights
There was a problem hiding this comment.
we have _check_sample_weight in validation
There was a problem hiding this comment.
that does a lot more though, right?
|
The test failure is due to me using I'm ok with moving the "latest" CI to conda-forge |
# Conflicts: # sklearn/model_selection/tests/test_search.py
|
Never mind, #14381 doesn't fix that, I just won't use pytest in that file. |
|
any takers? Should I include the change to CI in this PR? |
|
I was gonna do the CI change, if you don't, I'll do in a separate PR. |
bc9df43 to
e0f239e
Compare
|
this is quite hard to keep in sync with master :-/ |
|
greeeeen |
ogrisel
left a comment
There was a problem hiding this comment.
LGTM.
We need a changelog entry (and maybe a section in the doc) to explain that sklearn estimators will not rely on __array_function__ by default and instead materialize fit parameters as numpy arrays internally.
|
@ogrisel like this? |
ogrisel
left a comment
There was a problem hiding this comment.
Maybe mention NEP 18 explicitly to improve googl-ability.
|
|
||
| - Estimators are expected to convert input data (``X``, ``y``, | ||
| ``sample_weights``) to numpy ``ndarray`` and never call | ||
| ``__array_function__`` on the original datatype that is passed. |
There was a problem hiding this comment.
| ``__array_function__`` on the original datatype that is passed. | |
| ``__array_function__`` on the original datatype that is passed. This means that, | |
| by default, estimators are not expected to support the array function dispatching | |
| mechanism of `NEP 18`_. |
There was a problem hiding this comment.
I don't think this wording is particularly clear. Estimators are forbidden from supporting the mechanism of NEP 18 with this PR.
There was a problem hiding this comment.
I did not want to phrase it in a way that this will always be the case in the future. But phrase it as you wish as long as NEP 18 is mentioned :)
Co-Authored-By: Olivier Grisel <[email protected]>
Co-Authored-By: Olivier Grisel <[email protected]>
| :pr:`13875` by `Trevor Stephens`_. | ||
|
|
||
| - Estimators are expected to convert input data (``X``, ``y``, | ||
| ``sample_weights``) to numpy ``ndarray`` and never call |
There was a problem hiding this comment.
why not use :class:`numpy.ndarray` to get the cross-reference?
| Miscellaneous | ||
| ............. | ||
|
|
||
| - |API| Scikit-learn currently converts any input data structure implementing a duck array |
There was a problem hiding this comment.
Do you mean "now" instead of "currently"?
|
The only test is that estimators don't complain when you pass I think we should have @amueller I addressed Joel's comments and merged with master. I'm happy to address future comments if you need Regarding all the changes to sample_weight, I agree with @glemaitre that we could/should be using |
|
I think we should have _NotAnArray.__array_function__ raise an exception
to properly cover that?
Isn't that what this pr does?
|
|
Ah, indeed. I wasn't reading that well for some reason |
test for #14687.
I don't think we test against numpy 1.17 so this will only fail on cron not here?