Repository navigation
Description
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:
scikit-learn/sklearn/metrics/tests/test_common.py
Lines 364 to 380 in 7fbe0c2
| 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:
scikit-learn/sklearn/metrics/_ranking.py
Lines 264 to 274 in 7fbe0c2
| 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
ogrisel commentedon Feb 2, 2026
Maybe we could start by raising
FutureWarningand later raiseValueErrorwhenpos_labelis set to a non-default value for a metric function called with multiclass targets?lucyleeow commentedon Feb 3, 2026
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 wholeprecision_recall_fscore_supportfamily) function ignorespos_labelin 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 thelabelskwarg which is to be used in multiclass cases, which can be used to get 'binary' like results:Maybe @jeremiedbb or @thomasjpfan may know more?
thomasjpfan commentedon Feb 5, 2026
I do not have historical context on the current behavior.
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_labelfor multi-class targets. I suspect we'll end up needing a sentinel for the non-default. For example:lucyleeow commentedon Feb 5, 2026
That is a whole other can of worms, see #10010 (specifically #10010 (comment))
average_precision_scoreis also the only metric that does not supportpos_label=None. I do not know why.lucyleeow commentedon Feb 5, 2026
Yes AFAICT, it uses
multilabel_confusion_matrixwhich says:I would support:
pos_label=Noneforaverage_precision_score(seems to be required before we implement the next item)pos_labelnot set toNonewithy_type == "multiclass"(with or without future warning)(edited to clarify, as per @ogrisel's comment)
(also thanks for your input @thomasjpfan !)
ogrisel commentedon Feb 6, 2026
Don't you instead mean: "error when
pos_labelis set to!= Nonewithy_type == "multiclass"?lucyleeow commentedon Mar 10, 2026
As noticed in #33473, we should probably raise an error when we silently ignore
pos_labelin multiclass cases in any relevant Display classes (this may just beDecisionBoundaryDisplay)