What are precision, recall, and F1-score, and when do you use each?
Understand precision, recall, and F1-score with formulas, examples, and when to prioritize each metric for imbalanced classification and model evaluation.
Expected Interview Answer
Precision is the fraction of positive predictions that are actually correct (TP / (TP + FP)), recall is the fraction of actual positives the model catches (TP / (TP + FN)), and F1-score is their harmonic mean, balancing the two into a single number.
Use precision when false positives are costly, such as flagging a legitimate email as spam. Use recall when false negatives are costly, such as missing a fraud case or a disease. F1 is preferred over plain accuracy on imbalanced datasets because it ignores the large true-negative count and rewards a model only when both precision and recall are reasonably high.
- Precision controls the false-positive rate
- Recall controls the false-negative rate
- F1 balances both in one metric
- All three stay meaningful under class imbalance
- They expose weaknesses that accuracy hides
AI Mentor Explanation
A slip fielder who only dives for balls he is certain to hold has high precision — few dropped catches — but low recall because he lets edgy chances fly past. A keeper who lunges at everything catches more (high recall) but spills some (lower precision). F1 rewards the fielder who both attempts and completes catches reliably across the innings.
Step-by-Step Explanation
Step 1
Build the confusion matrix
Count true positives, false positives, true negatives, and false negatives from your predictions against the labels.
Step 2
Compute precision
Divide true positives by all predicted positives: TP / (TP + FP). This answers how trustworthy a positive prediction is.
Step 3
Compute recall
Divide true positives by all actual positives: TP / (TP + FN). This answers how many real positives you found.
Step 4
Combine into F1
Take the harmonic mean: F1 = 2 * (precision * recall) / (precision + recall), which punishes a large gap between the two.
Step 5
Pick by cost
Choose the operating point by which error hurts more — favor precision when false alarms are costly, recall when misses are costly.
What Interviewer Expects
- Correct formulas for all three metrics
- Clear reasoning about false-positive vs false-negative cost
- Why F1 uses the harmonic, not arithmetic, mean
- Awareness that accuracy misleads on imbalanced data
- A concrete example of when to prioritize each
Common Mistakes
- Swapping the precision and recall formulas
- Averaging precision and recall arithmetically instead of harmonically
- Reporting only accuracy on an imbalanced dataset
- Claiming one threshold is universally best regardless of cost
- Confusing recall with specificity (true-negative rate)
Best Answer (HR Friendly)
“Precision asks: of the things the model flagged, how many were right? Recall asks: of the things it should have flagged, how many did it catch? F1-score blends the two into one number, and you lean toward whichever matters more for the problem.”
Code Example
from sklearn.metrics import precision_score, recall_score, f1_score
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0]
print('Precision:', precision_score(y_true, y_pred))
print('Recall: ', recall_score(y_true, y_pred))
print('F1-score: ', f1_score(y_true, y_pred))Follow-up Questions
- Why is F1 the harmonic mean rather than the arithmetic mean?
- How do macro, micro, and weighted F1 differ for multiclass problems?
- What is the precision-recall tradeoff and how does the threshold control it?
- When would you prefer the F-beta score over F1?
- How does class imbalance affect each of these metrics?
MCQ Practice
1. Which metric penalizes false negatives the most?
Recall = TP / (TP + FN), so more false negatives directly lower recall, making it the metric that penalizes missed positives.
2. F1-score is the harmonic mean of which two metrics?
F1 = 2 * (precision * recall) / (precision + recall), combining precision and recall while punishing imbalance between them.
3. For a cancer screening test, missing a sick patient is far worse than a false alarm. Which should you prioritize?
Missing a sick patient is a false negative, so recall — which measures how many true positives are caught — should be prioritized.
Flash Cards
Precision formula — TP / (TP + FP) — of all predicted positives, how many were correct.
Recall formula — TP / (TP + FN) — of all actual positives, how many were found.
F1-score — Harmonic mean of precision and recall: 2PR / (P + R); high only when both are high.
When to favor recall — When false negatives are costly — fraud, disease screening, safety alerts.
When to favor precision — When false positives are costly — spam filters, marketing spend, arrests.