Scores and boxes
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).
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.
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.
Four numbers
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.
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.
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.
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.
Moving 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.
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.
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.
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.
What happens
;; step 1/11 · scores 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
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.