Skip to content

RFC Metric pos_label handling for multiclass (and multilabel) data #33143

Open

Description

@lucyleeow

Warning

This is a request for comment (RFC), please do not open a pull request for this issue. If you wish to contribute to scikit-learn please have a look at our contributing doc and in particular the section New contributors.

Noticed in #32755 (comment)

Here are the metrics that have a pos_label:

METRICS_WITH_POS_LABEL = {
"confusion_matrix_at_thresholds",
"roc_curve",
"precision_recall_curve",
"det_curve",
"brier_score_loss",
"d2_brier_score",
"precision_score",
"recall_score",
"f1_score",
"f2_score",
"f0.5_score",
"jaccard_score",
"average_precision_score",
"weighted_average_precision_score",
"micro_average_precision_score",
"samples_average_precision_score",

Of these, some only support binary input (e.g., roc_curve, confusion_matrix_at_thresholds).

Of the metrics that also support multi-class input, most silently ignore the pos_label parameter if data is multiclass (or multilabel) but pos_label is also set. average_precision_score is the only one that raises an error:

elif y_type == "multilabel-indicator" and pos_label != 1:
raise ValueError(
"Parameter pos_label is fixed to 1 for multilabel-indicator y_true. "
"Do not set pos_label or set pos_label to 1."
)
elif y_type == "multiclass":
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."

I am not sure whether it is better to error or silently ignore (or third option, warn) - but some consistency is probably ideal.

How should pos_label be handled?

cc @StefanieSenger @AnneBeyer @ogrisel ?

Edit:

Side note, brier_score_loss and d2_brier_score do not explicitly state that pos_label is ignored, but probably should. Maybe you'd be interested in tackling this @AnneBeyer ?

Activity

  1. ogrisel commented on Feb 2, 2026

    @ogrisel
    Member

    Maybe we could start by raising FutureWarning and later raise ValueError when pos_label is set to a non-default value for a metric function called with multiclass targets?

  2. lucyleeow commented on Feb 3, 2026

    @lucyleeow
    MemberAuthor

    Maybe we could start by raising FutureWarning and later raise ValueError when pos_label is set to a non-default value for a metric function called with multiclass targets?

    I would be fine with this.

    To further complicate things (or make the decision easier) see: #29734 - it seems that f1_score (and I imagine the whole precision_recall_fscore_support family) function ignores pos_label in multiclass, but when you create a scorer using it, the resulting scorer does not.

    I don't have a great feel of when/why users may be making this error and whether people would be relying on the function ignoring pos_label, or why the decision was made historically.

    Note precision_recall_fscore_support (and family) also have the labels kwarg which is to be used in multiclass cases, which can be used to get 'binary' like results:

     For multiclass or multilabel targets, set `labels=[pos_label]` and
      `average != 'binary'` to report metrics for one label only.
    

    Maybe @jeremiedbb or @thomasjpfan may know more?

  3. thomasjpfan commented on Feb 5, 2026

    @thomasjpfan
    Member

    I do not have historical context on the current behavior.

    Note precision_recall_fscore_support (and family) also have the labels kwarg which is to be used in multiclass cases, which can be used to get 'binary' like results:

    Does that mean it's treating the labels like one-vs-rest?

    I agree it'll be nicer to get to an API where we do not accept non-default pos_label for multi-class targets. I suspect we'll end up needing a sentinel for the non-default. For example:

    y_true = np.array([0, 0, 1, 1, 2, 2])
    y_scores = np.array([
        [0.7, 0.2, 0.1],
        [0.4, 0.3, 0.3],
        [0.1, 0.8, 0.1],
        [0.2, 0.3, 0.5],
        [0.4, 0.4, 0.2],
        [0.1, 0.2, 0.7],
    ])
    
    # no error, why not?
    average_precision_score(y_true, y_scores, pos_label=1)
    
    # errors
    average_precision_score(y_true, y_scores, pos_label=2)
  4. lucyleeow commented on Feb 5, 2026

    @lucyleeow
    MemberAuthor

    I suspect we'll end up needing a sentinel for the non-default.

    That is a whole other can of worms, see #10010 (specifically #10010 (comment))
    average_precision_score is also the only metric that does not support pos_label=None. I do not know why.

  5. lucyleeow commented on Feb 5, 2026

    @lucyleeow
    MemberAuthor

    Does that mean it's treating the labels like one-vs-rest?

    Yes AFAICT, it uses multilabel_confusion_matrix which says:

    Multiclass data will be treated as if binarized under a one-vs-rest
    transformation

    I would support:

    • add default of pos_label=None for average_precision_score (seems to be required before we implement the next item)
    • error when pos_label not set to None with y_type == "multiclass" (with or without future warning)
      (edited to clarify, as per @ogrisel's comment)

    (also thanks for your input @thomasjpfan !)

  6. ogrisel commented on Feb 6, 2026

    @ogrisel
    Member

    error when pos_label not set (with or without future warning)

    Don't you instead mean: "error when pos_label is set to != None with y_type == "multiclass"?

  7. lucyleeow commented on Mar 10, 2026

    @lucyleeow
    MemberAuthor

    As noticed in #33473, we should probably raise an error when we silently ignore pos_label in multiclass cases in any relevant Display classes (this may just be DecisionBoundaryDisplay)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions