Confusion Matrix — ML Technical Interview Questions & Answers
True/false positives and negatives laid out in a grid.
Confusion Matrix
- Simple meaning: A 2×2 (or bigger) table of counts. For “fail” as the positive class: TP = said fail, was fail. FP = said fail, actually pass. FN = said pass, actually fail. TN = said pass, was pass.
- Picture: 10 real fails. You caught 7 (TP=7) and missed 3 (FN=3). You also scared 5 pass students (FP=5). Accuracy can still look high if most students pass.
- From the table: precision = TP/(TP+FP). recall = TP/(TP+FN). Write the table before the formulas in a viva.
- Trap: swapping FP and FN. Trap: not saying which class is “positive”.
predicted fail predicted pass
actual fail TP FN
actual pass FP TNTechnical Interview Tip: Draw the grid. Positive class named. Then TP FP FN TN.
Code Examples for Confusion Matrix
Confusion Matrix — working example
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 0]
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, y_pred))
tn = sum(t == 0 and p == 0 for t, p in zip(y_true, y_pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
print({"TP": tp, "TN": tn, "FP": fp, "FN": fn})
# sklearn.metrics.confusion_matrix(y_true, y_pred) → [[TN, FP], [FN, TP]] for labels [0,1]Confusion matrix counts TP/TN/FP/FN — the base for precision, recall, and accuracy.
How to Explain Confusion Matrix to an Interviewer
What is FN?
Predicted negative, actually positive Missed fail / missed disease
Precision formula?
TP / (TP + FP)