Skip to content

roc_curve doesn't always arbitrarily set thresholds[0] to max(y_score) + 1 #9790

Closed

Description

@alexryndin

Description

The roc_curve Docstring says following

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

Expected Results

array([ 1.89,  0.89,  0.9 ,  0.83,  0.62,  0.61]) # Note length of array

Actual Results

array([ 0.9 ,  0.89,  0.83,  0.62,  0.61]) # Note length of array

Versions

Linux-4.10.0-33-generic-x86_64-with-debian-stretch-sid
Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
NumPy 1.12.1
SciPy 0.19.0
Scikit-Learn 0.18.1

Activity

  1. lesteve commented on Sep 21, 2017

    @lesteve
    Member

    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

    if tps.size == 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]
    for the code that decides whether to add the (0, 0) point.

  2. jnothman commented on Sep 27, 2017

    @jnothman
    Member

    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.

  3. alexryndin commented on Sep 27, 2017

    @alexryndin
    Author

    I think a solution would be:

     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 :-)

  4. added
    Easy
    on Sep 27, 2017
  5. jnothman commented on Sep 27, 2017

    @jnothman
    Member

    Let us know if you don't expect to find the time

  6. added
    good first issue
    and removed
    good first issue
    on Nov 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEasyhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions