Confusion Matrix, Precision, Recall & F1-Score
Interpret classification confusion matrices and balance precision vs recall with F1.
In plain words
The confusion matrix counts the model's right and wrong calls, and precision and recall tell you how trustworthy its positive calls are and how many real positives it catches.
Think of it like…
A metal detector at the beach: precision is the share of beeps that turn out to be treasure, recall is the share of buried treasure it finds; turn the sensitivity up and you find more treasure but dig up more bottle caps.
Where you’ve seen it
A churn model scores 1,000 customers: TP = 80, FP = 40, FN = 20, TN = 860. Accuracy = 940/1,000 = 94%. Precision = 80/120 = 0.667 and recall = 80/100 = 0.80, so F1 = 2 × 0.667 × 0.80 / 1.467 = 0.727. Specificity = 860/900 = 0.956, FPR = 0.044 and balanced accuracy = (0.80 + 0.956)/2 = 0.878. F2 (recall-weighted) = 5 × 0.667 × 0.80 / (4 × 0.667 + 0.80) = 0.769. MCC = (80 × 860 − 40 × 20)/√(120 × 100 × 900 × 880) = 68,000/97,488 = 0.698. A "never churn" model scores 90% accuracy with 0 recall, which accuracy alone can't detect. The retention team can contact only 120 customers a month and each contact costs $20, so precision (67%) directly sets cost per saved customer: 120 × $20 / 80 = $30. A multiclass ticket-routing model reports macro-F1 0.71 and micro-F1 0.84; the gap shows small classes perform much worse than the big ones, which weighted averaging would hide.
Words you’ll hear
- Precision / recall
- Share of flagged cases that are correct, and share of real positives found.
- F1 / Fβ
- Harmonic mean of precision and recall, with β weighting recall.
- MCC
- A balanced correlation between predictions and truth, robust to imbalance.
- Macro vs micro averaging
- Average per-class scores equally, or pool all decisions.
Visual diagram
Theory & deep dive
Reading the matrix and the core metrics
Rows are actual classes and columns predicted (scikit-learn's confusion_matrix convention: rows true, columns predicted). TP are positives caught, FN positives missed, FP false alarms and TN correct rejections. Recall answers "of all real churners, how many did we find?" (80/100 = 0.80). Precision answers "of the customers we flagged, how many really churn?" (80/120 = 0.667). They trade off through the decision threshold: lowering it raises recall and lowers precision. Specificity (TN/(TN + FP) = 0.956) is the recall of the negative class, and the false-positive rate is 1 − specificity (0.044).
F1 = 2PR/(P + R) combines precision and recall using the harmonic mean, which is dominated by the lower value (so 0.667 and 0.80 give 0.727, not 0.733). Fβ = (1 + β²)PR/(β²P + R) shifts the emphasis: F2 treats recall as twice as important (0.769) and F0.5 favours precision. F1 ignores true negatives, so it doesn't reflect specificity. Balanced accuracy averages recall and specificity (0.878) and MCC uses all four cells: (TP·TN − FP·FN)/√((TP + FP)(TP + FN)(TN + FP)(TN + FN)) = 0.698; MCC is a robust single number for imbalanced data (+1 perfect, 0 random, −1 inverted).
P = 80/120 = 0.667; R = 80/100 = 0.80; F1 = 2 × 0.667 × 0.80 / (0.667 + 0.80) = 0.727. Accuracy = (80 + 860)/1,000 = 0.94. MCC = (68,800 − 800)/√(120 × 100 × 900 × 880) = 68,000/97,488 = 0.698.
Averaging, thresholds and choosing what to optimise
For multiclass problems, compute precision and recall per class from the K × K matrix, then average: macro (unweighted mean over classes: treats small and large classes equally, exposing weak classes), micro (pool all TP, FP, FN across classes: equals accuracy for single-label problems and is dominated by large classes) or weighted (macro weighted by class support). In the ticket-routing example, macro-F1 0.71 against micro-F1 0.84 reveals that small categories perform much worse. For multilabel tasks use per-label metrics, and report per-class numbers, not only averages.
Metrics depend on the threshold: scikit-learn's default 0.5 is rarely right. Choose the operating point from costs or constraints: maximise F1, F2 or expected profit, fix precision ≥ 0.9 and maximise recall, or fit the capacity of a review team (flag the top 120). Always compare with baselines (majority class, simple rules) and report the prevalence, because precision depends on it: the same classifier has lower precision on a population with fewer positives. Visualise with ConfusionMatrixDisplay (normalised by row to show recall per class), and use classification_report to see per-class precision, recall, F1 and support. Finally, translate to business impact: 120 contacts at $20 with 80 saved customers means $30 per saved customer.
Three classes with F1 0.95 (n = 800), 0.60 (n = 150) and 0.40 (n = 50). Macro-F1 = (0.95 + 0.60 + 0.40)/3 = 0.65; the support-weighted average is (0.95×800 + 0.60×150 + 0.40×50)/1,000 = 0.87. The macro number reveals weak minority classes.
How it works, step by step
Step 1 of 41. Compute the confusion matrix
Fix a threshold and count TP, FP, FN and TN on held-out data.
- Report
- prevalence
When to use it & trade-offs
What it is
A confusion matrix counts classification outcomes at a chosen threshold: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). From it come the standard metrics: accuracy = (TP + TN)/N; precision = TP/(TP + FP) (of the cases flagged, how many were right); recall (sensitivity, TPR) = TP/(TP + FN) (of the real positives, how many were found); specificity = TN/(TN + FP); and F1 = 2PR/(P + R), the harmonic mean of precision and recall (Fβ weights recall β times as much). Additional single-number summaries include balanced accuracy and the Matthews correlation coefficient (MCC), which stay informative when classes are imbalanced. For multiclass problems, the matrix is K × K and metrics are averaged per class (macro), globally (micro) or weighted by support.
Why it matters: Accuracy hides the type of mistake, and the two kinds of mistake usually cost very different amounts. Precision and recall make the trade-off explicit: a spam filter wants high precision (don't lose real mail), a cancer screen wants high recall (don't miss cases). Reading the confusion matrix and choosing the metric that matches the cost structure is the first step in evaluating any classifier and in choosing a threshold.
When to reach for it
- Evaluating any classifier at a deployed threshold
- Setting operating points by cost or capacity
- Diagnosing which classes a model confuses
When to avoid or be careful
- Reporting accuracy alone on imbalanced data
- Using the default 0.5 threshold without checking
- Using micro-averages that hide weak classes
Classification summaries
Strengths: Directly interpretable trade-off.
Watch out: Depend on the threshold.
Strengths: Simple; Fβ adjusts emphasis.
Watch out: Ignores true negatives; hides the trade-off.
Strengths: Uses all cells; stable with skewed classes.
Watch out: Less intuitive to explain.
Interview signals & numbers to know
When this topic comes up
- “Accuracy is 94% but it misses every churner”
- Precision versus recall
- F1 and Fβ
- Macro versus micro averaging
- Matthews correlation
Benchmarks & rules of thumb
- 0.667 / 0.80 / 0.727
- precision, recall and F1 in the example
- 0.698
- MCC of the example classifier
- $30
- cost per saved customer: 120 × $20 / 80
Common mistakes & follow-ups
Common interview mistakes
- Comparing precision across populations with different prevalence.
- Optimising F1 when recall is far more valuable than precision.
- Forgetting that confusion matrices depend on the threshold.
Likely interviewer follow-ups
- What's the difference between precision and recall?
- When would you use F2 instead of F1?
- How do macro and micro averages differ?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Confusion Matrix, Precision, Recall & F1-Score with data science interview Q&A and hands-on practice.