2.6.7 · D4Model Evaluation & Selection

Exercises — Accuracy, precision, recall, F1-score

2,203 words10 min readBack to topic

A quick reference figure — the confusion matrix as a 2×2 grid of counts, so you always know which cell each letter lives in:

Figure — Accuracy, precision, recall, F1-score

Level 1 — Recognition

(Can you read the confusion matrix and pull the right numbers into the right formula?)

Recall Solution 1.1

WHAT we want: fraction of all predictions that were correct. WHY accuracy: the question says "how many did it get right overall" — no mention of a class we care about more, so we treat every cell equally. Correct cells are the two "true" ones: . Total = all four cells . Answer: 0.85 (85%).

Recall Solution 1.2

Precision asks: of everything I called positive, how much was right? The "called positive" column is . Recall asks: of everything that actually was positive, how much did I catch? Actual positives are . Answers: , . Notice the denominators differ — that's the whole point of these being two different questions.

Recall Solution 1.3

The trick: the first word (True/False) tells you if the model was right; the second word (Positive/Negative) is what the model said.

  • (a) said positive, was right → TP
  • (b) said positive, was wrong → FP (Type I error, a false alarm)
  • (c) said negative, was wrong → FN (Type II error, a miss)
  • (d) said negative, was right → TN

Level 2 — Application

Recall Solution 2.1

F1 — the harmonic mean, which drags toward the smaller of P and R: Answers: Acc , , , . Note how accuracy (0.97) looks glorious while F1 (0.667) is honest — because the negatives dominate.

Recall Solution 2.2

WHY this form exists: it skips computing P and R separately — you plug the raw counts straight in. It's algebraically identical (see the parent's derivation). Match confirmed: . Same answer, fewer steps — handy when you already have the counts.

Recall Solution 2.3

Answers: , , . This is the classic case: perfect precision, catastrophic recall. The arithmetic mean would give (misleading); the harmonic mean gives (honest) because it refuses to reward one metric when the other is near zero.


Level 3 — Analysis

Recall Solution 3.1

Model A: Model B: By F1: Model B wins (). For cancer screening specifically: a missed cancer (FN) is catastrophic, so we prioritise recall. Model A catches 18/20 sick patients (recall 0.90); Model B misses 6 (recall 0.70). Model A is safer for screening despite its worse F1 — because F1 balances both errors equally, but here the two errors are not equally costly. Lesson: the "better" model depends on which error hurts more, not on a single blended number.

Recall Solution 3.2

If , the two denominators are identical and the numerators are both , so . Call this common value . Then Meaning: when your two error types are equal in count, all three of P, R, F1 collapse to one number — there's nothing for F1 to "balance". Model B in Example 4 of the parent () is exactly this case.


Level 4 — Synthesis

Recall Solution 4.1

Key insight: F1 depends on P and R only — the raw counts have already been folded into those two numbers. So yes: Answer: . You cannot recover accuracy though — that needs TN, which P and R never see. This is why F1 survives on imbalanced data: it ignores the (often huge) TN count entirely.

Recall Solution 4.2

Step 1 — translate the requirement into recall. "At most 1 miss out of 20" means , so . At the boundary , : So the recall floor is 0.95. Step 2 — precision at : Step 3 — F1: Answers: recall floor , , . This is exactly how Threshold Optimization works: fix the metric a stakeholder cares about (here recall ), then move the decision threshold to maximise the other.


Level 5 — Mastery

Recall Solution 5.1

(a) Accuracy: (b) Recall: Precision is — undefined. By the standard scikit-learn convention we define precision when there are no positive predictions (the model raised zero correct alarms). Then So recall , F1 . (c) A model can score 99.9% accuracy while being completely useless on the class that matters — proof that on imbalanced data accuracy is the wrong headline metric; recall/F1 expose the failure.

Recall Solution 5.2

(a) : which is exactly the F1 formula. ✓ (b) With , : Ordinary F1 for the same numbers: . Why: here recall (0.9) is higher than precision (0.6). leans toward recall, so it rewards this model more than the balanced F1 does. If instead precision had been the higher number, would have scored lower than F1 — because it would be down-weighting the metric that happened to be strong. The moral: lets you tune which mistake the score forgives.


Recall Self-check: what each metric refuses to look at

Accuracy uses all four cells including the huge TN — that's its weakness on imbalance ::: True; precision and recall both ignore TN, which is why F1 survives imbalanced data The harmonic mean is used for F1 because it ::: collapses toward the smaller of P and R, punishing any model that is lopsided To compute F1 you must have the raw counts TP, FP, FN ::: False — needs only precision and recall

Related deep dives: Confusion Matrix · Precision-Recall Curve · ROC Curve and AUC · Cross-Validation · Multi-Label Classification · back to the parent note.