Topic 1 of 6 · 4 min read
Precision, recall and the confusion matrix
Goal: Compute precision and recall from a confusion matrix and say which type of error each one counts.
Precision asks "when the model says yes, how often is it right?" and recall asks "of all the real yeses, how many did the model find?" Both come from four counts in the confusion matrix, and each one punishes a different kind of mistake.
Check yourself · 1 of 3
A model flags 80 emails as spam, and 60 of them really are spam. What is its precision?
Why it matters
A fraud model that flags 1 in 10 normal payments annoys customers. One that misses real fraud costs money. Accuracy hides which of these is happening. Precision and recall do not.
The four cells
For a binary classifier, every prediction lands in one cell:
- True positive (TP): predicted yes, truly yes.
- False positive (FP): predicted yes, truly no. A false alarm.
- False negative (FN): predicted no, truly yes. A miss.
- True negative (TN): predicted no, truly no.
Then:
precision = tp / (tp + fp) # of the flagged items, how many were real
recall = tp / (tp + fn) # of the real items, how many were flagged
Precision looks down the "predicted yes" column. Recall looks along the "actually yes" row. Neither uses TN, which is why they stay honest when negatives dominate.
The trade-off
Most classifiers output a score, and you turn it into yes or no with a threshold. Lower the threshold and you flag more items: recall goes up, precision usually goes down. Raise it and the reverse happens. There is no setting that is best for every use; the right point depends on which error costs more.
Before and after
A spam filter report says "99% accurate" on a mailbox where 1% of mail is spam. A model that never flags anything scores that too.
# 10,000 emails, 100 are spam
tp, fn = 60, 40 # caught 60 of 100 spam
fp, tn = 20, 9880
precision = 60 / (60 + 20) # 0.75
recall = 60 / (60 + 40) # 0.60
The better report says: "Of every 4 emails we flag, 3 are spam; we catch 6 of every 10 spam emails." That tells you what users will see, and accuracy cannot.
What goes wrong
- Quoting one number alone. A model can reach recall 1.0 by flagging everything, with terrible precision. Always give both.
- Wrong denominator. Dividing TP by all predictions, or by all rows, gives a number that is neither metric.
- Ignoring zero cases. If the model predicts no positives, precision is 0/0. Libraries return 0 or warn; treat it as "undefined", not "perfect".
- Multi-class averaging. With several classes, macro, micro and weighted averages answer different questions. Say which one you report.
Examples
Reading a confusion matrix
A tumour screening model on 1,000 scans gives TP 45, FP 15, FN 5, TN 935.
Precision is 45 / 60 = 0.75: three of every four flagged scans are real. Recall is 45 / 50 = 0.90: nine of every ten real tumours are flagged. The 5 misses are the number a doctor cares about most, and recall is the metric that counts them.
Same model, two thresholds
At threshold 0.5 a support-ticket classifier flags 80 tickets as urgent, 60 correctly, out of 100 truly urgent. Precision 0.75, recall 0.60. At threshold 0.3 it flags 140, 85 correctly. Precision falls to 0.61, recall rises to 0.85. Nothing about the model changed; only the cut-off did.
Key points
- Precision is TP divided by TP plus FP: it counts false alarms among the items the model flagged.
- Recall is TP divided by TP plus FN: it counts misses among the items that were really positive.
- Neither metric uses true negatives, so they stay meaningful when negatives are the vast majority.
- Moving the decision threshold trades one against the other; it does not improve the model itself.
- Report precision and recall together, and say how multi-class scores were averaged.
Common mistakes
- Reporting accuracy for a rare-positive problem and calling the model good.
- Quoting recall alone, which a model that flags everything satisfies.
- Computing precision with all rows in the denominator instead of only predicted positives.
- Treating 0/0 precision on an empty prediction set as a perfect score.
Check yourself · 2 of 3
Which error does recall count?
Check yourself · 3 of 3
You lower a classifier's decision threshold from 0.5 to 0.3. What usually happens?
Practice items, not counted towards your mastery.
Start here
- Google ML Crash Course: classification (opens in a new tab)CourseFree
Accuracy, precision, recall and ROC/AUC explained with worked confusion-matrix numbers.
- scikit-learn: metrics and scoring (opens in a new tab)DocsFree
The reference for every metric named in this skill, with the exact function and its edge cases.
- scikit-learn: probability calibration (opens in a new tab)DocsFree
Reliability curves, Brier score and the sigmoid and isotonic recalibration methods, with plots.