00.250.500.751TRUE NEGATIVE1pred = [s >= t for s in p]2acc = (tp + tn) / n3prec = tp / (tp + fp)4rec = tp / (tp + fn)5f1 = 2*prec*rec/(prec+rec)FALSE POSITIVEFALSE NEGATIVEpredicted: not spampredicted: spamTRUE POSITIVEthresholdaccuracyprecisionrecallF1
Ruler: spam score from 0 (left) to 1 (right); gold post: the thresholdRose envelopes: really spam · mint envelopes: really not spamTrays: rows are the truth, columns the prediction (the confusion matrix)Ten held-out emails sit on a ruler at the spam score a filter gave them. A threshold turns each score into a prediction, and each email drops into one of four trays: the confusion matrix. At threshold 0.5: accuracy 0.80, precision 0.75, recall 0.75, F1 0.75. Raising the threshold to 0.75 lifts precision to 1.00 and drops recall to 0.50; lowering it to 0.35 does the opposite. Accuracy stays 0.80 throughout, and a filter that never says spam still scores 0.60. 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.

Scores and boxes

  1. 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).

Four numbers

Moving the threshold

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.