Confusion matrices, precision and recall
A defect detector is '95% accurate'. It might still miss every faulty part. How?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
Every perception system that makes yes/no decisions makes two kinds of mistake. Engineers must report both, because the cost of a false alarm and the cost of a miss are rarely the same: a scrapped good part versus a faulty brake reaching a customer.
- ◇ Build a confusion matrix (TP, FP, FN, TN) from labelled test results.
- ◇ Compute precision and recall and explain what each protects against.
- ◇ Choose a decision threshold by weighing false positives against false negatives.
2.Discover
On the tuning batch, slide the threshold from 0 to 100 and watch the four boxes. Find a threshold with recall at least 90%, then one with precision at least 90%. Can you get both at once? Write down the threshold you'd pick for a brake-part line and for a cheap plastic clip.
Loading the interactive…
Can't use the interactive? Read a description instead
Sixty parts are drawn on a score strip: faulty parts as triangles that tend to score high, good parts as circles that tend to score low, with overlap in the middle. A threshold line splits them into flagged and passed. The table shows TP, FN, FP and TN; at a threshold of 50 on the tuning batch most faulty parts are flagged and a handful of good parts are wrongly flagged.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A detector flags 25 parts, of which 20 are really faulty. What is its precision, as a percentage?
4.Predict
5 in 100 parts are faulty. A detector says 'good' to every part. What is its accuracy?
Guess first. Being wrong here is how the surprise works.
5.Understand
Four outcomes
With 'faulty' as the positive class: TP faulty and flagged; FN faulty but passed (a miss); FP good but flagged (a false alarm); TN good and passed. The confusion matrix lays these out as a 2×2 table of counts, rows for the truth and columns for the decision.
Precision and recall
Precision = TP ÷ (TP + FP): when it says faulty, how often is it right? Low precision wastes good parts. Recall = TP ÷ (TP + FN): of the faulty parts, how many does it catch? Low recall lets faults through. Accuracy = (TP + TN) ÷ total, which is dominated by the common class.
The threshold trade-off
Lowering the threshold flags more parts: recall rises and precision usually falls. Raising it does the opposite. Where to set it is not a maths question but an engineering one: what does each kind of mistake cost? Next lessons put numbers on that.
Explain it another way
A smoke alarm that goes off for toast has poor precision; one that stays quiet in a real fire has poor recall. You would rather have the toast problem.
Recall is also called sensitivity or the true-positive rate; the false-positive rate is FP/(FP + TN). Sweeping the threshold traces a precision–recall curve or an ROC curve, summarising performance across all thresholds.
F1 = 2PR/(P + R), the harmonic mean of precision and recall, is one way to combine them, but it silently assumes both mistakes cost the same.
Medical screening tests are tuned for high recall (miss few cases) and followed by a second, more precise test, because a missed illness costs far more than an extra check.
Worked example 1
TP = 40, FP = 10, FN = 20, TN = 930. Find precision, recall and accuracy.
Precision = 40/50 = 80%. Recall = 40/60 ≈ 66.7%. Accuracy = 970/1000 = 97%. The detector misses a third of the faulty parts despite 97% accuracy.
Common mix-ups
- High accuracy means a good detector.
✓ When positives are rare, a detector can be highly accurate and still miss most of them. Report precision and recall. - There is one correct threshold.
✓ The best threshold depends on the costs of each mistake, which depend on the application.
6.Watch
→ Watch how each box of the matrix is defined; then notice that this lesson calls 'faulty' the positive class.
Builds the four-box table of right and wrong answers that every classifier, from a vision check to a medical test, is judged by.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A defect detector is tested on labelled parts: TP = 23, FP = 33, FN = 8, TN = 108. What is its recall, as a percentage?
Practice 3 · a fresh version every time
A defect detector is tested on labelled parts: TP = 37, FP = 21, FN = 24, TN = 251. What is its precision, as a percentage?
Practice 4
You lower the detection threshold. What usually happens?
Practice 5
For an airport bag scanner, which matters most?
8.Apply it
Using the tuning batch, list the confusion matrix at thresholds 40, 50, 60 and 70. Pick one for a line where a missed fault costs ten times as much as a scrapped good part, and justify it with the counts. Then check it on the fresh test batch.
9.Reflect
Concepts: Evaluation metrics · Thresholding and simple classification