Skip to content

Matthews correlation coefficient metric throws misleading division by zero RuntimeWarning #16924

@simberaj

Description

@simberaj

Description

With tested values all equal, sklearn.metrics.matthews_corrcoef throws a RuntimeWarning reporting a division by zero. This behavior was already reported in #1937 and reported fixed, but reappears in recent versions.

Steps/Code to Reproduce

The snippet below reproduces the warning.

import sklearn.metrics                         
trues = [1,0,1,1,0]                            
preds = [0,0,0,0,0]                            
sklearn.metrics.matthews_corrcoef(trues, preds)

Expected Results

No warning is thrown.

Actual Results

The following warning is thrown:

C:\anaconda\envs\sklearn-test\lib\site-packages\sklearn\metrics\_classification.py:900: RuntimeWarning: invalid value encountered in double_scalars
  mcc = cov_ytyp / np.sqrt(cov_ytyt * cov_ypyp)

Versions

System:
    python: 3.8.2 (default, Mar 25 2020, 08:56:29) [MSC v.1916 64 bit (AMD64)]
executable: C:\anaconda\envs\sklearn-test\python.exe
   machine: Windows-10-10.0.18362-SP0

Python dependencies:
       pip: 20.0.2
setuptools: 46.1.3.post20200330
   sklearn: 0.22.1
     numpy: 1.18.1
     scipy: 1.4.1
    Cython: None
    pandas: None
matplotlib: None
    joblib: 0.14.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEasyWell-defined and straightforward way to resolvemodule:metrics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions