Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork26k
FIX metadata routing for scorer#31654
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
github-actionsbot commentedJun 25, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
❌ Linting issuesThis PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling You can see the details of the linting issues under the
|
A bit fighting with the |
StefanieSenger left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Hi@tomMoral, thanks for fixing this complicated bug. I was looking into this a bit and I have a few comments that mainly deal with the case the new test is written for (scoring is done via the default scoring and the estimator's predict method). I only touched the surface but I am not sure how to handle this on a more structural level. (The others will be able to help you better.) I still hope it's a bit useful and helps this move forwards. Otherwise feel free to ignore. :)
Maybe this PR could only deal with the scoring of a single metric and multimetric scoring could be a separate PR. I think it's easier to implement and review if it's separated?
@@ -396,17 +429,57 @@ def _score(self, method_caller, estimator, X, y_true, **kwargs): | |||
) | |||
pos_label = None if is_regressor(estimator) else self._get_pos_label() | |||
response_method = _check_response_method(estimator, self._response_method) | |||
routed_params = self._route_scorer_metadata(estimator, kwargs) |
StefanieSengerJul 11, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
self._route_scorer_metadata(estimator, kwargs)
returns the metadata that should be routed to the estimator's response method viarouted_params.estimator[method_name]
(which makes it possible to calculatey_pred
taking the metadata into account.
But it doesn't add the metadata (passed intoscore()
intest_scorer_metadata_routing
) to the scoring viarouted_params.score.score
.
I think that's because_Score._score_func
(such asaccuracy_score()
) does not expect a param called "metadata" (only "sample_weight" maybe). A param called "metadata" gets filtered out.
I think we cannot test a param "matadata" with scikit-learn's metrics, because they don't know what that is.
.add( | ||
score=self, | ||
method_mapping=MethodMapping().add(caller="score", callee="score"), | ||
) |
StefanieSengerJul 11, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
This reminds me on how we create aMetadataRouter
for splitters, likecross_validate
, where we pretend the splitter function was a caller namedfit
, and then we route to the calleessplit
andscore
through the router.
In general, for adding a request to consume metadata by a method of the present instance (self), there is theMetadataRouter.add_self_request(self)
method. In this case here, we route toself._score_func
.
I wonder and am a bit confused about whether that would that be a self-request or not and if that matters.
@@ -1392,6 +1393,31 @@ def score2(y_true, y_pred, common_arg=None, sample_weight=None): | |||
multi_scorer(clf, X, y, common_arg=1, sample_weight=1) | |||
@config_context(enable_metadata_routing=True) | |||
def test_scorer_metadata_routing(): |
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.
For testing if metadata is actually routed and used we have used test classes from thesklearn/tests/metadata_routing_common.py
module in the past. These accept a_Registry()
that is used to store routed metadata.
Seesklearn/compose/tests/test_column_transformer.test_metadata_routing_for_column_transformer
for an example.
Uh oh!
There was an error while loading.Please reload this page.
Fixes#27977
This PR add metadata routing to the estimator to score when calling a scorer.
One thing that is not easy to understand is that here, the routing needs to be dynamic, as it depends on the estimator that is being scored.
For now it is implemented for unique scorer, I have to see how to do this for multiple scorer...
TODO