DEPLOYEDAI-era interview training
RankBootstrapping
XP 0 / 250
0
0%
Ready
ML & Data Science Interviews · Evaluation Metrics · Lesson 1 of 3

Confusion matrix, precision, recall & F1

8 min

Accuracy alone lies, especially with imbalance. Start from the confusion matrix — true/false positives and negatives — and derive metrics that reflect the cost of each error type.

  • Precision = TP / (TP + FP): of everything flagged positive, how much really was? Punishes false alarms.
  • Recall (sensitivity) = TP / (TP + FN): of all real positives, how many did we catch? Punishes misses.
  • F1 = harmonic mean of precision and recall — one number when you care about both and classes are imbalanced.
  • Accuracy = (TP+TN)/all — misleading when one class dominates (99% 'not fraud' by predicting everything negative).
Which error hurts more?Cancer screening → favor recall (a miss is deadly; a false alarm just means more tests). Spam filter → favor precision (flagging a real email as spam is worse than letting one spam through). The domain sets the tradeoff.
# Confusion matrix (binary)
#                 predicted +   predicted -
#  actual +          TP            FN
#  actual -          FP            TN
# precision = TP / (TP + FP)   # trust of a positive prediction
# recall    = TP / (TP + FN)   # coverage of real positives
# F1        = 2 * P * R / (P + R)

◆ Lock it in

  • Build from the confusion matrix; accuracy hides imbalance.
  • Precision penalizes false alarms; recall penalizes misses.
  • F1 balances both — pick based on which error is costlier.
Feynman drill — say it out loudExplain precision vs recall using a concrete example where one clearly matters more than the other.
Step 1 rate your confidence · Step 2 pick your answer
A fraud model must catch as many fraudulent transactions as possible, even at the cost of extra manual reviews. Which metric do you optimize?
Step 1 — how sure are you?