Skip to content

Default argument pos_label=1 is not ignored in f1_score metric for multiclass classification #29734

Closed

Description

@slimebob1975

Describe the bug

I get a ValueError for pos_label=1 default argument value to f1_score metric with argument average='micro' for the iris flower classification problem:

ValueError: pos_label=1 is not a valid label: It should be one of ['setosa' 'versicolor' 'virginica']

According to the documentation, the pos_label argument should be ignored for the multiclass problem:

https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html#f1-score

The class to report if average='binary' and the data is binary, otherwise this parameter is ignored.

Setting pos_label explicitly to None solves the problem and produces the expected output, see below.

Steps/Code to Reproduce

# Import necessary libraries
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import make_scorer, f1_score

# Load the Iris dataset
data = load_iris()
X = data.data  # Features
y = data.target  # Labels

# Convert labels to string type
y = np.array([data.target_names[label] for label in data.target])

# Initialize the Linear Discriminant Analysis classifier
classifier = LinearDiscriminantAnalysis()

# Define a custom scorer using F1 score with average='micro'
f1_scorer = make_scorer(f1_score, average='micro', pos_label=1)

# Perform cross-validation with cross_val_score
try:
    scores = cross_val_score(classifier, X, y, cv=5, scoring=f1_scorer)
    print(f"Cross-validated F1 Scores (micro average): {scores}")
    print(f"Mean F1 Score: {np.mean(scores)}")
except ValueError as e:
    print(f"Error: {e}")

Expected Results

Cross-validated F1 Scores (micro average): [1.         1.         0.96666667 0.93333333 1.        ]
Mean F1 Score: 0.9800000000000001

Actual Results

