Draw ROC Curve Based on FPR and TPR in Python – Sklearn Tutorial

By | August 8, 2022

In order to evaluate the performance of a classification model, we have to draw a roc curve based on fpr and tpr. In this tutorial, we will introduce you how to do.

How to draw roc curve in python?

In order to draw a roc curve, we should compute fpr and far. In python, we can use sklearn.metrics.roc_curve() to compute.

Understand sklearn.metrics.roc_curve() with Examples – Sklearn Tutorial

After we have got fpr and tpr, we can drwa roc using python matplotlib.

Here is the full example code:

from matplotlib import pyplot as plt
from sklearn.metrics import roc_curve, auc

plt.style.use('classic')

labels = [1,0,1,0,1,1,0,1,1,1,1]
score = [-0.2,0.1,0.3,0,0.1,0.5,0,0.1,1,0.4,1]

fpr, tpr, thresholds = roc_curve(labels,score, pos_label=1)
print(fpr, tpr, thresholds)
auc_value = auc(fpr,tpr)
print(auc_value)

plt.plot(fpr,tpr, lw=1.5, label="AUC=%.3f)"%auc_value)

plt.xlabel("FPR",fontsize=15)
plt.ylabel("TPR",fontsize=15)

plt.title("ROC")
plt.legend(loc="lower right")
plt.show()

Run this code, we will see:

Draw ROC Curve Based on FPR and TPR in Python - Sklearn Tutorial