Skip to content

FIX Error handling in ranking metrics supporting multiclass: average_precision_score, roc_auc_score and top_k_accuracy_score - #32912

Merged
StefanieSenger merged 6 commits into
scikit-learn:mainfrom
StefanieSenger:fix_multiclass_average_precision_score
Jan 2, 2026
Merged

StefanieSenger merged 6 commits into
scikit-learn:mainfrom
StefanieSenger:fix_multiclass_average_precision_score

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Dec 16, 2025 •

Copy link
Copy Markdown
Member

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_true is passed into average_precision_score, roc_auc_score and top_k_accuracy_score while y_score has only shape (n_classes, ) (aka lacks information on the non-dominant classes).

y_true is internally label-binarized to make it match a multi-label case.

There is no way to calculate y_score from a binarised y_true, since we lack information on the scores of the other classes.
Thus, y_score needs to be passed with shape (n_samples, n_classes).

The previous error raised in average_precision_score from check_array for instance was

E ValueError: Expected 2D array, got 1D array instead:
E array=[0.5 0.4 0.8 0.9 0.8 0.7].
E Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

which gives wrong advise. Users cannot deal with that by reshaping y_score.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

Discovered while adding the check_array_api_multiclass_classification_metric test case in #32909.

Also added some documentation on the multiclass case for average_precision_score, since it was lacking.

# 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(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed it to match the param name.

@StefanieSenger

StefanieSenger commented Dec 16, 2025 •

Copy link
Copy Markdown
Member Author

I think we don't need a changelog entry, since it is only the error message that changes, not the error type.

@StefanieSenger StefanieSenger added No Changelog Needed Quick Review For PRs that are quick to review labels Dec 16, 2025
@StefanieSenger StefanieSenger changed the title FIX Add error handling to average_precision_score multiclass case FIX Add error handling in average_precision_score multiclass case Dec 16, 2025
@github-project-automation github-project-automation Bot moved this to Todo in Labs Dec 16, 2025
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Dec 16, 2025

@lucyleeow lucyleeow left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sklearn/metrics/_ranking.py Resolved
@StefanieSenger

StefanieSenger commented Dec 18, 2025 •

Copy link
Copy Markdown
Member Author

Thanks, @lucyleeow

I think some other metrics in _ranking.py may also benefit from this extra check, even if just for consistency

Oh, very true! I will take care of all of them that apply from this module.

@StefanieSenger StefanieSenger changed the title FIX Add error handling in average_precision_score multiclass case FIX Error handling in ranking metrics supporting multiclass: average_precision_score, roc_auc_score and top_k_accuracy_score Dec 18, 2025
@StefanieSenger

Copy link
Copy Markdown
Member Author

So, except for average_precision_score, only roc_auc_score and top_k_accuracy_score accept a multiclass y_true and I have added these to the test.

@StefanieSenger StefanieSenger removed the Quick Review For PRs that are quick to review label Dec 18, 2025

@lucyleeow lucyleeow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Comment thread sklearn/metrics/tests/test_ranking.py Resolved
@StefanieSenger

Copy link
Copy Markdown
Member Author

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 (if not y_score.shape == y_true.shape versus if not y_score.ndim == 2). Also, I'm okay with changing the error messages in a way that involves differences between these three functions. So, I'd prefer to leave it as is.

@lucyleeow lucyleeow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@lucyleeow lucyleeow added the Waiting for Second Reviewer First reviewer is done, need a second one! label Dec 27, 2025

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}`."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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}`."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"`y_score.shape={y_score.shape}`."
f"`{y_score.shape = }`."

@StefanieSenger

Copy link
Copy Markdown
Member Author

Thanks for the review, @virchan!
I'll merge this.

@StefanieSenger
StefanieSenger merged commit e0c7788 into scikit-learn:main Jan 2, 2026
@github-project-automation github-project-automation Bot moved this from In progress to Done in Labs Jan 2, 2026
@StefanieSenger
StefanieSenger deleted the fix_multiclass_average_precision_score branch January 2, 2026 13:02
tonybaloney pushed a commit to tonybaloney/swe-complex-scikit-learn that referenced this pull request Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants