Precision, Recall and F1-Score Explained
Understand precision, recall and F1-score with clear formulas, a scikit-learn example, and when to prioritise each for imbalanced classification problems.
Expected Interview Answer
Precision is the fraction of positive predictions that are actually correct (TP / (TP + FP)), recall is the fraction of real positives the model actually catches (TP / (TP + FN)), and the F1-score is their harmonic mean, balancing both into one number.
Precision answers 'when the model says positive, how often is it right?', while recall answers 'of all the true positives, how many did we find?'. These two trade off against each other: lowering the decision threshold usually raises recall but hurts precision. The F1-score combines them as 2 * (precision * recall) / (precision + recall), and because it is a harmonic mean it stays low unless both values are reasonably high, which makes it useful for imbalanced datasets where accuracy is misleading.
- Exposes performance that raw accuracy hides on imbalanced data
- Precision controls the cost of false alarms
- Recall controls the cost of missed positives
- F1 gives a single balanced metric for model comparison
- Threshold tuning becomes explicit and measurable
AI Mentor Explanation
A wicketkeeper appealing for catches shows precision: of all the appeals raised, how many were genuinely out. Recall is how many real edges were appealed for at all. Appeal for everything and you catch every edge (high recall) but annoy the umpire with wrong shouts (low precision). F1 rewards a keeper who appeals only for real edges and misses almost none.
Step-by-Step Explanation
Step 1
Build the confusion matrix
Count true positives, false positives, true negatives and false negatives from the model's predictions against the labels.
Step 2
Compute precision
Divide true positives by all predicted positives: TP / (TP + FP). This measures how trustworthy a positive prediction is.
Step 3
Compute recall
Divide true positives by all actual positives: TP / (TP + FN). This measures how much of the positive class the model finds.
Step 4
Combine into F1
Take the harmonic mean: 2 * (precision * recall) / (precision + recall), which punishes a large gap between the two.
Step 5
Pick the metric that matches cost
Favour recall when misses are expensive (disease screening) and precision when false alarms are expensive (spam filtering).
What Interviewer Expects
- Correct formulas for precision, recall and F1
- Understanding of the confusion matrix (TP, FP, TN, FN)
- Why accuracy fails on imbalanced datasets
- The precision-recall trade-off and threshold tuning
- When to prioritise precision vs recall with real examples
Common Mistakes
- Swapping the definitions of precision and recall
- Using accuracy as the only metric on imbalanced data
- Reporting F1 as the arithmetic mean instead of the harmonic mean
- Ignoring the decision threshold's effect on both metrics
- Assuming high precision automatically means high recall
Best Answer (HR Friendly)
“Precision measures how often the model is right when it says yes, and recall measures how many of the real yes-cases it manages to find. The F1-score blends the two into a single balanced number, which is handy when one class is rare and plain accuracy would be misleading.”
Code Example
from sklearn.metrics import precision_score, recall_score, f1_score, classification_report
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0]
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1-score: {f1:.2f}")
# Full per-class breakdown, including macro/weighted averages
print(classification_report(y_true, y_pred))Follow-up Questions
- How does moving the classification threshold affect precision and recall?
- What is the difference between macro, micro and weighted F1 averaging?
- When would you prefer the F-beta score over F1?
- Why is accuracy misleading on a 99:1 imbalanced dataset?
- How do precision and recall relate to the precision-recall curve?
MCQ Practice
1. Which formula defines recall?
Recall is true positives divided by all actual positives, TP / (TP + FN).
2. The F1-score is the ___ of precision and recall.
F1 = 2PR / (P + R), the harmonic mean, which stays low unless both values are high.
3. For cancer screening where missing a case is very costly, you should prioritise:
Missed positives (false negatives) are dangerous, so recall — catching as many true cases as possible — matters most.
Flash Cards
Precision formula — TP / (TP + FP): of everything predicted positive, how much was actually positive.
Recall formula — TP / (TP + FN): of all real positives, how many the model found.
F1-score formula — 2 * (precision * recall) / (precision + recall), the harmonic mean of the two.
When does accuracy mislead? — On imbalanced data — a model predicting only the majority class can score high accuracy yet be useless.