Repository navigation
ENH Auto-requesting sample_weight on scorers - #35099
Conversation
There was a problem hiding this comment.
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 ?
| @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): |
There was a problem hiding this comment.
The enable_metadata_auto_requests=True config_context is done twice.
There was a problem hiding this comment.
True, I fixed this.
| 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." | ||
| ) |
There was a problem hiding this comment.
A shorter err_msg would be enough, maybe
"[sample_weight] are passed but are not explicitly set as"
StefanieSenger
left a comment
There was a problem hiding this comment.
Thanks for reviewing, @antoinebaker!
| 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." | ||
| ) |
| @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): |
There was a problem hiding this comment.
True, I fixed this.
StefanieSenger
left a comment
There was a problem hiding this comment.
Thanks for reviewing, @antoinebaker!
|
Do you mind having a look if this is mergable, @adrinjalali, @OmarManzoor or @ogrisel? |
Reference Issues/PRs
towards #34901
What does this implement/fix? Explain your changes.
This adds auto-requesting
sample_weightfor_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: