Compute AUC Metric Based on FPR and TPR in Python

By | August 8, 2022

AUC is an important metric to evaluate the performance of a classification model. In this tutorial, we will introduce you how to compute its value.

What is AUC?

AUC is also called Area Under the Curve. It is the area under the ROC curve. For example:

understand what is AUC metric

It represents:

\(AUC = P(P_{positive sample}> P_{negative sample})\)

How to compute AUC?

In order to compute it, we should know fpr and tpr. We can compute them by sklearn.metrics.roc_curve().

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

Then,we can use sklearn.metrics.auc(fpr, tpr) to compute AUC.

For example:

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)

Run this code, we will find auc_value = 0.833

Leave a Reply