Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Is this behavior a bug?: f1_score([1,0,0], [0,1,0], zero_division=1.0) returns 1.0 not 0.0 #26864

Closed
@ftnext

Description

@ftnext

Describe the bug

Thank you for the update in #25531

In scikit-learn 1.3.0, there seems to be a bug in the zero_division argument of the f1_score.
It appears to be degrading comparing with scikit-learn 1.2.2.

Steps/Code to Reproduce

>>> from sklearn.metrics import f1_score
>>> f1_score([1,0,0], [0,1,0])
0.0
>>> f1_score([1,0,0], [0,1,0], zero_division=1.0)
1.0

Expected Results

Returns 0.0 not 1.0.

In scikit-learn 1.2.2

>>> from sklearn.metrics import f1_score
>>> f1_score([1,0,0], [0,1,0])
0.0
>>> f1_score([1,0,0], [0,1,0], zero_division=1.0)
0.0

>>> confusion_matrix([1,0,0], [0,1,0])
array([[1, 1],
       [1, 0]])
  • precision 0.0
  • recall 0.0
  • -> f1_score should be 0.0 to show model's low performance

Actual Results

Returns 1.0


Other case (OK)

>>> f1_score([0,0,0], [0,0,0])  # raise UndefinedMetricWarning
0.0
>>> f1_score([0,0,0], [0,0,0], zero_division=1)
1.0

Versions

System:
    python: 3.9.16 (main, Jan  3 2023, 22:16:38)  [Clang 14.0.0 (clang-1400.0.29.202)]
executable: /.../sci13/bin/python
   machine: macOS-12.6.6-arm64-arm-64bit

Python dependencies:
      sklearn: 1.3.0
          pip: 23.2
   setuptools: 68.0.0
        numpy: 1.25.1
        scipy: 1.11.1
       Cython: None
       pandas: None
   matplotlib: None
       joblib: 1.3.1
threadpoolctl: 3.2.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 10
         prefix: libomp
       filepath: /.../sci13/lib/python3.9/site-packages/sklearn/.dylibs/libomp.dylib
        version: None

       user_api: blas
   internal_api: openblas
    num_threads: 10
         prefix: libopenblas
       filepath: /.../sci13/lib/python3.9/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
        version: 0.3.23
threading_layer: pthreads
   architecture: armv8

       user_api: blas
   internal_api: openblas
    num_threads: 10
         prefix: libopenblas
       filepath: /.../sci13/lib/python3.9/site-packages/scipy/.dylibs/libopenblas.0.dylib
        version: 0.3.21.dev
threading_layer: pthreads
   architecture: armv8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions