Evaluating models
About this lesson
A classifier is only as good as the way you measure it. Take ten held-out emails, each scored by a spam filter, and turn the scores into answers with a threshold. Sort them into the four boxes of the confusion matrix, compute accuracy, precision, recall and F1 by hand, then slide the threshold and watch precision and recall trade while accuracy does not move.
Ten emails, ten scores
A spam filter scored ten test emails it never trained on: near 0 means surely fine, near 1 surely spam. We also know the truth: four are spam (rose, front lane) and six are not (mint, back lane).
held-out test emails (never used in training) truth score fine 0.04 fine 0.13 fine 0.22 fine 0.31 spam 0.38 fine 0.42 spam 0.64 fine 0.70 spam 0.82 spam 0.93
Read the lesson step by step
Ten emails, ten scores
A spam filter scored ten test emails it never trained on: near 0 means surely fine, near 1 surely spam. We also know the truth: four are spam (rose, front lane) and six are not (mint, back lane).
held-out test emails (never used in training) truth score fine 0.04 fine 0.13 fine 0.22 fine 0.31 spam 0.38 fine 0.42 spam 0.64 fine 0.70 spam 0.82 spam 0.93Draw a line at 0.5
A score is not an answer yet. Pick a threshold: a score of 0.5 or more means predict spam. Emails right of the line are flagged as spam; emails left of it go to the inbox.
t = 0.5 pred = [s >= t for s in p] -> flagged: 4 emails, inbox: 6 the model gives scores; the threshold turns each score into a yes / no answerFour boxes
Sort each email by truth and prediction. Spam caught: true positive (3). Good mail flagged: false positive (1). Spam missed: false negative (1). Good mail passed: true negative (5). That 2 × 2 table is the confusion matrix.
predicted predicted not spam spam really not spam TN 5 FP 1 really spam FN 1 TP 3 sklearn.metrics.confusion_matrix(y, pred) -> [[5, 1], [1, 3]]Accuracy: right out of all
Accuracy counts every right answer, true positives plus true negatives, out of all emails: (3 + 5) / 10 = 0.80. Easy to read, but it treats both kinds of mistake as the same.
acc = (tp + tn) / n = (3 + 5) / 10 = 0.80 right answers: the TP and TN boxes wrong answers: the FP and FN boxesPrecision: trust a flag
Precision asks: when the filter says spam, how often is it right? TP / (TP + FP) = 3 / (3 + 1) = 0.75. Each false positive, a good email in the spam folder, pulls it down.
prec = tp / (tp + fp) = 3 / (3 + 1) = 0.75 looks only at the right-hand column: the emails the filter flaggedRecall: spam caught
Recall asks: of all the real spam, how much did the filter catch? TP / (TP + FN) = 3 / (3 + 1) = 0.75. Each false negative, spam left in the inbox, pulls it down.
rec = tp / (tp + fn) = 3 / (3 + 1) = 0.75 looks only at the front row: the emails that really are spam (also called sensitivity or true positive rate)F1: one number for both
F1 joins precision and recall with the harmonic mean: 2 × P × R / (P + R) = 0.75. Unlike a plain average, it stays low when either one is low, so a filter cannot hide a bad recall behind a good precision.
f1 = 2*prec*rec/(prec+rec) = 2 * 0.75 * 0.75 / (0.75 + 0.75) = 0.75 precision 1.00 with recall 0.10: plain average 0.55, F1 0.18Raise the threshold
Move the threshold up to 0.75: only very sure emails are flagged. No good email is flagged, so precision is 1.00, but 2 spams slip through and recall drops to 0.50. Accuracy is still 0.80.
t = 0.75 TN 6 FP 0 FN 2 TP 2 t = 0.50 t = 0.75 accuracy 0.80 0.80 precision 0.75 1.00 recall 0.75 0.50 F1 0.75 0.67Lower the threshold
Move it down to 0.35. Every spam is caught: recall 1.00. But 2 good emails land in the spam folder, so precision falls to 0.67. Accuracy stays 0.80, blind to the trade.
t = 0.35 TN 4 FP 2 FN 0 TP 4 t = 0.75 t = 0.50 t = 0.35 precision 1.00 0.75 0.67 recall 0.50 0.75 1.00 lower t: more caught, more false alarmsAccuracy can lie
A filter that never says spam (threshold 1.00, above every score) is still 0.60 accurate here, with recall 0. If only 1 email in 100 were spam, this useless filter would score 0.99 accuracy.
t = 1.00 TN 6 FP 0 FN 4 TP 0 accuracy 0.60 recall 0.00 F1 0.00 precision = 0 / 0: no answer (shown as –) 1 spam in 100, never say spam: accuracy = 99 / 100 = 0.99, recall = 0 rare class: read precision and recallChoose for the cost
Which mistake costs more? Losing a real email hurts, so a spam filter favours precision: here, 0.75. A disease screen favours recall. Pick the threshold on validation data, then measure the test set once.
spam filter a false positive hides real mail -> favour precision, higher t disease screen a false negative misses a patient -> favour recall, lower t every threshold at once: the ROC curve (true positive rate vs false positive rate)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A spam filter flags an ordinary email from your boss as spam. Which box of the confusion matrix is that?
The filter said spam (positive) and it was wrong (false). A false negative is the other mistake: spam that reaches the inbox.
02A filter's results on a test set: TP 8, FP 2, FN 4, TN 86. What are its precision and recall?
Precision = TP / (TP + FP) = 8 / 10 = 0.80. Recall = TP / (TP + FN) = 8 / 12 = 0.67. (Accuracy is (8 + 86) / 100 = 0.94, which hides the four missed spams.)
03You raise the threshold from 0.5 to 0.9. What usually happens?
Only very sure emails are flagged, so fewer good emails are flagged by mistake (precision up), but more spam slips under the line (recall down). Recall can never rise when the threshold goes up.
04Only 1 in 100 transactions is fraud. A model that answers 'not fraud' every time has what accuracy and recall?
It is right on the 99 honest transactions, so accuracy is 0.99, but it catches none of the fraud, so recall is 0. When one class is rare, read precision and recall, not accuracy.
05Why does F1 use the harmonic mean instead of a plain average of precision and recall?
Precision 1.00 with recall 0.10 averages to 0.55 but has an F1 of only 0.18. F1 is high only when both are high.
Glossary
- Test set
- Held-out examples the model never trained on, used once at the end to estimate how well it does on new data.
- Threshold
- The cut-off that turns a score into a class: here, a spam score at or above the threshold means 'predict spam'.
- Confusion matrix
- A table that counts predictions against the truth. For two classes it has four boxes: true negatives, false positives, false negatives and true positives.
- False positive / false negative
- A false positive is a wrong alarm (good mail flagged as spam); a false negative is a miss (spam let through).
- Accuracy
- The share of all predictions that are right: (TP + TN) / all.
- Precision
- Of the examples the model flagged as positive, the share that really are: TP / (TP + FP).
- Recall
- Of the examples that really are positive, the share the model caught: TP / (TP + FN). Also called sensitivity or true positive rate.
- F1 score
- The harmonic mean of precision and recall, 2PR / (P + R); high only when both are high.
Notes and sources
- The ten emails and their scores are invented for the lesson so you can check every number by hand. A real test set holds hundreds or thousands of examples.
- In this small example the same ten emails show every threshold. In real work you choose the threshold on a separate validation set, so the test score stays an honest estimate.
- scikit-learn computes the same numbers: confusion_matrix, accuracy_score, precision_score, recall_score and f1_score in sklearn.metrics. For a threshold above every score, precision_score warns about 0 / 0 and returns 0 unless you set zero_division.
- The 99% case is plain arithmetic: with 1 spam in 100 emails, answering 'not spam' every time is right 99 times. This is why accuracy is a poor guide when one class is rare.
- Manning, Raghavan & Schütze, Introduction to Information Retrieval (2008), 8.3: precision, recall and the F measure
- T. Fawcett, An introduction to ROC analysis, Pattern Recognition Letters 27 (2006)
- Google for Developers, Machine Learning Crash Course: accuracy, recall, precision and related metrics
- Google for Developers, Machine Learning Crash Course: thresholds and the confusion matrix
- scikit-learn User Guide: precision, recall and F-measures; confusion matrix
- Goodfellow, Bengio & Courville, Deep Learning (2016), chapter 5: test sets and validation sets
- Stanford CS229 Machine Learning, lecture notes (A. Ng, T. Ma)
Quiz Unlocks after the last step.