Repository navigation
FIX metadata routing in learning_curve and related module tests - #34039
Conversation
| with pytest.raises(AssertionError, match=err_msg): | ||
| learning_curve( | ||
| estimator, | ||
| X, | ||
| y, | ||
| cv=3, | ||
| exploit_incremental_learning=True, | ||
| train_sizes=np.linspace(0.1, 1.0, 10), | ||
| error_score="raise", | ||
| params={"sample_weight": np.ones(3)}, | ||
| ) | ||
|
|
||
| learning_curve( | ||
| estimator, | ||
| X, | ||
| y, | ||
| cv=3, | ||
| exploit_incremental_learning=True, | ||
| train_sizes=np.linspace(0.1, 1.0, 10), | ||
| error_score="raise", | ||
| params={"sample_weight": np.ones(2)}, | ||
| ) | ||
|
|
There was a problem hiding this comment.
I have removed these tests:
- The first test now raises earlier (at
fit_params_iter) and so this test has lost its purpose. - The second test should also raise, because
sample_weightof shape (2,) gets passed together with sample size 30. This should not be allowed. This test is wrong.
A bit of digging why this test existed in the first place: It might be that it was intended to fit prior code where we had passed **fit_params as a whole into partial_fit like here:
scikit-learn/sklearn/model_selection/_validation.py
Lines 2159 to 2162 in dce0550
Users had to cut down their sample_weight before passing it, which is surprising to me at a glance.
| (cross_val_predict, {}), | ||
| (learning_curve, {}), | ||
| (permutation_test_score, {}), | ||
| (permutation_test_score, {"n_permutations": 20}), |
There was a problem hiding this comment.
This addresses the issue, that a pytest worker stops running for permutation_test_score with
worker 'gw1' crashed while running 'model_selection/tests/test_validation.py::test_validation_functions_routing[permutation_test_score-extra_args4]'
It is a bit of a lazy fix, since I couldn't investigate the real reason, but I think it's enough here.
|
@OmarManzoor maybe you could have a look here? |
OmarManzoor
left a comment
There was a problem hiding this comment.
Thank you for the PR @StefanieSenger
I left a few comments mainly related to some possible refactoring.
| func_names = { | ||
| "cross_validate": ("_score", "__call__"), | ||
| "cross_val_score": ("_score", "__call__"), | ||
| "learning_curve": ("_score", "__call__"), | ||
| "permutation_test_score": ("_score", "__call__"), | ||
| "validation_curve": ("_score", "__call__"), | ||
| } |
There was a problem hiding this comment.
This mapping seems to be the same. Why not just use this below?
method="_score",
parent="__call__",There was a problem hiding this comment.
Oh yes of cause! 🤦♀️ I really wonder why I didn't see that myself. 😆
| func_names = { | ||
| "cross_validate": ("fit", "_fit_and_score"), | ||
| "cross_val_score": ("fit", "_fit_and_score"), | ||
| "cross_val_predict": ("fit", "_fit_and_predict"), | ||
| "learning_curve": ("fit", "_fit_and_score"), | ||
| "permutation_test_score": ("fit", "_permutation_test_score"), | ||
| "validation_curve": ("fit", "_fit_and_score"), | ||
| } |
There was a problem hiding this comment.
Similar to the above comment this could be called fit_func_names and the first parameter fit removed?
Or maybe just create one mapping dictionary like this:
func_names = {
"cross_validate": {"split": "<genexpr>", "fit": "_fit_and_score"},
"cross_val_score": {"split": "<genexpr>", "fit": "_fit_and_score"},
"cross_val_predict": {"split": "cross_val_predict", "fit": "_fit_and_predict"},
"learning_curve": {"split": "learning_curve", "fit": "_fit_and_score"},
"permutation_test_score": {"split": "_permutation_test_score", "fit": "_permutation_test_score"},
"validation_curve": {"split": "<genexpr>", "fit": "_fit_and_score"},
}And this can be used in all three cases. We can add "score" too but that seems constant so we could use it directly like mentioned in the comment above.
There was a problem hiding this comment.
Great suggestion! Thank you. :)
Co-authored-by: Omar Salman <[email protected]>
StefanieSenger
left a comment
There was a problem hiding this comment.
Thank you for reviewing, @OmarManzoor. I've applied your suggestions.
| func_names = { | ||
| "cross_validate": ("_score", "__call__"), | ||
| "cross_val_score": ("_score", "__call__"), | ||
| "learning_curve": ("_score", "__call__"), | ||
| "permutation_test_score": ("_score", "__call__"), | ||
| "validation_curve": ("_score", "__call__"), | ||
| } |
There was a problem hiding this comment.
Oh yes of cause! 🤦♀️ I really wonder why I didn't see that myself. 😆
| func_names = { | ||
| "cross_validate": ("fit", "_fit_and_score"), | ||
| "cross_val_score": ("fit", "_fit_and_score"), | ||
| "cross_val_predict": ("fit", "_fit_and_predict"), | ||
| "learning_curve": ("fit", "_fit_and_score"), | ||
| "permutation_test_score": ("fit", "_permutation_test_score"), | ||
| "validation_curve": ("fit", "_fit_and_score"), | ||
| } |
There was a problem hiding this comment.
Great suggestion! Thank you. :)
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thank you @StefanieSenger
Reference Issues/PRs
closes #33283
also related to #28975
This PR is a split-off part of #33285 (which I have divided into two).
What does this implement/fix? Explain your changes.
With
exploit_incremental_learning=Truewe call the estimator'spartial_fitmethod via_incremental_fit_estimator. In this process,sample_weightin bothfit_paramsandscore_paramsneeds to get split following the same rules asXandy. Both was not done correctly before.I've also fixed the related module test that did not detect this bug.
AI usage disclosure
I used AI assistance for: