Lite / Intelligent Robotic Systems / Week 26
Confusion matrices, precision and recall
A defect detector is '95% accurate'. It might still miss every faulty part. How?
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.
Predict
5 in 100 parts are faulty. A detector says 'good' to every part. What is its accuracy?
- 5%
- 50%
- 95%
Guess first, then reveal
It is right about all 95 good parts and wrong about the 5 faulty ones: 95% accuracy while catching nothing. That is why accuracy alone hides the important mistakes when one class is rare.
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.
What the interactive shows (text version)
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.
Watch (optional)
Watch for: 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.
“Machine Learning Fundamentals: The Confusion Matrix” by StatQuest with Josh Starmer (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Describe a detector in everyday life where you would accept many false alarms, and one where you wouldn't.
Write your answer on paper or in your own notes. Lite never stores what you write.