Precision, recall and F1
A spam filter judges 100 emails. One confusion table, worked through with real numbers, shows why accuracy flatters and what precision, recall and F1 each measure.
In 30 seconds
When the thing you are looking for is rare, accuracy misleads: a spam filter that flags nothing is still right about every good email.
Precision asks: of everything flagged, how much was really spam? Recall asks: of all the real spam, how much was flagged? Both are built from the same four counts: true and false positives and negatives.
Flagging more raises recall and usually lowers precision. F1 folds the two into one score that is high only when both are.
Precision, recall and F1, step by step
F1 weighs precision and recall equally and ignores true negatives. When one kind of mistake costs more, people report both numbers or use a weighted version (F-beta).
- Step 1: An inbox and a filter. A spam filter gives each of 100 emails a spam score; here they are lined up from most to least suspicious. Twelve are really spam, 88 are fine. The filter flags the top 8 as spam. How good is it? That turns out to need more than one number.
- Step 2: Four kinds of outcome. Every email lands in one of four boxes. Spam that was flagged is a true positive (6); spam that slipped through is a false negative (6). Good mail wrongly flagged is a false positive (2); good mail left alone is a true negative (86). Together they form a confusion table.
- Step 3: Accuracy flatters. Accuracy is the share of all decisions that were right: 92%. But a filter that never flags anything gets 88 of 100 right, because 88% of the mail is fine. When one class is rare, accuracy is mostly a count of the easy cases and hides how the rare class is handled.
- Step 4: Precision. Precision looks only at what was flagged and asks how much of it deserved it. Of the 8 flagged emails, 6 are spam, so precision is 75%. The other 2 are good emails sent to the spam folder. High precision means few false alarms.
- Step 5: Recall. Recall looks only at the real spam and asks how much of it was caught. Of the 12 spam emails, 6 were flagged, so recall is 50%. The other 6 reached the inbox. High recall means few misses. Precision and recall share a top line, TP, but divide it by different totals.
- Step 6: The tradeoff. How many emails get flagged is a dial: the score threshold. Flag only the top 3 and every flag is right, but most spam escapes. Flag the top 48 and all spam is caught, along with 36 good emails. Raising one of precision and recall usually lowers the other; which matters more depends on what each mistake costs.
- Step 7: F1: one number for both. F1 combines precision and recall with a harmonic mean, which is dragged towards the smaller of the two. The balanced setting scores 60%. The strict and loose settings each have one perfect number and one poor one, and both get only 40%. F1 is high only when precision and recall are both high.
Try it yourself
Learn this first
Spotted a mistake on this page?