-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
TST Add sample order invariance to estimator_checks #17598
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 @ngojason9, this is very useful! For the failing estimaotr,
- we can skip RadiusNeighborsTransformer as it returns a (n_samples, n_samples) matrix and so won't verify this property. You can do that with
_xfail_checks
estimator tag: https://scikit-learn.org/stable/developers/develop.html#estimator-tags - For stochastic models including BernoulliRBM, maybe defaults, the dataset or tolerances would need to be adjusted.
Thanks @rth for the suggestion. I added |
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 @ngojason9 ! A few last comments otherwise LGTM.
sklearn/utils/estimator_checks.py
Outdated
if hasattr(estimator, "n_components"): | ||
estimator.n_components = 1 | ||
if hasattr(estimator, "n_clusters"): | ||
estimator.n_clusters = 1 |
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 it make sense to have 1 clusters for this test though? The result is always going to be the same cluster, component so we won't be testing much?
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.
umm I'm not entirely sure what n_clusters
should be instead. Are you suggesting that if the estimator has 2 clusters, then we should test each cluster individually? Apologies if I totally misunderstood your suggestion.
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.
No mean that with n_clusters=1
, for instance KMeans.predict
whether there is sample invariance or not will predict that one cluster, since there is no alternative values. Could we try changing this to 2 and see if tests still pass?
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.
@rth I tried changing to 2 and the tests still pass. I also tried deleting that block of code altogether and the tests also pass. How would you like me to proceed?
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.
Please set it to 2.
Co-authored-by: Roman Yurchak <rth.yurchak@gmail.com>
Co-authored-by: Roman Yurchak <rth.yurchak@gmail.com>
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 looks good. Could you add an entry in whats new since it will be run by the check_estimator
for third-party libraries?
@rth could you have a final look at this one. |
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.
Setting estimator.n_clusters = 2
would be preferable I think otherwise LGTM
sklearn/utils/estimator_checks.py
Outdated
if hasattr(estimator, "n_components"): | ||
estimator.n_components = 1 | ||
if hasattr(estimator, "n_clusters"): | ||
estimator.n_clusters = 1 |
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.
Please set it to 2.
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 assume we have decided that we can break check_estimator backwards compatibility since new checks can be disabled by downstream libraries?
Hi @ngojason9 ! It looks like the CI is failing but this PR is otherwise okay. @rth, is that correct? If so, Jason, please make the necessary fixes to ensure CI passes. |
That and we should ideally merge some version of #17361 before the next release.
Merging master in might be enough. Once we fix #17943 that is. |
@ngojason9 Could you please merge upstream/master to resolve CI issues? |
@ngojason9 we finally introduce #17361 in master. So if you could merge master into your branch and make the above change, then we will be able to merge this PR. |
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
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 @ngojason9, three approvals already! Once the lint fixed this PR is ready for merging!
@@ -1168,6 +1169,41 @@ def check_methods_subset_invariance(name, estimator_orig, strict_mode=True): | |||
atol=1e-7, err_msg=msg) | |||
|
|||
|
|||
@ignore_warnings(category=FutureWarning) | |||
def check_methods_sample_order_invariance(name, estimator_orig, strict_mode=True): |
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.
def check_methods_sample_order_invariance(name, estimator_orig, strict_mode=True): | |
def check_methods_sample_order_invariance(name, estimator_orig, | |
strict_mode=True): |
I fixed the linting issue in #18570 because I could not push directly in this branch. Thanks @ngojason9 for the work. |
Reference Issues/PRs
Fixes #8695
What does this implement/fix? Explain your changes.
Added
check_methods_sample_order_variance
function, which checks for method invariance under sample order (i.e. results should not change when sample before or after applying the method).For example, if we randomize the indices, the results should still be the same:
Any other comments?
The original issue mentions using
assert_array_equal
to check for invariance. While this method works forpredict
,decision_function
,score_samples
, andpredict_proba
, it fails fortransform
. Therefore, I have opted forassert_allclose_dense_sparse
withatol=1e-9
instead ofassert_array_equal
, which passes the tests for all methods.Furthermore, the original issue uses random sampling, but since we already have
check_methods_subset_invariance
already, I just shuffled the indices instead of random sampling.