Skip to content

Add zero_division for single class prediction in MCC #28982

@cynddl

Description

@cynddl

Describe the bug

I have found a potential edge case issue with sklearn.metrics.matthews_corrcoef. The example provided in the documentation works as expected:

from sklearn.metrics import matthews_corrcoef
matthews_corrcoef([1, 1, 1, -1], [1, -1, 1, 1])  # returns -1/3 OK

However, edge cases appear when either y or y_true have only one single label.

Steps/Code to Reproduce

from sklearn.metrics import matthews_corrcoef
matthews_corrcoef([1, 1, 1, 1], [1, 1, 1, 1])  # returns 0 instead of 1
matthews_corrcoef([0, 0, 0, 0], [0, 0, 0, 0])  # returns 0 instead of 1

Expected Results

Outputs should be 1, not 0.

Actual Results

Outputs are 0.

Versions

System:
    python: 3.11.9 (main, Apr  2 2024, 08:25:04) [Clang 16.0.6 ]
executable: /tmp/sklearn-test/.venv/bin/python
   machine: macOS-14.2.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.4.2
          pip: 24.0
   setuptools: 69.5.1
        numpy: 1.26.4
        scipy: 1.13.0
       Cython: None
       pandas: None
   matplotlib: None
       joblib: 1.4.2
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: libomp
       filepath: /tmp/sklearn-test/.venv/lib/python3.11/site-packages/torch/lib/libomp.dylib
        version: None

       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /tmp/sklearn-test/.venv/lib/python3.11/site-packages/numpy/.dylibs/libopenblas64_.0.dylib
        version: 0.3.23.dev
threading_layer: pthreads
   architecture: armv8

       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /tmp/sklearn-test/.venv/lib/python3.11/site-packages/scipy/.dylibs/libopenblas.0.dylib
        version: 0.3.26.dev
threading_layer: pthreads
   architecture: neoversen1

       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: libomp
       filepath: /tmp/sklearn-test/.venv/lib/python3.11/site-packages/sklearn/.dylibs/libomp.dylib
        version: None

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions