Skip to content

ENH Auto-requesting sample_weight on scorers - #35099

Merged
ogrisel merged 7 commits into
scikit-learn:mainfrom
StefanieSenger:auto-request_scorer
Oct 28, 2026
Merged

ogrisel merged 7 commits into
scikit-learn:mainfrom
StefanieSenger:auto-request_scorer

Conversation

@StefanieSenger

Copy link
Copy Markdown
Member

Reference Issues/PRs

towards #34901

What does this implement/fix? Explain your changes.

This adds auto-requesting sample_weight for _BaseScorer.score. Every time when a user passes a scorer into a cv object (or a CV meta-estimator), set_score_request(sample_weight=True) is set automatically, if the auto-routing is switched on.
_MulimetricScorers are affected through _BaseScorer.

I have also added tests and - along the way - refactored the existing metadata routing tests for scorers, so they are a bit easier to look at.

Note this PR doesn't account for an estimators score method (which is dealt with in #34966).

For reviewers: The first commit is a pure refactoring of the tests (without adding anything new) and the first commit adds the auto-request plus the test for this.

AI usage disclosure

I used AI assistance for:

  • Test/benchmark generation
  • Research and understanding

@StefanieSenger StefanieSenger added the Metadata Routing all issues related to metadata routing, slep006, sample props label Oct 8, 2026
@StefanieSenger StefanieSenger added this to the 1.10 milestone Oct 13, 2026

@antoinebaker antoinebaker left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @StefanieSenger for the PR, LGTM ! Here a few optional suggestions.

We could additionally test the routing via a cv object. The test will be a bit redundant with the mock _scorer_in_router, so maybe we can skip it. On the other hand we will have a direct test of the feature enhancement (mainly aimed at scorers wrapped in cv routers).
EDIT: suggestion to test via a cv object removed, I now think such a test would be superfluous.

I think only _BaseScorer instances are tested here ? Maybe we should add a _MultimetricScorer test case ?

Comment on lines +1369 to +1373
@config_context(enable_metadata_routing=True, enable_metadata_auto_requests=True)
def test_scorer_set_score_request_false_opts_out_of_auto_request():
"""Check that explicitly requesting `sample_weight=False` overrides the
auto-request."""
with config_context(enable_metadata_auto_requests=True):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The enable_metadata_auto_requests=True config_context is done twice.

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.

True, I fixed this.

Comment on lines +1335 to +1342
scorer_repr = repr(scorer)
err_msg = (
"[sample_weight] are passed but are not explicitly set as requested or"
f" not requested for {scorer_repr}.score, which is used within"
f" test.score."
f" Call `{scorer_repr}.set_score_request({{metadata}}=True/False)` for"
" each metadata you want to request/ignore."
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A shorter err_msg would be enough, maybe
"[sample_weight] are passed but are not explicitly set as"

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.

Done.

@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.

Thanks for reviewing, @antoinebaker!

Comment on lines +1335 to +1342
scorer_repr = repr(scorer)
err_msg = (
"[sample_weight] are passed but are not explicitly set as requested or"
f" not requested for {scorer_repr}.score, which is used within"
f" test.score."
f" Call `{scorer_repr}.set_score_request({{metadata}}=True/False)` for"
" each metadata you want to request/ignore."
)

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.

Done.

Comment on lines +1369 to +1373
@config_context(enable_metadata_routing=True, enable_metadata_auto_requests=True)
def test_scorer_set_score_request_false_opts_out_of_auto_request():
"""Check that explicitly requesting `sample_weight=False` overrides the
auto-request."""
with config_context(enable_metadata_auto_requests=True):

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.

True, I fixed this.

@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.

Thanks for reviewing, @antoinebaker!

@StefanieSenger

Copy link
Copy Markdown
Member Author

Do you mind having a look if this is mergable, @adrinjalali, @OmarManzoor or @ogrisel?

@StefanieSenger StefanieSenger added the Waiting for Second Reviewer First reviewer is done, need a second one! label Oct 20, 2026

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM as well. Merging.

@ogrisel
ogrisel merged commit 4d18ce1 into scikit-learn:main Oct 28, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Oct 28, 2026
@StefanieSenger
StefanieSenger deleted the auto-request_scorer branch November 2, 2026 14:21
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:metrics size/M Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants