Skip to content
UniCircle
MediumNumericalEvaluation metrics

Precision, recall and F1 for a spam filter

Question ML-105

A spam filter is tested on 1,000 emails, of which 100 are actually spam. The filter flags 80 emails as spam, and 60 of those are truly spam. Compute the accuracy, precision, recall and F1 score. Why is accuracy misleading here?

Show hints

Build the confusion matrix first: TP, FP, FN, TN.

Show the explanation

Worked answer.

Confusion matrix (positive class = spam)

  • TP (spam correctly flagged) = 60
  • FP (legitimate emails wrongly flagged) = 80 − 60 = 20
  • FN (spam missed) = 100 − 60 = 40
  • TN (legitimate correctly passed) = 1,000 − 60 − 20 − 40 = 880

Check: 60 + 20 + 40 + 880 = 1,000 ✓, and actual spam TP + FN = 60 + 40 = 100 ✓.

Metrics

  • Accuracy = (TP + TN) / total = (60 + 880) / 1,000 = 0.94 (94%)
  • Precision = TP / (TP + FP) = 60 / 80 = 0.75 — of the emails flagged, 75% really were spam.
  • Recall = TP / (TP + FN) = 60 / 100 = 0.60 — the filter caught 60% of all spam.
  • F1 = 2 × P × R / (P + R) = 2 × 0.75 × 0.60 / (0.75 + 0.60) = 0.90 / 1.35 = 0.667 (to three decimals).

Why accuracy misleads
Only 10% of emails are spam. A useless model that marks everything as "not spam" would get 900/1,000 = 90% accuracy while catching zero spam (recall = 0). Our 94% hides that the model misses 40% of spam and wrongly flags 20 genuine emails.

Which matters more here?

  • Spam filter: a false positive (an offer letter sent to spam) is usually costlier, so precision is prioritised.
  • Disease screening: missing a patient is worse, so recall is prioritised.
  • Raising the threshold usually increases precision and lowers recall.

Common follow-up: "Why use the harmonic mean for F1 instead of the arithmetic mean?" The harmonic mean is dragged down by the smaller value, so a model cannot score well by being excellent on one metric and poor on the other. Here the arithmetic mean would be 0.675, slightly above the F1 of 0.667; the gap grows when precision and recall differ more.

Original UniCircle question. Last reviewed 28 Oct 2026.