Skip to content

Fix pos_label validation in multiclass scorers - #33473

Merged
ogrisel merged 10 commits into
scikit-learn:mainfrom
ogrisel:fix-multiclass-scorer-response
Mar 19, 2026
Merged

ogrisel merged 10 commits into
scikit-learn:mainfrom
ogrisel:fix-multiclass-scorer-response

Conversation

@ogrisel

@ogrisel ogrisel commented Mar 6, 2026 •

Copy link
Copy Markdown
Member

While working on an array API integration test (#32873) that calls cross_validate with scoring="average_precision" on a multiclass classification problem with string class labels, I discovered that our scorer wrongfully attempts to validate pos_label and raise ValueError because pos_label=1 (the current default in average_precision_score) is not one of the observed clf.classes_.

I isolated the problem with a minimal unit test so that we can fix that independently of the array API concerns discovered with this integration test.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thank you @ogrisel

@OmarManzoor OmarManzoor added the Waiting for Second Reviewer First reviewer is done, need a second one! label Mar 9, 2026

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

Thank you @ogrisel!

It feels a bit unintuitive to do nothing with "pos_lable" when it is explicitly passed but invalid, but I don't fully understand the discussions regarding setting it to None yet, so for now, leaving it unchecked and unchanged in the multiclass case seems fine to me.

(And getting rid of the re-raise in the DecisionBoundaryDisplay was also on my list.)

@ogrisel

ogrisel commented Mar 9, 2026

Copy link
Copy Markdown
Member Author

It feels a bit unintuitive to do nothing with "pos_lable" when it is explicitly passed but invalid,

The problem is that for average_precision_score (and related metric function) the default is pos_label=1 so we cannot know whether the user intended to pass it explicitly or if was left to its default value. But since pos_label is only meaningful for binary classification problems, I think we should always ignore it in the scoring machinery for multiclass tasks.

@ogrisel

ogrisel commented Mar 9, 2026

Copy link
Copy Markdown
Member Author

@lucyleeow any opinion on this?

Comment thread sklearn/utils/tests/test_response.py Resolved
- Fixed incorrect :class:`ValueError` when using
``scoring="average_precision"`` or similar in model selection utilities such
as `model_selection.GridSearchCV` or `model_selection.cross_validate` with
multiclass classifiers. The ``pos_label`` parameter is only relevant for

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.

From the public API, if one were to create a scorer with:

make_scorer(
    average_precision_score,
    response_method=("decision_function", "predict_proba"),
    pos_label="invalid_label",
)

will they still end up with this ValueError?

if pos_label != 1:
raise ValueError(
"Parameter pos_label is fixed to 1 for multiclass y_true. "
"Do not set pos_label or set pos_label to 1."
)

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.

I will update the test accordingly.

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.

I updated the test to use public API based on your suggestion 4317474. I think we can leave the changelog entry as is though. Both using make_scorer manually or passing score="average_precision" to a validation tool are valid ways to trigger the problem in the private _response.py module.

@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 - some nits around the other 'response' function docs.

Comment thread sklearn/utils/_response.py Resolved
Comment thread sklearn/utils/_response.py Resolved
Comment thread sklearn/utils/_response.py Resolved
Comment thread sklearn/utils/_response.py Resolved
Comment thread sklearn/utils/_response.py Resolved
classes = estimator.classes_
target_type = type_of_target(classes)

if target_type in ("binary", "multiclass"):

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 can't understand why "multiclass" used to be here. Looking at other places where multiclass data may be input to this function: CalibratedClassifierCV, partial_dependence - we never use pos_label. Decision boundary is the only place that it is used but it re-raises a more informative error... 🤔

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

Mis-clicked, was meant to approve

@lucyleeow

Copy link
Copy Markdown
Member

@lucyleeow any opinion on this?

Agree that it is not ideal to silently ignore pos_label. I'll comment in #33143, but I would support also raising an error for relevant displays.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Mar 19, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Mar 19, 2026
@ogrisel
ogrisel enabled auto-merge (squash) March 19, 2026 17:07
@ogrisel
ogrisel merged commit ba47342 into scikit-learn:main Mar 19, 2026
31 checks passed
@ogrisel
ogrisel deleted the fix-multiclass-scorer-response branch August 27, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:utils Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants