Sub topic 7
AUC: summarizing the curve as one number
This chapter assumes you can build a ROC curve by sweeping a threshold over scores, and that you know true positive rate (TPR) and false positive rate (FPR). It also assumes basic Python and NumPy. Here you will turn the curve into a single number, the area under the curve (AUC), and learn what that number does and does not tell you.
From a curve to a number
A ROC curve has one point per threshold. Comparing two models means comparing two curves, which is hard when you need a ranking or a table of results. AUC reduces the curve to one value: the area between the curve and the FPR axis, over FPR from 0 to 1.
Both axes run from 0 to 1, so the plot is a unit square and the area lies between 0 and 1. The corners of that range have fixed meanings:
| AUC | What the curve looks like | Meaning |
|---|---|---|
| 1.0 | Goes straight up the left edge, then across the top | Some threshold separates the classes perfectly |
| 0.5 | Follows the diagonal | Scores carry no information about the label |
| 0.0 | Goes straight right along the bottom, then up the right edge | Every positive scores below every negative |
| Between 0.5 and 1.0 | Bows above the diagonal | Scores are better than chance at ordering classes |
An AUC below 0.5 means the scores are informative in the wrong direction. If you flip the sign of the scores, the new AUC is exactly 1 minus the old one. Before concluding a model is bad, check that higher scores really mean "more likely positive".
Computing the area by hand
The ROC curve built from a finite sample is a sequence of points joined by straight segments. The area under straight segments is exactly a sum of trapezoids. For consecutive points (x₁, y₁) and (x₂, y₂), the trapezoid has width x₂ − x₁ and average height (y₁ + y₂) / 2.
Take four examples: labels [0, 0, 1, 1] and scores [0.1, 0.4, 0.35, 0.8]. Sweeping the threshold from high to low gives these points:
| Threshold | TPR | FPR |
|---|---|---|
| (start) | 0.0 | 0.0 |
| 0.8 | 0.5 | 0.0 |
| 0.4 | 0.5 | 0.5 |
| 0.35 | 1.0 | 0.5 |
| 0.1 | 1.0 | 1.0 |
The first step goes straight up, so its width is 0 and it adds no area. The step from FPR 0 to 0.5 has height 0.5 and adds 0.25. The next step is vertical and adds nothing. The last step, from FPR 0.5 to 1.0 at height 1.0, adds 0.5. The total is 0.75.
The code below does this sum directly, then checks it against scikit-learn.
import numpy as np
from sklearn.metrics import roc_curve, roc_auc_score
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)
# Trapezoid rule: sum of width * average height over consecutive points.
area = np.sum(np.diff(fpr) * (tpr[1:] + tpr[:-1]) / 2)
print(area) # 0.75
print(roc_auc_score(y_true, y_score)) # 0.75sklearn.metrics.auc(fpr, tpr) performs the same trapezoid sum on any monotonic x-axis, so you can use it on points you built yourself.
What AUC is invariant to
AUC depends only on the order of the scores, not on their values. This has practical consequences.
- Monotonic transforms do not change it. Applying any strictly increasing function to the scores, such as a logarithm or an exponential, leaves the ordering unchanged, so the ROC curve and AUC stay the same.
- Calibration is invisible. A model that outputs 0.9 for every positive and 0.8 for every negative has the same AUC as one that outputs 0.51 and 0.50. Both rank perfectly. Only the first would give trustworthy probabilities.
- No threshold is involved. AUC summarizes performance over all thresholds at once. It cannot tell you how the model performs at the threshold you will actually deploy.
The code below checks these properties.
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
print(roc_auc_score(y_true, y_score)) # 0.75
print(roc_auc_score(y_true, np.exp(5 * y_score))) # 0.75, order unchanged
print(roc_auc_score(y_true, -y_score)) # 0.25, order reversed
print(roc_auc_score([0, 1], [0.5, 0.5])) # 0.5, a complete tieThe last line shows how ties behave. When a positive and a negative share the same score, no threshold can separate them. The curve jumps diagonally from (0, 0) to (1, 1), and that diagonal contributes the area of a chance-level classifier. This is why trapezoids are the right rule: they give tied examples exactly half credit.
Edge cases
- One class only. If
y_truecontains only one class, FPR or TPR is 0/0. scikit-learn'sroc_auc_scoreraises aValueError. This happens in practice when a small cross-validation fold or a rare subgroup contains no positives. - Few distinct scores. A model that outputs only a handful of distinct values, such as a shallow decision tree, produces a curve with few points and large diagonal segments. Its AUC is low-resolution and often lower than the same model's accuracy would suggest.
- Hard labels as scores. Passing 0/1 predictions instead of scores gives a curve with one interior point. The result equals the average of TPR and TNR (balanced accuracy). It throws away the ranking information AUC exists to measure.
- Direction of the score. Some models output a distance or a loss, where lower means more positive. Negate it first, or you will get an AUC below 0.5.
Partial AUC
A full AUC counts every FPR equally, including regions you would never operate in. If a screening system must keep false alarms under 10%, the area at FPR above 0.1 is irrelevant to your decision. roc_auc_score accepts max_fpr to compute a partial AUC over FPR from 0 to that limit. The value is rescaled with the McClish correction, so that 0.5 still means chance and 1.0 still means perfect within that range. Report the max_fpr you used, because partial AUCs are not comparable with full AUCs.
Rules of thumb and their limits
People often quote bands such as "0.7 to 0.8 is acceptable, 0.8 to 0.9 is good". These are conventions, not statistical facts. An AUC of 0.85 may be excellent for predicting a noisy human outcome and poor for reading printed digits. Judge a value against a baseline on the same data, such as a simple model or an existing system.
Two models with the same AUC can behave very differently. Their curves may cross, so that one is better at low FPR and the other at high FPR. The single number hides that, so plot the curves before declaring a winner. Later chapters cover this, along with class imbalance and sampling uncertainty.
Summary of the working model
- The ROC curve plots TPR against FPR as the threshold sweeps.
- AUC is the area under that curve, computed exactly with the trapezoid rule over the curve's points.
- It ranges from 0 to 1, where 0.5 is chance and values below 0.5 mean the score direction is reversed.
- It depends only on the ordering of scores, so it ignores calibration and is unchanged by monotonic transforms.
- It needs both classes present and is meaningful only when given scores, not hard labels.
Exercise
A model gives these scores: negatives at 0.2 and 0.6, positives at 0.6 and 0.9. Without running code, what is the ROC AUC? Then say what happens to the AUC if you replace every score s with s squared.
Show answer
Compare every positive with every negative. Positive 0.6 vs negative 0.2: win. Positive 0.6 vs negative 0.6: tie, worth 0.5. Positive 0.9 vs negative 0.2: win. Positive 0.9 vs negative 0.6: win. That gives 3.5 out of 4 pairs, so AUC = 0.875. Squaring is strictly increasing for non-negative scores, so the order and the ties are preserved and the AUC stays 0.875. This agrees with the trapezoid view: the tie at 0.6 makes one diagonal segment that contributes half credit.
Exercise
You compute AUC for a fraud model and get 0.18. The labels are correct and the code runs without errors. What is the most likely cause, and what is the quick fix?
Show answer
The scores are probably oriented the wrong way, so higher scores correspond to the negative class. This happens when you pass a value such as a distance or an anomaly score where lower means more positive, or when you take the probability column for the wrong class. Negate the scores, or select the probability column for the positive class. The new AUC is 1 − 0.18 = 0.82, and you should then check why the orientation was reversed.