Machine Learning · easy
Explain precision vs recall with an example.
Asked in Data Scientist interviews, in the Machine Learning round.
Short answer
Precision is the share of predicted positives that are correct, recall is the share of actual positives that were caught.
| Precision | Recall | |
|---|---|---|
| Formula | TP / (TP + FP) | TP / (TP + FN) |
| Answers | Of what I flagged, how much was right | Of what was real, how much did I find |
| Hurt by | False positives | False negatives |
| Fraud example | Analysts wasting time on clean accounts | Fraud you never reviewed |
| Optimize it when | Acting on a flag is expensive | Missing a case is expensive |
How to answer it
Definition
Two different questions about the same predictions. Precision asks how much of what you flagged was right; recall asks how much of what was real you caught. The table above compares them.
Formula
precision = TP / (TP + FP) of what I flagged, how much was right
recall = TP / (TP + FN) of what was real, how much did I find
F1 = 2 * P * R / (P + R) harmonic mean, equal weighting
F-beta weights recall by beta; beta = 2 favors recall
Example
1,000 transactions, 50 are fraud. The model flags 40, of which 30 are fraud.
TP = 30 FP = 10 FN = 20
precision = 30 / 40 = 75% three quarters of the alerts are real
recall = 30 / 50 = 60% but two fifths of the fraud was missed
The tradeoff
They move against each other as you slide the threshold. Lower it and you flag more, catching more fraud and raising recall while precision falls. Raise it and precision improves while cases slip through. No threshold maximises both, which is why the question is always which error is more expensive.
Optimize recall missing a case is expensive
cancer screening, fraud, safety alerts
Optimize precision acting on a false alarm is expensive
auto-blocking accounts, spam deletion, an offer with a
real cost attached
Key points
- Precision is about the alerts; recall is about the cases.
- Both ignore true negatives, which is exactly why they suit imbalanced problems.
- The threshold is the dial between them, and it is a business decision.
- F1 gives them equal weight, which the business rarely does. F-beta lets you say which matters.
- Quote them at a stated threshold, or give the whole precision-recall curve.
FAQ
- Which matters more? Whichever error costs more. Ask what happens to a false positive and to a false negative.
- Can both be high? Yes, if the classes separate well. The tradeoff bites when they overlap.
- What about specificity? It is recall for the negative class, and more common in medical contexts.
The 30-second answer
Precision is how much of what I flagged was actually right. Recall is how much of what was really there I managed to catch.
With a fraud example: fifty real cases, the model flags forty, and thirty of those are genuine. Precision is thirty out of forty, seventy-five percent. Recall is thirty out of fifty, sixty percent. So three quarters of the alerts are real, and two fifths of the fraud still got through.
They trade off through the threshold, so the real question is which error costs more. For cancer screening or fraud I would favor recall, because missing a case is worse than an extra review. For something that automatically blocks an account I would favor precision, because a false positive hits a real customer.
In the interview
- What they are checking: correct definitions, and an example where you say which to protect and why.
- Common mistake: swapping them. Anchor on "precision is about what you predicted, recall is about what was true".