[MRG+1] Add check_is_fitted to non standard functions#12279
[MRG+1] Add check_is_fitted to non standard functions#12279ogrisel merged 15 commits intoscikit-learn:masterfrom whiletruelearn:fix_12276
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
| 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.
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.
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.
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.
@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
, 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.
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.
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.
@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.
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>`.
|
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.
:func:`kneighbors <neighbors.NearestNeighbors.kneighbors>`
is equivalent to
:func:`~neighbors.NearestNeighbors.kneighbors`
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.
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.
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 :-) |
jnothman
left a comment
There was a problem hiding this comment.
Just wait for another review, please
ogrisel
left a comment
There was a problem hiding this comment.
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_fittedmethod to other non standard functionsAny other comments?