Repository navigation
FIX Error handling in ranking metrics supporting multiclass: average_precision_score, roc_auc_score and top_k_accuracy_score - #32912
Conversation
| # Raise an error for multiclass y_true with pos_label other than 1 | ||
| y_true = np.array([0, 1, 2, 0, 1, 2]) | ||
| y_pred = np.array( | ||
| y_score = np.array( |
There was a problem hiding this comment.
Renamed it to match the param name.
|
I think we don't need a changelog entry, since it is only the error message that changes, not the error type. |
average_precision_score multiclass caseaverage_precision_score multiclass case
There was a problem hiding this comment.
I think some other metrics in _ranking.py may also benefit from this extra check, even if just for consistency - e.g., roc_auc_score gives the following error (with the input from your new test), which is less informative:
AxisError: axis 1 is out of bounds for array of dimension 1
Otherwise looks good.
|
Thanks, @lucyleeow
Oh, very true! I will take care of all of them that apply from this module. |
average_precision_score multiclass caseaverage_precision_score, roc_auc_score and top_k_accuracy_score
|
So, except for |
lucyleeow
left a comment
There was a problem hiding this comment.
Do you think it is worth factorising this check out? Mostly so we can easily ensure the error message is always consistent, if it ever gets changed...
I don't think it's worth adding an additional layer (function to call from here) just to ensure consistency of the error messages. The conditions that to the error being raised are also slightly different ( |
virchan
left a comment
There was a problem hiding this comment.
LGTM! Thanks, @StefanieSenger!
I only have a few optional suggestions, but feel free to merge this as is.
| raise ValueError( | ||
| "`y_score` needs to be of shape `(n_samples, n_classes)`, since " | ||
| "`y_true` contains multiple classes. Got " | ||
| f"`y_score.shape={y_score.shape}`." |
There was a problem hiding this comment.
| f"`y_score.shape={y_score.shape}`." | |
| f"`{y_score.shape = }`." |
| raise ValueError( | ||
| "`y_score` needs to be of shape `(n_samples, n_classes)`, since " | ||
| "`y_true` contains multiple classes. Got " | ||
| f"`y_score.shape={y_score.shape}`." |
There was a problem hiding this comment.
| f"`y_score.shape={y_score.shape}`." | |
| f"`{y_score.shape = }`." |
|
Thanks for the review, @virchan! |
PR: scikit-learn#32912 Issue: scikit-learn#32909 Base commit: 4ac1079 Changed lines: 41
Reference Issues/PRs
discovered while working in #32909
What does this implement/fix? Explain your changes.
This adds a proper error handling if a multiclass
y_trueis passed intoaverage_precision_score,roc_auc_scoreandtop_k_accuracy_scorewhiley_scorehas only shape(n_classes, )(aka lacks information on the non-dominant classes).y_trueis internally label-binarized to make it match a multi-label case.There is no way to calculate
y_scorefrom a binarisedy_true, since we lack information on the scores of the other classes.Thus,
y_scoreneeds to be passed with shape(n_samples, n_classes).The previous error raised in
average_precision_scorefromcheck_arrayfor instance waswhich gives wrong advise. Users cannot deal with that by reshaping
y_score.AI usage disclosure
I used AI assistance for:
Any other comments?
Discovered while adding the
check_array_api_multiclass_classification_metrictest case in #32909.Also added some documentation on the multiclass case for
average_precision_score, since it was lacking.