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
add an argument to make_scorer to enable passing labels argument to the custom loss function
Related to this - perhaps also allow make_scorer's need_threshold = True to be used for non-binary classification problems. Not sure why it's limited to binary. Totally meaningful for multi-class situations, where the metric might use the decision function to evaluate based on the rank order or something (for example, SVC doesn't have a cheap predict_proba, but has a cheap decision_function, so it would allow it to be used with rank-based metris). From make_scorer perspective, it just seems like a completely arbitrary restriction because it shouldn't care about what the custom loss function does with the decision_function output.
Reacted by IvoMerchiers, David Cottrell and sharpe5
add an argument to make_scorer to enable passing labels argument to
the custom loss function
Related to this - perhaps also allow make_scorer's need_threshold = True
to be used for non-binary classification problems. Not sure why it's
limited to binary. Totally meaningful for multi-class situations, where the
metric might use the decision function to evaluate based on the rank order
or something (for example, SVC doesn't have a cheap predict_proba, but
has a cheap decision_function, so it would allow it to be used with
rank-based metris). From make_scorer perspective, it just seems like a
completely arbitrary restriction because it shouldn't care about what the
custom loss function does with the decision_function output.
—
Reply to this email directly or view it on GitHub #6231.
@jnothman - I don't see how it would work. I meant that the when the classifier finishes training, its classes_ attribute should be passed to the custom loss function. This would help avoid calling LabelEncoder inside the custom loss function (which is required to match the unlabeled np.ndarray returned by predict_proba with the labels in the true y).
make_scorer can only pass additional kwargs to the custom loss function if these arguments are known at the time make_scorer is called. But make_scorer is called to create a scorer, which is to be used on arbitrary future datasets, with arbitrary future classifiers. The class labels won't be known until the classifier is trained on a particular dataset.
On another thought, if sklearn were to return DataFrame instead of np.ndarray this would be unnecessary. But I doubt it's in the plans?
And I agree this is an issue with the current design of the scorers. I think the classifiers should actually pass the classes_ to the scorer. There is even more issues with this, if this happens in cross-validation, for example, as different folds might have different classes, so we might want to tell all classifiers what the classes are (but that proposal was rejected a while ago because it really clutters the API).
I'm fine with a fix on the scorer level, though. The scorers could actually inspect the classes_ attribute and act accordingly. This is somewhat of a big API change though. I'd be happy to see a test and PRs though.
This relates closely to how we compute the R^2 by the way, but that is harder to fix. The R^2 should use the training set mean, not the test set mean, but that's unknown to the scorer. Getting classes_ in the call from scorer is easy, but the R^2 computation is hard, as the model doesn't actually store the class mean :-(
Ideally I'd fix both issues in one go, but I'm not sure how to do that.
I feel this is pretty important, as currently results can be pretty wrong if not all classes are present in a test set.
On second thought, I'm not sure what the right way to handle this is, though. Should we warn or error if macro-average is requested and not all classes are present?
I'd be interested in seeing scorers access classes_ to pass that on to
capable metrics...
On 8 October 2016 at 05:30, Andreas Mueller [email protected]
wrote:
+1 on adding labels to all metrics.
And I agree this is an issue with the current design of the scorers. I
think the classifiers should actually pass the classes_ to the scorer.
There is even more issues with this, if this happens in cross-validation,
for example, as different folds might have different classes, so we might
want to tell all classifiers what the classes are (but that proposal was
rejected a while ago because it really clutters the API).
I'm fine with a fix on the scorer level, though. The scorers could
actually inspect the classes_ attribute and act accordingly. This is
somewhat of a big API change though. I'd be happy to see a test and PRs
though.
Hm so we make labels a required parameter (with deprecation)?
and do we want to store the labels in the scorer actually? we can also just make it a required argument of the __call__ and always provide it in GridSearchCV and cross_val_score etc.
In general this problem also happens for the score method of GridSearchCV, where we don't have labels... but we stored them in fit... but these where the training labels. But I guess that's the best we can do?
Hi all,
I am recently reading Advances in Financial Machine Learning and this bug is discussed here. I cannot find any links to '2.0' mentioned above. I wanted to ask the community, has a smarter person than me come up with a solution to this yet?
If the function support labels then we can pass it when calling make_scorer. However, we are not consistent regarding the labels parameter in the different scoring functions.
Hm that would fix the issue in cross-validation with different classes in different test sets, but if there are different classes in the training set, it's still tricky, right?
Hi folks
Sorry to chase, just wanted to keep this issue active. Let me know if I can assist. Seems like an important one to me. (correct me if I am wrong)
Great reminder Ahmed
Dear sklearn team, where are your thoughts on this? If its something you don't want to pursue, pls just reply confirming that so that we stop chasing you
Hm that would fix the issue in cross-validation with different classes in different test sets, but if there are different classes in the training set, it's still tricky, right?
I think a general *CV meta-estimator won't work in that case. Specialized ones, like LogisticRegressionCV, are now able to do that.
Activity
jnothman commentedon Jan 26, 2016
Responding to that second dot point only, make_scorer will automatically
pass on any additional kwargs.
On 26 January 2016 at 15:32, pkch [email protected] wrote:
pkch commentedon Jan 28, 2016
@jnothman - I don't see how it would work. I meant that the when the classifier finishes training, its
classes_attribute should be passed to the custom loss function. This would help avoid callingLabelEncoderinside the custom loss function (which is required to match the unlabelednp.ndarrayreturned bypredict_probawith the labels in the truey).make_scorercan only pass additional kwargs to the custom loss function if these arguments are known at the timemake_scoreris called. Butmake_scoreris called to create a scorer, which is to be used on arbitrary future datasets, with arbitrary future classifiers. The class labels won't be known until the classifier is trained on a particular dataset.On another thought, if
sklearnwere to returnDataFrameinstead ofnp.ndarraythis would be unnecessary. But I doubt it's in the plans?amueller commentedon Oct 7, 2016
+1 on adding
labelsto all metrics.And I agree this is an issue with the current design of the scorers. I think the classifiers should actually pass the
classes_to the scorer. There is even more issues with this, if this happens in cross-validation, for example, as different folds might have different classes, so we might want to tell all classifiers what the classes are (but that proposal was rejected a while ago because it really clutters the API).I'm fine with a fix on the scorer level, though. The scorers could actually inspect the
classes_attribute and act accordingly. This is somewhat of a big API change though. I'd be happy to see a test and PRs though.This relates closely to how we compute the R^2 by the way, but that is harder to fix. The R^2 should use the training set mean, not the test set mean, but that's unknown to the scorer. Getting
classes_in the call fromscoreris easy, but theR^2computation is hard, as the model doesn't actually store the class mean :-(Ideally I'd fix both issues in one go, but I'm not sure how to do that.
[-]Scoring functions[/-][+]Scoring functions don't know classes_[/+]amueller commentedon Oct 7, 2016
I feel this is pretty important, as currently results can be pretty wrong if not all classes are present in a test set.
On second thought, I'm not sure what the right way to handle this is, though. Should we warn or error if macro-average is requested and not all classes are present?
jnothman commentedon Oct 8, 2016
I'd be interested in seeing scorers access classes_ to pass that on to
capable metrics...
On 8 October 2016 at 05:30, Andreas Mueller [email protected]
wrote:
jnothman commentedon Jun 19, 2017
Re my comment at #9144 that scorer objects should look at the set of classes before cv, we should also consider how this might work with nested CV...
amueller commentedon Jun 20, 2017
Hm so we make
labelsa required parameter (with deprecation)?and do we want to store the labels in the scorer actually? we can also just make it a required argument of the
__call__and always provide it in GridSearchCV and cross_val_score etc.In general this problem also happens for the
scoremethod ofGridSearchCV, where we don't havelabels... but we stored them infit... but these where the training labels. But I guess that's the best we can do?amueller commentedon Jun 20, 2017
If we don't pass the labels along with X and y, then doing it for the built-in ones and the
make_scorerones would be quite different.18 remaining items
adrinjalali commentedon Aug 22, 2021
Moving this to 2.0, this needs somebody to pay attention to it though.
nsp121 commentedon Jun 7, 2024
Hi all,
I am recently reading Advances in Financial Machine Learning and this bug is discussed here. I cannot find any links to '2.0' mentioned above. I wanted to ask the community, has a smarter person than me come up with a solution to this yet?
Context:
adrinjalali commentedon Jun 10, 2024
@glemaitre as for the scorers, don't we support labels in some cases now? I guess the scorer can now use
classes_to pass as labels?glemaitre commentedon Jun 10, 2024
If the function support
labelsthen we can pass it when callingmake_scorer. However, we are not consistent regarding thelabelsparameter in the different scoring functions.adrinjalali commentedon Jun 10, 2024
So I guess a next step here is to add
labelsto all scoring functions where it can be relevant?amueller commentedon Jun 13, 2024
Hm that would fix the issue in cross-validation with different classes in different test sets, but if there are different classes in the training set, it's still tricky, right?
nelsonpeaceAB commentedon Jun 25, 2024
Hi folks
Sorry to chase, just wanted to keep this issue active. Let me know if I can assist. Seems like an important one to me. (correct me if I am wrong)
Nels
nsp121 commentedon Jul 22, 2024
AhmedThahir commentedon Jan 26, 2025
Any updates?
nsp121 commentedon Jan 26, 2025
lorentzenchr commentedon Mar 16, 2026
gives
Accuracy does not need a
labelargument.I think a general *CV meta-estimator won't work in that case. Specialized ones, like
LogisticRegressionCV, are now able to do that.What is left to do for this issue?