Amn Kh - 1 year ago 207

Python Question

I got the following ROC curve like this by the below code:

`fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())`

`plt.plot(fpr["micro"], tpr["micro"], label='ROC curve Fold1 (area = %0.2f)' % roc_auc1["micro"])`

But I do need

Answer Source

ROC curves are a way to compare a set of *continuous-valued* scores to a set of binary-valued labels by applying a varying discrimination threshold to the scores.

If your scores are already binary then there's no need to apply any threshold - just compute the true-positive rate and false-positive rate by directly comparing your scores to your labels, e.g.:

```
tpr = np.mean((y_score == 1) & (y_label == 1))
fpr = np.mean((y_score == 1) & (y_label == 0))
```

If you try to plot the ROC curve for a set of binary scores, you end up with a line containing only three points because there are only three possible pairs of TPR/FPR values:

- If
`threshold > 1`

then you classify every sample as negative, and therefore both your FPR and TPR are both 0. - If
`threshold <= 0`

then you classify every sample as positive, and therefore your TPR is equal to the fraction of 1s in your set of labels, and your FPR is equal to the fraction of 0s in your labels. - If
`0 < threshold <= 1`

then your TPR and FPR values are equivalent to the standard TPR and FPR values calculated above.

That's it.