You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importnumpyasnpimportsklearnfromsklearn.dummyimportDummyClassifierfromsklearn.metricsimportaccuracy_score, make_scorerfromfairlearn.postprocessingimportThresholdOptimizerfromfairlearn.metricsimportdemographic_parity_differencesklearn.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 OKclassifier=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
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.
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 aThresholdOptimizer
estimator which needs (in addition to X and y) thesensitive_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.
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
orset_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
The text was updated successfully, but these errors were encountered: