Amn Kh - 1 year ago 357
Python Question

# How to plot ROC curve in python?

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 curve. What I am achieving now, as seen in the figure, is a two-broken lines!!!

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download