ROC curves and AUC
See how a classifier ranks positives above negatives at every threshold, build a ROC curve by hand from eight examples, and learn when AUC flatters a model.
Lesson 37 of 6612 minIntermediate
You will learn to
- Build a ROC curve from thresholds
- Interpret AUC as a ranking probability
- Know when ROC AUC is too optimistic
Before this lesson
Intuition
Most classifiers do not just say "yes" or "no". They give each row a score, such as a probability, and a threshold turns the score into a decision: above it, predict positive. Move the threshold down and you catch more positives, but you also raise more false alarms. Move it up and the opposite happens.
Think of airport security with a sensitivity dial. Turned low, nobody dangerous gets through but every passenger is searched. Turned high, the queue is quick but threats slip by. Every dial position is a different trade-off. The ROC curve plots all of the trade-offs in one picture, so you can judge the scoring itself, before choosing a threshold.
Each point shows two rates. The true positive rate (the same as recall) is the share of real positives caught. The false positive rate is the share of real negatives wrongly flagged. A good model reaches a high true positive rate while the false positive rate is still small.
Picture it
The curve starts at the bottom-left (threshold so high that nothing is flagged) and ends at the top-right (threshold so low that everything is flagged). The dashed diagonal is a model that guesses at random. The shaded area under the curve is the AUC.
The math
For a threshold , count the confusion matrix and compute:
The ROC curve is the set of points for all thresholds. The area under it has a neat meaning. Let be the model score of row . Then
where is a random positive row and a random negative one (ties count half). AUC = 1 is a perfect ranking, 0.5 is random, and below 0.5 means the ranking is reversed. Because it only uses the order of the scores, AUC does not change if you rescale all scores, and it needs no threshold.
Worked example
Eight examples, four positive (P) and four negative (N), with the scores a model gave. Sorted from the highest score:
| Score | 0.95 | 0.80 | 0.70 | 0.60 | 0.50 | 0.35 | 0.30 | 0.10 |
|---|---|---|---|---|---|---|---|---|
| True class | P | P | N | P | N | P | N | N |
Lower the threshold one example at a time. After each example, record how many positives (out of 4) and negatives (out of 4) have been flagged:
| Threshold just below | Positives flagged | Negatives flagged | TPR | FPR |
|---|---|---|---|---|
| none | 0 | 0 | 0 | 0 |
| 0.95 | 1 | 0 | 0.25 | 0 |
| 0.80 | 2 | 0 | 0.50 | 0 |
| 0.70 | 2 | 1 | 0.50 | 0.25 |
| 0.60 | 3 | 1 | 0.75 | 0.25 |
| 0.50 | 3 | 2 | 0.75 | 0.50 |
| 0.35 | 4 | 2 | 1 | 0.50 |
| 0.30 | 4 | 3 | 1 | 0.75 |
| 0.10 | 4 | 4 | 1 | 1 |
These points are the curve in the figure. Two ways to get the AUC give the same answer:
- Area: the horizontal steps have widths 0.25 at heights 0.5, 0.75, 1 and 1. Area = 0.25 × 0.5 + 0.25 × 0.75 + 0.25 × 1 + 0.25 × 1 = 0.125 + 0.1875 + 0.25 + 0.25 = 0.8125.
- Pairs: there are 4 × 4 = 16 positive-negative pairs. The positive 0.95 beats all 4 negatives, 0.80 beats 4, 0.60 beats 3 (not 0.70) and 0.35 beats 2. That is 13 of 16 pairs, and 13 / 16 = 0.8125.
Now the warning. Imagine 10,000 negatives and 100 positives, and a threshold with FPR = 0.05 and TPR = 0.80. That is 0.05 × 10,000 = 500 false positives and 80 true positives, so precision = 80 / 580 = 0.14. The ROC point looks excellent (0.05, 0.80), yet 86 percent of the alerts are false. FPR is divided by the huge negative class, so it hides the volume of false alarms. The next lesson shows the curve that does not.
How mlflow does it
The leaderboard has an AUC column for classification (two classes: area under the ROC curve; more classes: computed one class against the rest and weighted). The Plots tab of a model candidate shows the ROC curve, computed on the held-out test rows. See the metrics reference and Classification in the docs.
Common mistakes
- Treating AUC as accuracy. An AUC of 0.8 does not mean 80 percent correct. It says nothing about any one threshold. Pick a threshold and report precision and recall there.
- Trusting ROC AUC on rare positives. Many false alarms can hide behind a small false positive rate. Check precision, and look at the precision-recall curve.
- Comparing AUC of models with different test sets. A different mix of rows changes the score. Compare on the same folds.
- Ignoring the region that matters. Two models with the same AUC can differ a lot at the low false positive rates you can afford. Look at the curve, not only the area.
- Expecting calibrated probabilities from a good AUC. AUC only needs the order of scores. A model can rank perfectly and still output poor probabilities (see the calibration lesson).
Try it in mlflow
Open the ROC curve
- Open Runs, open a finished classification run and click a model candidate.
- Open the Plots tab and choose ROC curve.
- Compare the curve with the diagonal. The further it bows toward the top-left corner, the better the ranking.
- Compare the AUC column on the leaderboard with the area under the curve you see.