Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Routing metadata to the response_method used by a scorer #27977

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #22893
jeromedockes opened this issue Dec 18, 2023 · 2 comments
Open
Tracked by #22893

Routing metadata to the response_method used by a scorer #27977

jeromedockes opened this issue Dec 18, 2023 · 2 comments
Assignees
Milestone

Comments

@jeromedockes
Copy link
Contributor

Describe the workflow you want to enable

I would like to pass sample properties to the response method (eg predict) called by a scorer.
For example, the fairlearn package has a ThresholdOptimizer estimator which needs (in addition to X and y) the sensitive_features argument both for fit and predict.

AFAICT I can pass arguments to the score function (the metric), but not to the response method of the estimator.

import numpy as np
import sklearn
from sklearn.dummy import DummyClassifier
from sklearn.metrics import accuracy_score, make_scorer
from fairlearn.postprocessing import ThresholdOptimizer
from fairlearn.metrics import demographic_parity_difference


sklearn.set_config(enable_metadata_routing=True)

rng = np.random.default_rng(0)
X = rng.normal(size=(10, 3))
y = rng.integers(0, 2, size=X.shape[0])
sensitive = rng.integers(0, 2, size=X.shape[0])

classifier = (
    ThresholdOptimizer(estimator=DummyClassifier(), predict_method="auto")
    .set_fit_request(sensitive_features=True)
    .set_predict_request(sensitive_features=True)
    .fit(X, y, sensitive_features=sensitive)
)

scoring = make_scorer(accuracy_score)
scoring(classifier, X, y, sensitive_features=sensitive) # TypeError: predict() missing 1 argument -- how could I pass `sensitive_features to predict() ?

# passing arguments to the score function (demographic_parity_difference) is OK
classifier = DummyClassifier().fit(X, y)
scoring = make_scorer(
    demographic_parity_difference, greater_is_better=False
).set_score_request(sensitive_features=True)

scoring(classifier, X, y, sensitive_features=sensitive)

This also applies when using a scorer indirectly, for example in cross_validate

Describe your proposed solution

Maybe the scorers could have a method like set_predict_request or set_response_request to specify which parameters should be forwarded to the response method?

Describe alternatives you've considered, if relevant

No response

Additional context

https://fairlearn.org/v0.9/api_reference/generated/fairlearn.postprocessing.ThresholdOptimizer.html
https://fairlearn.org/v0.9/api_reference/generated/fairlearn.metrics.demographic_parity_difference.html

@jeromedockes jeromedockes added Needs Triage Issue requires triage New Feature labels Dec 18, 2023
@glemaitre glemaitre removed the Needs Triage Issue requires triage label Dec 18, 2023
@adrinjalali
Copy link
Member

So for fairlearn, the scorers can themselves be routers and report the metadata required by the response_method as requested by the scorer, and things should work out of the box for tools we've implemented the routing for here in scikit-learn.

As for scikit-learn scorers themselves, yes, we should make this happen, but it's not gonna be an easy one I think.

@glemaitre
Copy link
Member

We have the issue in TunedThresholdClassifierCV now. I'll put it in my TODO task for the next release.

@glemaitre glemaitre added this to the 1.6 milestone May 16, 2024
@glemaitre glemaitre self-assigned this May 16, 2024
@glemaitre glemaitre moved this to Todo in Metadata routing May 16, 2024
@glemaitre glemaitre modified the milestones: 1.6, 1.7 Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

3 participants