8000 Wrong behaviour when calculating `f1_score` with `zero_division=1` · Issue #26965 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content
Wrong behaviour when calculating f1_score with zero_division=1 #26965
Closed
@mwydmuch

Description

@mwydmuch

Describe the bug

It seems that in the new version 1.3 f1_score with zero_division=1 incorrectly calculates the results. It treats the prediction with 0 true positives as a division by zero and assigned 1. This was not the case in 1.2.X version and below, which were triggering division by zero only when true positives, false negatives, and false positives were 0. The example below demonstrates the issue. This bug may result in over-positive estimates, especially when using macro averaging in multi-label classification.

Steps/Code to Reproduce

y_true = np.array([0, 1])
y_pred = np.array([1, 0])
print(f1_score(y_true, y_pred, zero_division=1)) # Should be 0.0

y_true = np.array([0, 1])
y_pred = np.array([0, 1])
print(f1_score(y_true, y_pred, zero_division=1)) # Should be 1.0

y_true = np.array([0, 1])
y_pred = np.array([0, 0])
print(f1_score(y_true, y_pred, zero_division=1)) # Should be 0.0

y_true = np.array([0, 0])
y_pred = np.array([0, 0])
print(f1_score(y_true, y_pred, zero_division=1)) # Here division by zero should be triggered resulting in 1.0

Expected Results

In versions 1.2.X and below, the correct results are printed, that is 0.0, 1.0, 0.0, 1.0,

Actual Results

The new version 1.3 prints 1.0, 1.0, 0.0, 1.0 - the first value is incorrect.

Versions

System:
    python: 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
executable: /usr/bin/python
   machine: Linux-5.19.0-50-generic-x86_64-with-glibc2.35

Python dependencies:
      sklearn: 1.3.0
          pip: 22.0.2
   setuptools: 59.6.0
        numpy: 1.25.1
        scipy: 1.10.0
       Cython: None
       pandas: 1.5.3
   matplotlib: 3.7.1
       joblib: 1.2.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /home/marek/.local/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 16

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /usr/local/lib/python3.10/dist-packages/numpy.libs/libopenblas64_p-r0-7a851222.3.23.so
        version: 0.3.23
threading_layer: pthreads
   architecture: Zen
    num_threads: 16

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /home/marek/.local/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Zen
    num_threads: 16

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

      0