Sub topic 7
AUC: summarizing the curve as one number
This chapter assumes you can build a ROC curve by sweeping a threshold, and that you know TPR and FPR. You should also be comfortable with basic NumPy.
From a curve to a number
A ROC curve shows how the true positive rate (TPR) and false positive rate (FPR) move together as the threshold changes. Comparing two curves by eye is hard when they differ in shape. The area under the ROC curve (AUC) gives one number per model.
The ROC plot sits inside a unit square, with FPR on the x-axis and TPR on the y-axis. AUC is the area under the curve within that square, so it always falls between 0 and 1.
Reference values
| AUC | What it means |
|---|---|
| 1.0 | A threshold exists that separates every positive from every negative. |
| 0.5 | The curve follows the diagonal. The scores carry no information, like random guessing. |
| Below 0.5 | The scores point the wrong way. Flipping them would give 1 minus the AUC. |
| 0.5 to 1.0 | The scores are partly informative. Higher is better. |
An AUC of 0.5 does not mean 50% accuracy. AUC does not depend on any single threshold, so it says nothing about accuracy at a particular cutoff.
Computing the area
The ROC curve is a list of points (FPR, TPR), ordered from (0, 0) to (1, 1). The standard way to get the area is the trapezoidal rule. Between two neighboring points, the area is the width times the average of the two heights:
area of one strip = (FPR₂ − FPR₁) × (TPR₁ + TPR₂) / 2
Summing the strips gives the AUC.
For the four-example set, with labels [0, 0, 1, 1] and scores [0.1, 0.4, 0.35, 0.8], the curve passes through these points:
| Threshold | FPR | TPR |
|---|---|---|
| (start) | 0.0 | 0.0 |
| 0.8 | 0.0 | 0.5 |
| 0.4 | 0.5 | 0.5 |
| 0.35 | 0.5 | 1.0 |
| 0.1 | 1.0 | 1.0 |
Vertical moves have zero width, so they add no area. The horizontal move from FPR 0 to 0.5 at height 0.5 adds 0.25. The move from 0.5 to 1.0 at height 1.0 adds 0.5. The total is 0.75.
import numpy as np
from sklearn.metrics import auc, roc_curve
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
fpr, tpr, thresholds = roc_curve(y_true, y_score)
# Trapezoidal rule written out by hand
manual = np.sum(np.diff(fpr) * (tpr[1:] + tpr[:-1]) / 2)
print(manual) # 0.75
print(auc(fpr, tpr)) # 0.75The function sklearn.metrics.auc is a general trapezoid integrator. It works on any monotonic x-axis, so you can reuse it for other curves. It does not know the points came from a ROC curve.
What AUC hides
AUC compresses the whole curve, so different curves can share a value. One model may be strong at low FPR and weak at high FPR, while another shows the opposite pattern. If your application only tolerates a low FPR, the left part of the curve matters most, and the full area can mislead you.
Keep these points in mind:
- AUC does not tell you which threshold to use. Choosing one is a separate step.
- AUC ignores calibration. Scores can be rescaled by any increasing function, and the AUC stays the same.
- Two models with equal AUC can behave very differently at the operating point you care about.
The next chapter explains why this area also equals the probability that a random positive is scored above a random negative.
Exercise
A model's ROC curve passes through (0, 0), (0.2, 0.6), (1, 1). Use the trapezoidal rule to compute the AUC.
Show answer
Strip 1: width 0.2, average height (0 + 0.6) / 2 = 0.3, area 0.06. Strip 2: width 0.8, average height (0.6 + 1) / 2 = 0.8, area 0.64. Total AUC = 0.06 + 0.64 = 0.70.
Exercise
A classifier has AUC 0.30 on a validation set. What does this suggest, and what is a simple fix?
Show answer
The scores rank negatives above positives more often than not, so they point the wrong way. Flipping the sign of the scores, or the label convention, would give an AUC of 0.70. First check for a label or score-direction bug.