What is a Confusion Matrix?
A confusion matrix breaks results into true/false positives and negatives, powering precision, recall, and F1. See how it works with scikit-learn code.
Expected Interview Answer
A confusion matrix is a table that summarizes a classifier's performance by counting true positives, true negatives, false positives, and false negatives, showing exactly where predictions match or miss the actual labels.
Rows typically represent actual classes and columns predicted classes, so the diagonal holds correct predictions and off-diagonal cells hold errors. From these four counts you derive accuracy, precision, recall, F1-score, and specificity. It is far more informative than accuracy alone, especially on imbalanced datasets where a model can score high accuracy while missing the rare but important class. It also distinguishes the two error types — false positives versus false negatives — which often carry very different real-world costs.
- Reveals per-class errors that overall accuracy hides
- Separates false positives from false negatives for cost-aware decisions
- Serves as the basis for precision, recall, F1, and specificity
- Essential for evaluating imbalanced datasets
- Extends naturally to multi-class problems as an N x N grid
AI Mentor Explanation
Think of a DRS review tally: times you correctly reviewed an out, correctly left a not-out, wrongly reviewed a genuine not-out, and wrongly ignored a real out. Those four buckets tell you far more than a single success rate — they show whether you waste reviews or miss dismissals. A confusion matrix is precisely that four-cell scorecard of right and wrong calls for a classifier.
Step-by-Step Explanation
Step 1
Get predictions and labels
Collect the model's predicted classes alongside the true labels for a test set.
Step 2
Define positive class
Decide which class is 'positive' so TP/FP/FN/TN are unambiguous.
Step 3
Tally the four cells
Count true positives, true negatives, false positives, and false negatives.
Step 4
Build the matrix
Place actuals on rows and predictions on columns; correct counts land on the diagonal.
Step 5
Derive metrics
Compute accuracy, precision, recall, specificity, and F1 from the four counts.
Step 6
Interpret errors
Compare false positives and false negatives against their real-world costs.
What Interviewer Expects
- Correct definition of TP, TN, FP, and FN
- How precision, recall, and F1 are computed from the matrix
- Why accuracy alone misleads on imbalanced data
- Understanding the different costs of FP vs FN
- Knowing it generalizes to N x N for multi-class problems
Common Mistakes
- Swapping false positives and false negatives
- Relying only on accuracy from the matrix on imbalanced data
- Confusing precision (of predicted positives) with recall (of actual positives)
- Mislabeling which axis is actual vs predicted
- Ignoring the real-world cost difference between the two error types
Best Answer (HR Friendly)
“A confusion matrix is a small table that shows how often a model's predictions were right or wrong, broken down into the four possible outcomes. It helps you see not just how accurate a model is, but what kinds of mistakes it makes — which matters a lot when some errors are costlier than others.”
Code Example
from sklearn.metrics import confusion_matrix, classification_report
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 0]
cm = confusion_matrix(y_true, y_pred)
print('Confusion matrix:\n', cm) # [[TN, FP], [FN, TP]]
print(classification_report(y_true, y_pred, digits=3))tn, fp, fn, tp = cm.ravel()
accuracy = (tp + tn) / (tp + tn + fp + fn)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print(round(accuracy, 3), round(precision, 3), round(recall, 3), round(f1, 3))Follow-up Questions
- How do you compute precision and recall from a confusion matrix?
- Why can accuracy be misleading on imbalanced datasets?
- When is a false negative worse than a false positive?
- What is the F1-score and when do you prefer it?
- How does a confusion matrix extend to multi-class classification?
MCQ Practice
1. In a binary confusion matrix, what is a false negative?
A false negative is when the model predicts negative but the true label is positive — a missed positive.
2. Precision is defined as?
Precision is the fraction of predicted positives that are truly positive: TP / (TP + FP).
3. Why is a confusion matrix better than accuracy on imbalanced data?
It reveals false negatives and false positives per class, so a model that ignores a rare class is caught despite high accuracy.
Flash Cards
What four counts make up a confusion matrix? — True positives, true negatives, false positives, and false negatives.
What is recall? — TP / (TP + FN) — the fraction of actual positives the model correctly found.
What is precision? — TP / (TP + FP) — the fraction of predicted positives that are correct.
Why not rely on accuracy alone? — On imbalanced data a model can be highly accurate while missing the rare, important class.
What is the F1-score? — The harmonic mean of precision and recall, balancing both in one number.