-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG+1] Add check_is_fitted to non standard functions #12279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks @whiletruelearn , can you add a test to make sure that the correct exception is raised, like the code snippets in #12276? |
You can make sure the tests pass locally before pushing commits:
|
|
@NicolasHug thanks . Had some trouble in setting up a proper dev environment. I think i have found the issue and fixed it with the last commit. |
sklearn/neighbors/base.py
Outdated
@@ -664,7 +665,7 @@ class from an array representing our data set and ask who's | |||
For efficiency, `radius_neighbors` returns arrays of objects, where | |||
each object is a 1D array of indices or distances. | |||
""" | |||
check_is_fitted(self, "_fit_method") | |||
check_is_fitted(self, "_fit_X") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
check_is_fitted(self, "_fit_method", "_fit_X")
still work? I think it would be best as it still ensure the previous test is covered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works. Initially used _fit_method
which was failing.
My understanding is that _fit_X
is the attribute that needs to be checked for this functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave check_is_fitted(self, "_fit_method", "_fit_X")
then, it's safer and might cover some bugs that aren't caught in the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay @NicolasHug . Will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NicolasHug I Tried adding this and tested locally. The build fails when i do so.
The current implementation looks correct to me .
Because in check_is_fitted
scikit-learn/sklearn/utils/validation.py
Line 944 in e6359e2
if not all_or_any([hasattr(estimator, attr) for attr in attributes]): |
, exception is raised even if one of the attribute is not present. Please correct me if i am wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use all_or_any=any
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also made a slight change check_is_fitted(self, ["_fit_method", "_fit_X"], all_or_any=any)
@jnothman @NicolasHug Please review and let me know if i need to change something else. |
Slight ping @jnothman @NicolasHug |
Please add a small |
Thanks. I have updated @jnothman |
@jnothman @NicolasHug Kindly let me know if there are anymore changes to make. Thanks |
doc/whats_new/v0.21.rst
Outdated
:func: `NearestNeighbors.kneighbors`, | ||
:func: `NearestNeighbors.radius_neighbors`, | ||
:func: `NearestNeighbors.kneighbors_graph`, | ||
:func: `NearestNeighbors.radius_neighbors_graph` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest building the docs locally and checking the formatting is correct. Here you need to remove spaces after :func:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NicolasHug Sorry , I have corrected this. I was hoping the build would capture issues if any. I did try make html
, but it seems to be taking lots of time. What would be the right way to test a small change like this ?
doc/whats_new/v0.21.rst
Outdated
:func:`NearestNeighbors.kneighbors_graph`, | ||
:func:`NearestNeighbors.radius_neighbors_graph` | ||
without first fitting should raise `NotFittedError`. | ||
:issue:`12279` by :user:`Krishna Sangeeth <whiletruelearn>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting is still a bit messy. You can copy / paste this, it will do:
- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions
:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`,
:func:`radius_neighbors <neighbors.NearestNeighbors.radius_neighbors>`,
:func:`kneighbors_graph <neighbors.NearestNeighbors.kneighbors_graph>`, and
:func:`radius_neighbors_graph
<neighbors.NearestNeighbors.radius_neighbors_graph>` without first fitting
should raise ``NotFittedError``. :issue:`12279` by :user:`Krishna Sangeeth
<whiletruelearn>`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @NicolasHug . I have updated.
Thanks @whiletruelearn , LGTM! |
doc/whats_new/v0.21.rst
Outdated
...................... | ||
|
||
- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions | ||
:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`
is equivalent to
:func:`~neighbors.NearestNeighbors.kneighbors`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
doc/whats_new/v0.21.rst
Outdated
:mod:`sklearn.neighbors` | ||
...................... | ||
|
||
- |Fix| Fixed a bug in :class:`neighbors.NearestNeighbors` calling functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a big bug. Let's phrase it as "Methods x x x now raise NotFittedError, rather than AttributeError, when called before fit". Can label it |API|
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for correcting. I have updated @jnothman . Apologies for the delay.
Appveyor build seems to be failing for some reason. Should i create another issue for this.
|
Sorry for the failing test. See #12382 |
@jnothman Slight ping. Anything else to look into :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wait for another review, please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as well, merging. Thank you very much @whiletruelearn!
…-learn#12279)" This reverts commit 48ceed0.
…-learn#12279)" This reverts commit 48ceed0.
Reference Issues/PRs
Fixes #12276
What does this implement/fix? Explain your changes.
Adding
check_is_fitted
method to other non standard functionsAny other comments?