Skip to content

[Bug]: Large integers in the confusion matrix silently report false values #624

@fhausmann

Description

@fhausmann

Contact details

No response

What happened?

If you create a ConfusionMatrix from a numpy array with large integers, some metrics report false values.
This is due to integer overflow. The warnings are displayed, but no error is raised.

Steps to reproduce

>>> from pycm import *
>>> import numpy as np
>>> maxval = 10e17
>>> matrix = np.array([[maxval,maxval],[maxval,2]]).astype(int)
>>> cm = ConfusionMatrix(matrix=matrix)
>>> cm.class_stat["MCC"]
{0: np.float64(2976900914.971392), 1: np.float64(2976900914.971392)}

Expected behavior

>>> from pycm import *
>>> import numpy as np
>>> maxval = 10e17
>>> matrix = np.array([[maxval,maxval],[maxval,2]]).astype(int)
>>> cm = ConfusionMatrix(matrix=matrix)
>>> cm.class_stat["MCC"]
{0: -0.5, 1: -0.5}

Actual behavior

The overflow occurs, and a wrong value, which is way too large, is reported.

Operating system

Linux

Python version

Python 3.12

PyCM version

PyCM 4.5

Relevant log output

./pycm/pycm/class_funcs.py:228: RuntimeWarning: overflow encountered in scalar multiply
    result = (TP * TN - FP * FN) / \

./pycm/pycm/class_funcs.py:229: RuntimeWarning: overflow encountered in scalar multiply
    (math.sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions