You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
roc_curve doesn't always arbitrarily set thresholds[0] to max(y_score) + 1 #9790
thresholds : array, shape = [n_thresholds]
Decreasing thresholds on the decision function used to compute
fpr and tpr. *`thresholds[0]` represents no instances being predicted*
*and is arbitrarily set to `max(y_score) + 1`.*
But as you may see below, it actually isn't so. If there are any negative-class sample, which was estimated by classifier with a score more than max (y_score) (comparing to any other positive-class sample), then thresholds[0] will not be set to max(y_score) + 1, and consequently roc_curve will not be started at (0,0) point.
Steps/Code to Reproduce
from sklearn.metrics import roc_curve
actual = np.array([ 0., 0., 1., 1., 1.])
predicted = np.array([ 0.89, 0.61, 0.83, 0.62, 0.90]) # Just change predicted[0] to 0.91 and you'll get right output.
_,_, thresholds = roc_curve(actual, predicted, drop_intermediate=False)
thresholds
Good catch, quickly looking at it, I don't think this is a huge problem. Either the documentation is a bit misleading or we could always add a point to make sure that the (0, 0) point is explicitly returned by roc_curve for consistency reasons. Look at
Thanks for the report. The lengths of those threshold arrays are consistent with the corresponding tpr and fpr arrays.
You consider the output incorrect, I suppose? Seeing as we allow tpr to increase while fpr is constant, I suppose that it is incorrect, because we could have two points with fpr=0, one at the origin, the next with nonzero tpr, in this case.
if tps.size == 0 or fps[0] != 0 or fps[0] != 0:
# Add an extra threshold position if necessary
tps = np.r_[0, tps]
fps = np.r_[0, fps]
thresholds = np.r_[thresholds[0] + 1, thresholds]
In this case we will always start at (0, 0) and as the result the docs will not be misleading. I'm not sure it is a perfect solution from a point of view of data scientist, because actually I'm not one of. I think I will create pull request as soon as I have enough free time :-)
Activity
lesteve commentedon Sep 21, 2017
Good catch, quickly looking at it, I don't think this is a huge problem. Either the documentation is a bit misleading or we could always add a point to make sure that the (0, 0) point is explicitly returned by roc_curve for consistency reasons. Look at
scikit-learn/sklearn/metrics/ranking.py
Lines 530 to 534 in a78b66f
jnothman commentedon Sep 27, 2017
Thanks for the report. The lengths of those threshold arrays are consistent with the corresponding tpr and fpr arrays.
You consider the output incorrect, I suppose? Seeing as we allow tpr to increase while fpr is constant, I suppose that it is incorrect, because we could have two points with fpr=0, one at the origin, the next with nonzero tpr, in this case.
IMHO, a PR to fix is welcome.
alexryndin commentedon Sep 27, 2017
I think a solution would be:
In this case we will always start at (0, 0) and as the result the docs will not be misleading. I'm not sure it is a perfect solution from a point of view of data scientist, because actually I'm not one of. I think I will create pull request as soon as I have enough free time :-)
jnothman commentedon Sep 27, 2017
Let us know if you don't expect to find the time