Cross-validated F1 Scores (micro average): [nan nan nan nan nan]
Mean F1 Score: nan
[C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\model_selection\_validation.py:1000](file:///C:/Users/rgt0227/AppData/Local/anaconda3/Lib/site-packages/sklearn/model_selection/_validation.py#line=999): UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "[C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\metrics\_scorer.py", line 139](file:///C:/Users/rgt0227/AppData/Local/anaconda3/Lib/site-packages/sklearn/metrics/_scorer.py#line=138), in __call__
    score = scorer._score(
            ^^^^^^^^^^^^^^
  File "[C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\metrics\_scorer.py", line 371](file:///C:/Users/rgt0227/AppData/Local/anaconda3/Lib/site-packages/sklearn/metrics/_scorer.py#line=370), in _score
    y_pred = method_caller(
             ^^^^^^^^^^^^^^
  File "[C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\metrics\_scorer.py", line 89](file:///C:/Users/rgt0227/AppData/Local/anaconda3/Lib/site-packages/sklearn/metrics/_scorer.py#line=88), in _cached_call
    result, _ = _get_response_values(
                ^^^^^^^^^^^^^^^^^^^^^
  File "[C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\utils\_response.py", line 204](file:///C:/Users/rgt0227/AppData/Local/anaconda3/Lib/site-packages/sklearn/utils/_response.py#line=203), in _get_response_values
    raise ValueError(
ValueError: pos_label=1 is not a valid label: It should be one of ['setosa' 'versicolor' 'virginica']

Versions

System:
    python: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\rgt0227\AppData\Local\anaconda3\python.exe
   machine: Windows-10-10.0.19045-SP0

Python dependencies:
      sklearn: 1.5.0
          pip: 23.2.1
   setuptools: 68.0.0
        numpy: 1.26.2
        scipy: 1.11.4
       Cython: None
       pandas: 2.1.1
   matplotlib: 3.8.0
       joblib: 1.2.0
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: mkl
    num_threads: 8
         prefix: mkl_rt
       filepath: C:\Users\rgt0227\AppData\Local\anaconda3\Library\bin\mkl_rt.2.dll
        version: 2023.1-Product
threading_layer: intel

       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: vcomp
       filepath: C:\Users\rgt0227\AppData\Local\anaconda3\Lib\site-packages\sklearn\.libs\vcomp140.dll
        version: None

Activity

  1. added
    Needs Triage
    on Aug 29, 2024
  2. glemaitre commented on Aug 29, 2024

    @glemaitre
    Member

    Indeed this is a bug. We have a discrepancy between the f1_score function and the scorer.

    y_pred = classifier.predict(X)
    f1_score(y, y_pred, average="micro", pos_label=1)
    

    will work because it ignores the pos_label. However, I think that the scorer will raise an error before to actually call the f1_score function because it is less lenient.

    I don't really know what is the right fix here. We could have either to modify f1_score but it could be tricky. We could otherwise implement a workaround in the scorer but this is weird to special case this function. So we should carefully assess the problem and check what are all the possible resolution.

  3. removed
    Needs Triage
    on Aug 29, 2024
  4. Higgs32584 commented on Sep 14, 2024

    @Higgs32584
    Contributor

    it also appears that both average='micro' and pos_label=None have to be present for the code to run successfully. If either is removed, we get an error as well.

  5. Higgs32584 commented on Sep 14, 2024

    @Higgs32584
    Contributor

    I tried various combinations of params and this is what I got for each
    for Micro, macro, and weighted, when pos_label=None, it worked. For samples and binary, it did not, which makes sense for binary, idk how samples works. when pos_label was 1, none of the averages worked.

  6. Nokobo commented on Sep 25, 2024

    @Nokobo

    Ah, this helps. Thanks for the clarification!

    Bug-throwing code:

    I have also encountered this bug while using scikit-learn 1.4.2.
    I am evaluating multiple scores by assigning a dictionary to the scoring parameter like so:

    scoring = {
        'precision': make_scorer(precision_score, average='macro', zero_division=0),
        'recall': make_scorer(recall_score, average='macro', zero_division=0),
        'f1': make_scorer(f1_score, average='macro', zero_division=0)
    }
    
    results = cross_validate(model, X, y, cv=cv, scoring=scoring)

    Working Dicts

    My code was only showing errors after I removed balanced_accuracy_score, which was my leading score until then (calculating the same value as the macro recall). Apparently, using multiple scorers and having a "working" one in the first position also fixes every scorer that comes after.

    Alternatively, it is also enough to assign pos_label=None to just the first scorer.

    scoring = {
        'accuracy': make_scorer(balanced_accuracy_score),
        'precision': make_scorer(precision_score, average='macro', zero_division=0),
        'recall': make_scorer(recall_score, average='macro', zero_division=0),
        'f1': make_scorer(f1_score, average='macro', zero_division=0)
    }

    or

    scoring = {
        'precision': make_scorer(precision_score, average='macro', pos_label=None, zero_division=0),
        'recall': make_scorer(recall_score, average='macro', zero_division=0),
        'f1': make_scorer(f1_score, average='macro', zero_division=0)
    }
    
  7. added
    good first issue
    on Jun 16, 2025
  8. ArmandNM commented on Jun 16, 2025

    @ArmandNM

    /take

  9. sirzechs66 commented on Jun 24, 2025

    @sirzechs66

    is the issue resolved?

    i am trying to do a fix it and having some questions if anyone available could you please work with me for the fix
    thank you

  10. removed
    good first issue
    on Jul 7, 2025
  11. lucyleeow commented on Jul 7, 2025

    @lucyleeow
    Member

    Removing good first issue label as per:

    I don't really know what is the right fix here. We could have either to modify f1_score but it could be tricky. We could otherwise implement a workaround in the scorer but this is weird to special case this function. So we should carefully assess the problem and check what are all the possible resolution.

    We should think about what the right fix is first

  12. jeremiedbb commented on Apr 7, 2026

    @jeremiedbb
    Member

    In main the reproducer gives the expected result. I believe it was fixed in #26840. I'm closing this one

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions