Imagine a doctor announces a new test for a rare disease that is "99% accurate." Sounds fantastic — until you learn the disease affects only 1 in 100 people. A "test" that never bothers looking and just prints "healthy" for everyone would also score 99% accuracy, while catching precisely nobody who is actually sick. Accuracy alone told us nothing useful.
This is the trap of an imbalanced problem — one where the classes aren't roughly equal in size. To see past it, we need sharper tools built from the confusion matrix: for every prediction, was it a
From those four counts come two metrics that ask two different questions:
Let's make this concrete with a spam filter that checks 100 emails, of which 20 really are spam and 80 are not. Suppose the filter flags 25 emails as spam, and of those, 18 really are spam (so 7 genuine emails get wrongly caught) — and it lets 2 real spam emails slip through to the inbox:
| Actually spam | Actually not spam | |
|---|---|---|
| Predicted spam | TP = 18 | FP = 7 |
| Predicted not spam | FN = 2 | TN = 73 |
Here
Back to the disease that affects 1 in 100 people. Out of 1,000 patients, 10 truly have the disease. The "always say healthy" test produces this confusion matrix:
| Actually sick | Actually healthy | |
|---|---|---|
| Predicted sick | TP = 0 | FP = 0 |
| Predicted healthy | FN = 10 | TN = 990 |
Accuracy
A classifier usually doesn't just output "positive" or "negative" — it outputs a
score between 0 and 1, and we pick a threshold above which we call it "positive." Each
example below has a true label and a model score; slide the decision threshold and watch the
confusion-matrix counts and all three metrics update live. At the default threshold of 0.5, this
toy dataset gives TP = 5, FP = 3, FN = 2, TN = 4 — precision
Precision and recall matter in different amounts depending on which kind of mistake is more expensive:
When you need one single number that balances both, use the F1 score, the
harmonic mean of precision and recall,
Whenever one class vastly outnumbers the other, accuracy becomes almost meaningless as a headline number. If 99% of patients are healthy, a model can hit 99% accuracy by memorising the single word "healthy" and never looking at the data at all — its recall for the disease is a flat zero. The same trap appears everywhere: fraud detection (almost all transactions are legitimate), rare-event alerts, even spam filters if spam is much rarer than real mail. The fix is always the same — check precision and recall (or F1) for the class you actually care about, not just overall accuracy, especially whenever one class is much rarer than the other.
Long before anyone wrote "confusion matrix," Aesop told the story of a shepherd boy who raised a false alarm about a wolf again and again for fun. His "wolf detector" had perfect recall in one sense — he never once failed to shout when there really was danger — but his precision was terrible, because almost every alarm was a false positive. By the time a real wolf showed up, the villagers had learned to ignore him, and it cost the flock dearly. The moral is a precision/recall lesson in disguise: a detector that cries "positive!" constantly will eventually catch every real case, but if nobody trusts its alarms any more, high recall stops being worth anything.
This isn't just a classroom exercise — production spam filters, fraud-detection systems, and medical-screening tools all explicitly choose where to sit on the precision/recall trade-off by setting their decision threshold. A hospital screening tool for a dangerous disease is deliberately tuned toward high recall, accepting more false alarms (which just mean a follow-up test) in exchange for almost never missing a real case. An email provider tunes the opposite way, accepting that a little spam gets through so that your important mail practically never vanishes. Neither threshold is "more correct" than the other — they're different answers to the question "which mistake costs us more here?"