Skip to content

FIX metadata routing in learning_curve and related module tests - #34039

Merged
OmarManzoor merged 4 commits into
scikit-learn:mainfrom
StefanieSenger:fix_metadata_learning_curve
May 20, 2026
Merged

OmarManzoor merged 4 commits into
scikit-learn:mainfrom
StefanieSenger:fix_metadata_learning_curve

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented May 18, 2026 •

Copy link
Copy Markdown
Member

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=True we call the estimator's partial_fit method via _incremental_fit_estimator. In this process, sample_weight in both fit_params and score_params needs to get split following the same rules as X and y. 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:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Comment on lines -1572 to -1594
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)},
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed these tests:

  1. The first test now raises earlier (at fit_params_iter) and so this test has lost its purpose.
  2. The second test should also raise, because sample_weight of 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:

if classes is None:
partial_fit_func = partial(estimator.partial_fit, **fit_params)
else:
partial_fit_func = partial(estimator.partial_fit, classes=classes, **fit_params)

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}),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-project-automation github-project-automation Bot moved this to Todo in Labs May 18, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs May 18, 2026
@StefanieSenger StefanieSenger added the Metadata Routing all issues related to metadata routing, slep006, sample props label May 18, 2026
@adrinjalali

Copy link
Copy Markdown
Member

@OmarManzoor maybe you could have a look here?

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @StefanieSenger
I left a few comments mainly related to some possible refactoring.

Comment on lines +2623 to +2629
func_names = {
"cross_validate": ("_score", "__call__"),
"cross_val_score": ("_score", "__call__"),
"learning_curve": ("_score", "__call__"),
"permutation_test_score": ("_score", "__call__"),
"validation_curve": ("_score", "__call__"),
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mapping seems to be the same. Why not just use this below?

method="_score",
parent="__call__",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes of cause! 🤦‍♀️ I really wonder why I didn't see that myself. 😆

Comment on lines +2659 to +2666
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"),
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Thank you. :)

Comment thread sklearn/model_selection/_validation.py Resolved

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing, @OmarManzoor. I've applied your suggestions.

Comment on lines +2623 to +2629
func_names = {
"cross_validate": ("_score", "__call__"),
"cross_val_score": ("_score", "__call__"),
"learning_curve": ("_score", "__call__"),
"permutation_test_score": ("_score", "__call__"),
"validation_curve": ("_score", "__call__"),
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes of cause! 🤦‍♀️ I really wonder why I didn't see that myself. 😆

Comment on lines +2659 to +2666
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"),
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Thank you. :)

Comment thread sklearn/model_selection/_validation.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @StefanieSenger

@OmarManzoor
OmarManzoor merged commit e4cc780 into scikit-learn:main May 20, 2026
33 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs May 20, 2026
@jeremiedbb jeremiedbb added this to the 1.9 milestone May 20, 2026
@jeremiedbb jeremiedbb added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label May 20, 2026
@StefanieSenger
StefanieSenger deleted the fix_metadata_learning_curve branch May 20, 2026 15:04
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props module:model_selection To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

learning_curve with exploit_incremental_learning=True fails to compute training scores when sample_weight is provided

4 participants