Explain a Confusion Matrix — Interview Answer — ML Technical Interview Questions & Answers

Model answer covering TP/FP/TN/FN.

Explain a Confusion Matrix — Interview Answer

  • Say this: “I draw predicted vs actual. For the positive class I count TP FP FN TN. Precision and recall come from that table.” Then pick fail or spam as positive and put numbers.
  • Example: 8 TP, 2 FN, 3 FP, 87 TN. Recall = 8/10. Precision = 8/11. Accuracy looks high because of 87 TN — mention that.
  • Multiclass: a bigger grid, one row per true class.
  • Trap: swapping axes. Trap: never saying the positive class.

Technical Interview Tip: Draw the grid with numbers. Then precision and recall.

Code Examples for Explain a Confusion Matrix — Interview Answer

Explain a Confusion Matrix — Interview Answer — 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]

Explain a Confusion Matrix — Interview Answer — confusion matrix counts TP/TN/FP/FN — the base for precision, recall, and accuracy.

How to Explain Explain a Confusion Matrix — Interview Answer to an Interviewer

FP vs FN?

False alarm vs miss

Why not only accuracy?

TN can hide misses on the rare class