What is a Confusion Matrix?
Learn what a confusion matrix is, how true/false positives and negatives work, and how it drives precision, recall, and F1 in classification models.
Expected Interview Answer
A confusion matrix is a table that summarizes a classification model's predictions against actual labels, breaking results into true positives, true negatives, false positives, and false negatives so you can see exactly what kinds of mistakes the model makes.
Each row typically represents the actual class and each column the predicted class (or vice versa), with the diagonal cells showing correct predictions and off-diagonal cells showing specific error types. For binary classification this yields four cells: true positives (correctly predicted positive), true negatives (correctly predicted negative), false positives (predicted positive but actually negative, a type I error), and false negatives (predicted negative but actually positive, a type II error). From these four counts you derive nearly every classification metric: accuracy, precision, recall, F1 score, and specificity. A confusion matrix is especially valuable on imbalanced datasets, where overall accuracy can look great while the model completely fails on the minority class, a failure the matrix exposes immediately.
- Shows exactly which types of errors a model makes, not just an aggregate score
- Exposes poor minority-class performance hidden by overall accuracy
- Foundation for deriving precision, recall, F1, and specificity
- Extends naturally to multi-class problems as an N×N table
- Guides threshold tuning by visualizing the tradeoff between error types
AI Mentor Explanation
A confusion matrix is like a scorer's breakdown of every LBW appeal into four outcomes: correctly given out, correctly given not out, wrongly given out, and wrongly given not out. Just glancing at the umpire's overall accuracy hides which specific mistake is happening most, while this breakdown shows exactly whether the umpire is too trigger-happy or too lenient.
Step-by-Step Explanation
Step 1
Get predictions and true labels
Run the trained model on a held-out test set to get predicted labels, and pair them with the known true labels.
Step 2
Build the table
Tabulate counts of every (actual, predicted) label combination into rows and columns.
Step 3
Identify the four core cells (binary case)
Label the diagonal as true positives and true negatives, and off-diagonal as false positives and false negatives.
Step 4
Derive metrics
Compute accuracy, precision, recall, F1, and specificity directly from these four counts.
Step 5
Check for class imbalance issues
Inspect whether the minority class has disproportionately many false negatives or false positives despite high overall accuracy.
Step 6
Use it to tune the decision threshold
Recompute the matrix at different probability thresholds to trade off false positives against false negatives for your use case.
What Interviewer Expects
- Defines all four cells: true positive, true negative, false positive, false negative
- Can derive precision, recall, and F1 from the matrix
- Explains why accuracy alone is misleading on imbalanced data
- Knows the matrix extends to multi-class as an N×N table
- Connects the matrix to threshold tuning for classification decisions
Common Mistakes
- Mixing up rows and columns (actual vs predicted) inconsistently
- Confusing false positive with false negative
- Relying only on accuracy without checking the matrix on imbalanced data
- Forgetting the matrix generalizes beyond binary classification
- Not connecting the matrix to precision/recall tradeoffs
Best Answer (HR Friendly)
“A confusion matrix is a simple table that shows how many predictions a model got right and wrong, broken down by exactly what kind of mistake it made — like mistakenly flagging something as positive when it wasn't, or missing something it should have caught. It helps you understand a model's specific weaknesses beyond a single accuracy number.”
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))Follow-up Questions
- How do you compute precision and recall from a confusion matrix?
- Why can accuracy be misleading on an imbalanced dataset?
- How does a confusion matrix generalize to multi-class classification?
- What is an ROC curve and how does it relate to the confusion matrix at different thresholds?
- How would you use a confusion matrix to decide on a classification threshold for a medical diagnosis model?
MCQ Practice
1. In a confusion matrix, a false negative means:
A false negative is when the model predicts the negative class but the true label is actually positive — a missed detection.
2. Why is a confusion matrix especially useful on imbalanced datasets?
On imbalanced data, accuracy alone can look high while the model fails on the minority class; the confusion matrix exposes this directly.
3. Which metrics are directly derived from a confusion matrix?
Precision, recall, F1 score, specificity, and accuracy are all computed directly from the four confusion matrix cell counts.
Flash Cards
What are the four cells of a binary confusion matrix? — True positive, true negative, false positive, and false negative.
What is a false positive? — The model predicted positive, but the true label was actually negative (a type I error).
Why is accuracy alone risky on imbalanced data? — A model can achieve high accuracy by always predicting the majority class, while completely failing the minority class.
What metrics come directly from a confusion matrix? — Accuracy, precision, recall, F1 score, and specificity.