some bug of scikit learn auc function?
<p>Here is the code and output, I think from the output, it mean when fpr is 0, tpr is 0, this is correct as the prediction results marks everything to be 0.</p>

<p>But the output also said, when fpr is 1, tpr is also 1. I think it is not correct, since the predictor never predict something to be positive (label to be <code>1</code>), so how could the fpr (= # of correct prediction of 1/total # of 1) and tpr (= # of prediction of 1 / total # of 0) both to be 1?</p>

<pre><code>import numpy as np
from sklearn import metrics
y = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
pred = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
fpr, tpr, thresholds = metrics.roc_curve(y, pred)
print fpr
print tpr
print thresholds
print metrics.auc(fpr, tpr)
</code></pre>

<p><strong>Output</strong>,</p>

<pre><code>[ 0.  1.]
[ 0.  1.]
[1 0]
0.5
</code></pre>

<p>These two illustrations would give you a better understanding of how the <em>FPR</em> and <em>TPR</em> get computed.</p>

<p><strong><em>Case-1:</em></strong></p>

<pre><code>y    = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
pred = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
#                                          -^- see the change here 
</code></pre>

<p><em>True Positive = 0 <br> 
False Positive = 0 <br>
True Negative  = 9 <br>
False Negative = 1 <br></em> </p>

<p><em>True Positive Ratio, (tpr) = True Positive/(True Positive + False Negative)</em><br>
Therefore, <em>tpr = 0/(0+1) = 0.</em></p>

<p><em>False Positive Ratio, (fpr) = False Positive/(False Positive + True Negative)</em><br>
Therefore, <em>fpr = 0/(0+9) = 0.</em></p>

<pre><code>#Output:

fpr â [ 0.  1.]
tpr â [ 0.  1.]
</code></pre>

<p><strong><em>Case-2:</em></strong></p>

<pre><code>y    = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
pred = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
#                                          -^- see the change here 
</code></pre>

<p><em>True Positive = 1 <br> 
False Positive = 0 <br>
True Negative  = 9 <br>
False Negative = 0 <br></em> </p>

<p><em>True Positive Ratio, (tpr) = True Positive/(True Positive + False Negative)</em><br>
Therefore, <em>tpr = 1/(1+0) = 1.</em></p>

<p><em>False Positive Ratio, (fpr) = False Positive/(False Positive + True Negative)</em><br>
Therefore, <em>fpr = 0/(0+9) = 0.</em></p>

<pre><code>#Output:

fpr â [ 0.  1.]
tpr â [ 1.  1.]
</code></pre>

<hr>

<p><strong>Note:</strong> </p>

<p>According to the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html" rel="nofollow"><code>roc_curve documentation</code></a>, it is clearly stated that thresholds[0] represents no instances being predicted and is arbitrarily set to <code>max(pred)+1</code>.
[Here, 2 for binary classification task]</p>

<p>This becomes valid when the <code>fpr</code> and <code>tpr</code> when calculated becomes a fraction and cannot be quantified to 0 or 1. Hence, the <code>threshold</code> varies from 0, 1, 2.
For eg, when the last 2 values of <code>pred</code> array become 1, you get 3 values for threshold as <code>fpr</code> and <code>tpr</code> become fractional values.</p>

<p>But in our case, both <code>fpr</code> and <code>tpr</code> had been either of 0 or 1 and hence there wasn't any need for a third value of the <code>threshold</code>.</p>

<p>Also, the array elements in both <code>fpr</code> and <code>tpr</code> form an increasing sequence, i.e vary from  0 â1 and must satisfy a shape >= 2. Therefore, it is mandatory to have both 0's and 1's in the array as the starting and terminal values.</p>

<p>Incase of fractional values of <code>fpr</code> and <code>tpr</code>, the middle column would contain these values enclosed by 0 and 1 on either side of the array.</p>

python machine-learning scikit-learn roc auc