For regression, one number such as RMSE told us most of what we needed. Classification is trickier. There are two different ways to be wrong, they rarely cost the same, and the most obvious score can hide a useless model. This chapter builds the small set of measurements that people actually use.
Ninety percent and useless
A clinic screens 200 patients for an illness. Twenty of them have it. Someone proposes this classifier:
Whatever the patient’s data says, answer “healthy”.
It is right about all 180 healthy patients and wrong about the 20 ill ones. That is 180 out of 200, an accuracy of 90%. It has also failed every single person the screening exists to find.
Accuracy counts all mistakes as equal and all examples as equal. When one class is rare, as here, the data is
One email in a hundred is a scam. A filter that lets every email through has what accuracy?
Four kinds of outcome
A real model gives each patient a risk score, and we flag those above a threshold, as in
The table of those four counts is the
One threshold, four outcomes
| Model: flagged | Model: cleared | |
|---|---|---|
| Really ill | 17 Caught true positive | 3 Missed false negative |
| Really healthy | 15 False alarm false positive | 165 Correctly cleared true negative |
- Threshold
- 0.50
- Accuracy
- 91.0%
- Ill patients missed
- 3 of 20
At a threshold of 0.50 the model catches 17 of the 20 ill patients and misses 3. It also raises 15 false alarms. Its accuracy is 91%, one point better than the classifier that did nothing, yet it is doing something completely different and far more useful. Accuracy could not tell them apart. The four counts can.
Precision and recall
Two questions get the useful information out of the matrix. Each one looks at a different slice.
Of the patients we flagged, how many are really ill? That is
Of the patients who are really ill, how many did we flag? That is
The do-nothing classifier has a recall of zero, which exposes it immediately.
A trade, not a free choice
- Precision: of those flagged, how many are ill
- Recall: of those ill, how many are flagged
- F1
- Flagged
- 32
- Precision
- 17 of 32 = 53%
- Recall
- 17 of 20 = 85%
- F1
- 0.65
The two pull in opposite directions. Flag almost everyone and recall is perfect but precision is dreadful. Flag only the most certain cases, say above 0.70, and precision rises to 85% while recall drops to 55%. No threshold maximises both.
When a single number is wanted, the
Every threshold at once
Precision and recall describe a model at one threshold. To judge the model itself, whatever threshold is used later, plot all the thresholds together.
For each threshold, mark two things: across, the share of healthy patients wrongly flagged; up, the share of ill patients caught. Joining the points gives the
The whole model in one curve
- This model, every threshold
- Your threshold
- A model that guesses
- Threshold
- 0.50
- Ill patients caught
- 85%
- Healthy patients flagged
- 8%
- Area under the curve
- 0.958
A perfect model reaches the top left corner: every ill patient caught, no healthy one flagged. A model that guesses lies along the diagonal. The closer a curve bows toward the corner, the better the model separates the two groups.
The area under the curve, the
Choosing the threshold
The ROC curve shows which trade-offs are available. It does not say which to take. That depends on what each mistake costs, and the costs come from the situation, not from the data.
Suppose a false alarm means an extra blood test, cost 1 unit. A missed illness means late treatment, cost 50 units. Compare two thresholds:
| Threshold | Missed | False alarms | Accuracy | Total cost |
|---|---|---|---|---|
| 0.50 | 3 | 15 | 91.0% | 3 × 50 + 15 = 165 |
| 0.35 | 0 | 41 | 79.5% | 0 × 50 + 41 = 41 |
The lower threshold has much worse accuracy and a quarter of the cost.
Put a price on each mistake
| Model: flagged | Model: cleared | |
|---|---|---|
| Really ill | 17 Caught true positive | 3 Missed false negative |
| Really healthy | 15 False alarm false positive | 165 Correctly cleared true negative |
- Threshold
- 0.50
- Misses × 50
- 150
- False alarms × 1
- 15
- Total cost
- 165
The default threshold of 0.50 treats both mistakes as equally bad. That is almost never true. A spam filter should rarely bin a real email, so it wants high precision. A screening test should rarely miss an illness, so it wants high recall. Decide which mistake is worse, by how much, and choose the threshold to match.
A bank's fraud model blocks cards. Blocking a genuine customer is annoying; missing a fraud is expensive but refundable. Customers are leaving because of wrongly blocked cards. What should change?
In code
from sklearn.metrics import (classification_report, confusion_matrix,
precision_score, recall_score, roc_auc_score)
# y_true: 1 for ill, 0 for healthy. scores: the model's risk score for each patient.
y_pred = (scores >= 0.5).astype(int) # apply a threshold yourself
print(confusion_matrix(y_true, y_pred)) # [[165 15] rows: truth (healthy, ill)
# [ 3 17]] columns: prediction
print(precision_score(y_true, y_pred)) # 0.53
print(recall_score(y_true, y_pred)) # 0.85
print(roc_auc_score(y_true, scores)) # 0.958: uses the scores, not a threshold
print(classification_report(y_true, y_pred)) # precision, recall and F1 for both classes
Note that scikit-learn puts the negative class first, so the matrix reads cleared-and-healthy in the top left. Always check which way round a confusion matrix is before reading it.
Key Takeaways
- Accuracy misleads when one class is rare. A model can score well by ignoring the cases that matter.
- The confusion matrix separates the two kinds of mistake: false alarms and misses.
- Precision asks how trustworthy a flag is. Recall asks how many real cases were found. Raising one lowers the other.
- The ROC curve shows every threshold at once, and AUC scores the model without choosing one.
- The right threshold depends on what each mistake costs. 0.50 is rarely it.
- Explain why accuracy misleads when one class is rare.
- Fill in a confusion matrix and work out precision, recall and F1 from it.
- Read an ROC curve and say what AUC measures.
- Choose a threshold from the costs of the two kinds of mistake.
Exercises
Try each one before opening its solution. Getting stuck for a few minutes is where most of the learning happens.
At a threshold of 0.50 the screening model catches 17 of the 20 ill patients and raises 15 false alarms among the 180 healthy ones. Fill in the four cells of the confusion matrix, then work out accuracy, precision, recall and F1.
Hint
True negatives are the healthy patients who were not flagged: 180 minus the false alarms.
Solution
True positives 17, false negatives 3, false positives 15, true negatives .
- Accuracy:
- Precision:
- Recall:
- F1:
About half of the flags are false alarms, and three ill patients in twenty are missed. The accuracy figure hides both facts.
For the same 200 patients, work out accuracy, precision and recall for a model that flags everyone, and for a model that flags no one.
Hint
For each model, fill in the four cells first. One of the measures ends up as 0 divided by 0.
Solution
Flags everyone. All 20 ill patients are caught, so recall is 100%. Of the 200 flags, 20 are right, so precision is 10%. Accuracy is also 10%.
Flags no one. No ill patient is caught, so recall is 0%. Accuracy is . Precision is 0 divided by 0, which has no value: the model never made a flag to be judged.
The two extremes show why one number is never enough. Each of them is perfect on one measure and useless in practice.
A false alarm costs 1 (a follow-up test) and a missed illness costs 20. Threshold A gives 15 false alarms and 3 misses. Threshold B gives 40 false alarms and 1 miss. Which is cheaper? Which has the higher accuracy?
Hint
Multiply each kind of mistake by its cost and add. For accuracy, count the mistakes of both kinds together.
Solution
Cost of A: . Cost of B: . B is cheaper.
Accuracy of A: 18 mistakes in 200, so 91%. Accuracy of B: 41 mistakes in 200, so 79.5%. A is more accurate.
Accuracy counts every mistake the same. When a miss is twenty times worse than a false alarm, the better model is the one that makes more mistakes of the cheap kind.
What does an AUC of 0.5 tell you about a model? What about an AUC of 0.3, and what would you do with such a model?
Hint
AUC is the chance that a random positive case gets a higher score than a random negative one.
Solution
AUC is the chance that the model gives a randomly chosen positive case a higher score than a randomly chosen negative one. At 0.5 the scores carry no information: the model ranks no better than a coin.
An AUC of 0.3 means the model usually ranks positives below negatives. It has found a real pattern and has it backwards. Flip its scores (use one minus the score) and the AUC becomes 0.7. In practice an AUC well below 0.5 nearly always means a bug, such as labels swapped somewhere.
In the figure “A trade, not a free choice”, read precision and recall at a threshold of 0.50 and at 0.70. Then find the lowest threshold at which no ill patient is missed, and read the precision there.
Hint
At 0.50 the model catches 17 of 20 with 15 false alarms.
Solution
At 0.50, recall is 85% and precision about 53%. At 0.70, precision rises to 85% and recall falls to 55%: the flags are more trustworthy and nearly half the ill patients are missed.
To miss nobody the threshold has to drop well below 0.50, and precision falls a long way, because most of the people flagged are now healthy. For a screening test that can be the right choice: a false alarm costs a follow-up test, and a miss costs far more.
Going further
When to prefer a precision-recall curve
With a very rare positive class, the ROC curve can look excellent while precision is poor, because the false alarm rate is a share of a huge number of negatives. Plotting precision against recall for every threshold gives a more honest picture in that case.
Is 70% really 70%?
A risk score of 0.7 is only useful as a probability if about 70% of patients given that score turn out to be ill. A model for which this holds is called calibrated. A model can rank patients well, with a high AUC, and still be badly calibrated.
More than two classes
With several classes the confusion matrix grows to one row and one column per class. Precision and recall are then computed for each class in turn and averaged.
References and further reading
- Google, Machine Learning Crash Course: Classification.
- MLU-Explain, ROC and AUC and Precision and Recall.
- T. Fawcett, “An introduction to ROC analysis”, Pattern Recognition Letters 27 (2006).