What is a confusion matrix and what does it tell you?
Learn what a confusion matrix is, how TP, FP, TN, and FN work, and how it drives precision, recall, and F1 for evaluating classification models.
Expected Interview Answer
A confusion matrix is a table that compares a classifier's predictions against the true labels, breaking results into true positives, false positives, true negatives, and false negatives so you can see exactly what kinds of errors the model makes.
For binary classification it is a 2x2 grid: rows are actual classes, columns are predicted classes (or vice versa). From these four counts you derive accuracy, precision, recall, specificity, and F1. It is far more informative than a single accuracy number because it reveals whether errors are concentrated in false alarms or missed cases, and it extends to N x N grids for multiclass problems.
- Shows the specific type of each error, not just a total
- Is the basis for precision, recall, specificity, and F1
- Exposes class imbalance that accuracy hides
- Extends naturally to multiclass as an N x N grid
- Guides threshold tuning and cost-based decisions
AI Mentor Explanation
Think of a third-umpire review log that tallies every LBW decision four ways: correctly given out, correctly not out, wrongly given out, and wrongly ruled not out. Reading those four buckets tells you whether the umpire errs toward the bowler or the batter. A confusion matrix does the same for a model's calls across a whole innings of predictions.
Step-by-Step Explanation
Step 1
Collect predictions and labels
Run the model on a labeled test set and pair each prediction with its true class.
Step 2
Define positive and negative
Decide which class is 'positive' — usually the rarer or more important class you care about detecting.
Step 3
Tally the four cells
Count TP, FP, TN, and FN by comparing each prediction to its actual label.
Step 4
Arrange the grid
Place actual classes on the rows and predicted classes on the columns (or vice versa) as a 2x2 or N x N table.
Step 5
Derive metrics
Compute accuracy, precision, recall, specificity, and F1 from the four counts to interpret model behavior.
What Interviewer Expects
- Correct definitions of TP, FP, TN, and FN
- Which axis is actual versus predicted
- How precision and recall are read off the matrix
- Why it beats a single accuracy score
- How it generalizes to multiclass N x N grids
Common Mistakes
- Mixing up false positives and false negatives
- Confusing which axis holds actual versus predicted labels
- Reading only the diagonal and ignoring error cells
- Assuming the positive class is always the majority class
- Forgetting that a high-accuracy matrix can still miss all minority cases
Best Answer (HR Friendly)
“A confusion matrix is a small scorecard showing how a model's guesses line up with the real answers, split into right guesses and the two ways it can be wrong. It tells you not just how often the model is correct, but exactly what kind of mistakes it makes.”
Code Example
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0]
cm = confusion_matrix(y_true, y_pred)
print(cm)
# [[TN, FP],
# [FN, TP]]
ConfusionMatrixDisplay(cm).plot()Follow-up Questions
- How do you read precision and recall directly from a confusion matrix?
- What does a confusion matrix look like for a multiclass problem?
- Why can a model with 99% accuracy still be useless on imbalanced data?
- How does changing the decision threshold reshape the matrix?
- What is specificity and where does it come from in the matrix?
MCQ Practice
1. In a confusion matrix, a false negative means the model:
A false negative is an actual positive that the model labeled negative — a missed detection.
2. Which metric is computed as TP / (TP + FN) from the matrix?
Recall (sensitivity) divides true positives by all actual positives, TP + FN.
3. Why is a confusion matrix more useful than accuracy alone?
The matrix separates errors into false positives and false negatives, revealing behavior that a single accuracy figure hides.
Flash Cards
True Positive (TP) — Model predicted positive and the actual label is positive.
False Positive (FP) — Model predicted positive but the actual label is negative — a false alarm.
False Negative (FN) — Model predicted negative but the actual label is positive — a miss.
True Negative (TN) — Model predicted negative and the actual label is negative.
Why use it — It shows the kind of errors a model makes and is the source of precision, recall, and F1.