2.6.7 · D3Model Evaluation & Selection

Worked examples — Accuracy, precision, recall, F1-score

2,015 words9 min readBack to topic

This is the hands-on companion to the parent note on Accuracy, Precision, Recall, F1. Before any formula, remember the four raw counts they are all built from — the four cells of the Confusion Matrix:

Recall The four counts (never use them before you can name them)

Given a positive class (the thing we hunt for, e.g. "cancer") and a negative class ("healthy"):

  • TP = predicted +, truly + (a correct catch)
  • FP = predicted +, truly − (a false alarm)
  • FN = predicted −, truly + (a miss)
  • TN = predicted −, truly − (a correct all-clear)

Every metric below is only arithmetic on these four whole numbers.

Here are the four formulas, all in one place, so no example uses a symbol you haven't seen:


The scenario matrix

Every situation these four metrics can throw at you falls into one of these case classes. The examples that follow each hit one labelled cell, and together they cover the whole grid.

Cell Case class What makes it tricky Example
C1 Balanced data, normal counts baseline — just plug in Ex 1
C2 Heavy class imbalance accuracy lies; precision/recall disagree Ex 2
C3 Degenerate: model predicts all negative → precision is Ex 3
C4 Degenerate: model predicts all positive → recall , precision tiny Ex 4
C5 Limiting: one lucky positive prediction precision , recall Ex 5
C6 Precision–recall trade-off across two models F1 as tie-breaker Ex 6
C7 Threshold shift on one model how counts move as you slide the cutoff Ex 7
C8 Real-world word problem (fraud) with a cost which metric to optimise & why Ex 8
C9 Exam twist: given metrics, recover the counts working backwards Ex 9

C1 — The clean baseline

Forecast: with only 7 mistakes out of 100, guess: all four metrics land around . Let's check whether they really agree.

  1. Accuracy. Why this step? It asks "what fraction of all calls were right", so we sum the two correct cells over everything.
  2. Precision. Why? Of everything called positive (), how many were truly positive?
  3. Recall. Why? Of everything truly positive (), how many did we catch?
  4. F1. Why? One number balancing and via harmonic mean.

Verify: all four sit between and — consistent with a good model on balanced data. Cross-check F1 through the other form: . ✓ Same number, so the two F1 formulas agree.


C2 — When accuracy lies

Forecast: the model catches zero fraud. Will accuracy expose it? Guess before reading.

  1. Accuracy. Why first? It's what a naive person quotes.
  2. Recall. Why? This is the honest question: of the 10 real frauds, how many caught? .
  3. Precision. Why? Predicted-positive count is — see the degenerate zero-over-zero warning below; here it's conventionally 0 because there were no true catches.

Verify: accuracy, recall — a useless model looks excellent by accuracy alone. This is exactly why Class Imbalance Handling and the Precision-Recall Curve exist. Sanity: the figure below shows the confusion grid — the tiny fraud column is invisible against the giant TN block.

Figure — Accuracy, precision, recall, F1-score

C3 — Degenerate: predict everything negative

Forecast: which metric collapses to , which to something misleadingly high?

  1. Recall . Why? Denominator is the 20 real positives; it caught none.
  2. Precision undefined, reported . Why? No positive predictions were made.
  3. Accuracy . Why still 0.8? Because the 80 negatives were all "correctly" labelled by accident.
  4. F1 .

Verify: F1 correctly flags the model as worthless despite accuracy — the harmonic mean of " and anything" is . ✓


C4 — Degenerate: predict everything positive

Forecast: recall should be perfect. What happens to precision?

  1. Recall . Why? It missed nobody — every real positive is caught.
  2. Precision . Why so low? Of 100 alarms only 20 were real; the class base-rate () is exactly the precision of a shout-everything model.
  3. F1 .

Verify: cross-check with . ✓ Note recall is cheap — you can always max it by yelling positive. That's why we pair it with precision. See Threshold Optimization.


C5 — Limiting case: one lucky catch

Forecast: precision and recall are about to point in opposite directions.

  1. Precision . Why perfect? Every alarm it raised was right — but it barely raised any.
  2. Recall . Why terrible? It found 1 of 100 real cases.
  3. F1 .

Verify: F1 is near recall, not near precision. The harmonic mean is dragged toward the smaller input — this is the whole reason we prefer it over the arithmetic mean , which would flatter this model. ✓ The figure contrasts the two means.

Figure — Accuracy, precision, recall, F1-score

C6 — Trade-off: pick between two models

Forecast: A brags about precision, B is even-handed. Guess whose F1 wins.

  1. Model A. Why compute P and R separately? To see how it's lopsided.
  2. Model B.

Verify: B wins () even though A has higher precision. Why? F1's harmonic mean punishes A's low recall (). Balance beats a single strong axis. ✓


C7 — Watching the counts move: threshold shift

Forecast: lowering raises how many things we call positive. Does recall go up or down? Does precision?

  1. At we predict + for scores . Why these? They alone clear the bar. Truths there: . So . Remaining positives (0.70, 0.55, 0.30) are missed → .
  2. At we predict + for scores : . Truths: .

Verify: lowering the threshold caught more real positives (recall ) — as expected, fewer misses. Here precision also rose because the newly-admitted items happened to be true positives; in general lowering tends to lower precision. This sweep is exactly what the ROC Curve and AUC and Precision-Recall Curve plot. The figure shows the score line with the two cutoffs.

Figure — Accuracy, precision, recall, F1-score

C8 — Real-world word problem with a cost

Forecast: with a cost ratio between misses and reviews, which error hurts more?

  1. Current recall . Why care? Each of the 20 missed frauds () costs $500. Missed-fraud cost =20\times\500=$10{,}000$.
  2. Current precision . Why compute? Each false alarm () costs one $5 review. Review cost =(TP+FP)\times\5=480\times$5=$2{,}400$.
  3. Compare the levers. Missed-fraud cost ($10 000) dwarfs review cost ($2 400). Cutting misses (raising recall) saves more per unit than cutting false alarms.

Verify: total current loss =\10{,}000+$2{,}400=$12{,}400FN=5$2{,}500$7{,}500$ saving, far larger than anything precision could recover here. So optimise recall. This is the reasoning formalised in Cost-Sensitive Learning. ✓


C9 — Exam twist: work backwards

Forecast: recall pins from the actual-positive total; precision then pins . Try it before reading.

  1. Find . Why start here? Recall and (all actual positives).
  2. Find . Why now? Precision and is known.
  3. F1. Why the count form? is unknown but F1 never needs it.

Verify: back-substitute — recall ✓, precision ✓. Cross-check F1 via ✓. Notice F1 didn't need at all — that's why it survives extreme imbalance where accuracy fails.


Recap — one cloze per case class

Recall the metric that each degenerate case exposes.

All-negative model has recall equal to
(it catches no positives)
All-positive model has recall equal to
(it misses nobody, but precision collapses to the base rate)
The metric that stays high yet meaningless on imbalanced data is
accuracy
F1 is dragged toward the
smaller of precision and recall (harmonic-mean behaviour)
Precision is undefined when
the model makes no positive predictions ()
Lowering the decision threshold generally
raises recall and lowers precision

See also Cross-Validation for estimating these metrics reliably, and Multi-Label Classification for when there is more than one positive class.