meeliskull / prg

Software to create Precision-Recall-Gain curves and calculate area under the curve
38 stars 18 forks source link

Python: ValueError exception when only one positive label has the lowest score #6

Open perellonieto opened 4 years ago

perellonieto commented 4 years ago

The following example contains two cases that raise the exception shown below. I am not sure if it happens only when there is only one positive sample and it has the lowest output score. Need to investigate further.

from prg import prg
import numpy as np

y_true = np.array([1, 1, 0, 0], dtype='int')
scores = np.arange(4, 1, -1)
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)

y_true = np.array([0, 0, 1, 1], dtype='int')
scores = np.arange(4, 1, -1)
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)

y_true = np.array([0, 0, 1, 0], dtype='int')
scores = np.arange(4, 1, -1)
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)

y_true = np.array([0, 0, 0, 1], dtype='int')
scores = np.arange(4, 1, -1)
# This one raises an exception
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)

y_true = np.array([0, 1, 0], dtype='int')
scores = np.arange(3, 1, -1)
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)

y_true = np.array([0, 0, 1], dtype='int')
scores = np.arange(3, 1, -1)
# This one raises an exception
prg_curve = prg.create_prg_curve(y_true, scores)
auprg = prg.calc_auprg(prg_curve)
print(auprg)
type(auprg)

Raised exception

Traceback (most recent call last):
  File "test_exception.py", line 33, in <module>
    prg_curve = prg.create_prg_curve(y_true, scores)
  File "/home/miquel/git/uob/conditional_sampler/venv/lib/python3.6/site-packages/prg/prg.py", line 212, in create_prg_curve
    points = _create_crossing_points(points, n_pos, n_neg)
  File "/home/miquel/git/uob/conditional_sampler/venv/lib/python3.6/site-packages/prg/prg.py", line 126, in _create_crossing_points
    j = np.amin(np.where(points['recall_gain'] >= 0)[0])
  File "<__array_function__ internals>", line 6, in amin
  File "/home/miquel/git/uob/conditional_sampler/venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 2831, in amin
    keepdims=keepdims, initial=initial, where=where)
  File "/home/miquel/git/uob/conditional_sampler/venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation minimum which has no identity