-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
FIX Add common test to check for unfitted behaviour in classifiers and fix RadiusNeighborsClassifier and ClassifierChain correspondingly #27051
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
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!
We might want to change the title of this PR for including FIX
, I think. How about:
FIX Check that `RadiusNeighborsClassifier` is fit in `predict{,_proba}`
@glemaitre: I am ignorant here, but aren there common tests checking that estimators are fit before predict
and predict_proba
are called? 🙂
"""Check that :class:`RadiusNeighborsClassifier` is fitted before | ||
calling the :meth:`predict` and :meth:`predict_proba`. |
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.
check_is_fitted
also must be called in RadiusNeighborsClassifier.predict
(for consistency with RadiusNeighborsRegressor.predict
) and test this change accordingly.
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 don't think we need to do that here because the first thing that predict calls is predict_proba.
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 think that we need at least to test predict
on unfit estimator.
RadiusNeighborsClassifier
is fit in predict{,_proba}
Indeed we have common tests that check that we raise a |
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. Thank you, @OmarManzoor.
"""Check that :class:`RadiusNeighborsClassifier` is fitted before | ||
calling the :meth:`predict` and :meth:`predict_proba`. |
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 think that we need at least to test predict
on unfit estimator.
@jjerphan I wonder if we should remove the test considering the common test and maybe add this classifier there if it is missing. |
If this is doable, it's preferable, yes. |
* Add a common test to check for unfitted classifiers * Fix ClassifierChain by adding check_is_fitted inside predict_proba and decision_function methods
@jjerphan Could you kindly have a look? |
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.
I would wait other maintainers with more authority on common tests to complement my comment and to approve this PR.
@pytest.mark.parametrize( | ||
"classifier", | ||
_tested_estimators(type_filter="classifier"), | ||
ids=_get_check_estimator_ids, | ||
) | ||
def test_classifiers_unfitted(classifier): | ||
classifier_name = classifier.__class__.__name__ | ||
_set_checking_parameters(classifier) | ||
check_estimators_unfitted(classifier_name, classifier) |
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 am surprised that a test like this one is not already existing.
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.
Changes made to this file enlarge the scope of this PR.
Could you change the title of this PR accordingly?
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.
Done! Feel free to modify if it looks too long.
RadiusNeighborsClassifier
is fit in predict{,_proba}
This is already the job of the What should be checked instead is the reason why we would skip this test for the model here. |
OK so the reason here is that |
What is the reason that RadiusNeighborsClassifier did not get picked up in this test if it applicable? The main purpose of this PR initially was to check for is fitted in RadiusNeighborsClassifier predict_proba so that it means this was not being checked by the test you mentioned. |
Thanks @glemaitre! I explored a bit and found that the check scikit-learn/sklearn/neighbors/_base.py Lines 1140 to 1145 in ae01cec
@jjerphan I don't think we need a dedicated PR for this as all we are doing in #26828 is shifting this check a bit earlier as required. |
OK, feel free to close this PR. |
Thanks @OmarManzoor for investigating. |
Reference Issues/PRs
Relates to #26828
What does this implement/fix? Explain your changes.
Any other comments?
CC: @jjerphan