Precision, recall and F1
A flood warning and a spam filter make opposite mistakes on purpose. Which one should never miss?
- Calculate precision, recall and F1
- Count a confusion matrix in Python
- Choose the metric a problem needs
Precision, recall and F1
A flood warning and a spam filter make opposite mistakes on purpose. Which one should never miss?
- Calculate precision, recall and F1
- Count a confusion matrix in Python
- Choose the metric a problem needs
Story
Learn it
Accuracy cannot tell a flood warning from a spam filter, but they need opposite things. The flood warning must never miss a flood, even if it raises false alarms. The spam filter must be sure before it hides a message, even if some spam slips through. Three metrics, all read from the confusion matrix, capture this: precision, recall and F1. One question picks between them: which does more harm here, a false alarm or a miss?
Words to know
TP ÷ (TP + FP). Of everything the model flagged, how much was real? Matters when a false alarm is costly.
(or sensitivity) — TP ÷ (TP + FN). Of everything that was real, how much did it catch? Matters when a miss is costly.
2 × Precision × Recall ÷ (Precision + Recall). One number that balances both, useful on unbalanced data.
in Python's csv.DictReader, each row is a dictionary: row["actual"] is that row's actual class.
Step by step: precision, recall and F1 for the fever checker
From Lesson 3.3: TP = 4, FN = 1, FP = 5, TN = 40.
- Precision = 4 ÷ (4 + 5) = 4 ÷ 9 = 0.44. Fewer than half the children sent to the nurse really had fever.
- Recall = 4 ÷ (4 + 1) = 4 ÷ 5 = 0.80. It caught 4 of the 5 children who did.
- F1 = 2 × 0.44 × 0.80 ÷ (0.44 + 0.80) = 0.704 ÷ 1.24 = 0.57.
- Which matters here? A missed fever spreads to a class; a false alarm costs ten minutes. Recall matters most, and 0.80 means one sick child in five is still missed.
Choosing the metric:
| Problem | Worse mistake | Metric |
|---|---|---|
| flood warning, disease screening, fraud check | a miss | recall |
| spam filter, satellite-launch weather | a false alarm | precision |
| both matter, and data is unbalanced | — | F1 |
Counting in Python: a loop reads the CSV row by row and adds 1 to tp whenever
row["actual"] == "fever" and row["predicted"] == "fever", and likewise for the other three boxes.
Careful: F1 is not the plain average of precision and recall. It is pulled towards the lower one: for 0.1 and 0.9, F1 is 0.18, not 0.5. And a program counts exactly what you tell it to, so name the positive class correctly before you count.
Your missions
programs/metrics.py · datasets/leaf-predictions.csvBy hand first
7 minin pairs- 1
Copy the leaf model's matrix from Lesson 3.3: TP 11, FN 4, FP 6, TN 39.
- 2
Work out precision, recall and F1 on paper or in Calc. Round to two decimals.
Let Python count
12 min- 3
Copy
metrics.pyinto thedatasetsfolder, next toleaf-predictions.csv, and open it in Thonny. Read it. A row is a dictionary:row["actual"]is that leaf's actual class. - 4
Predict: what will the first line it prints say? Write it down.
Only you can see this - 5
Run it. Does it match your prediction — and your answers from Part 1?
- 6
Change it: add a line that also prints the accuracy. Predict the number, then run it.
Choose the metric
6 min- 7
For each problem, write precision, recall or F1, and the reason in five words:
- A flood warning for river villages
- A spam filter on a school email account
- A test that screens children for a vision problem
- A system that decides which days are safe to launch a satellite
- A bank flagging payments that might be fraud
- A filter keeping unsafe videos away from young children
Change the program so the positive class is healthy instead of scab. What happens to precision and recall — and does the change make sense?
No laptop today?
Do Part 1 on the board. Then your teacher reads metrics.py aloud, line by line, while the class
keeps four tallies — TP, FN, FP, TN — for the first ten rows of the CSV. Do Part 3 as a vote.
Debate it
The flood model is tuned for high recall, so it cries wolf. People have started ignoring it.
- If people ignore the warnings, is high recall still helping them? What would you change?
- Who should decide how many false alarms are acceptable — the model's makers, or the villages?
Check yourself · practice, not a test
In the app, students answer these one at a time and each answer is checked straight away. It's practice, never a mark — anything they miss comes back later for another go.
1.A disease test must not miss sick people. Which metric matters most?
- APrecision
- BRecall
- CAccuracy
- DError rate
2.Precision = TP ÷ (TP + ______).
3.TP = 11, FP = 6. What is the precision?
- A0.35
- B0.65
- C0.73
- D0.83
4.F1 score combines precision and recall into one number.
TrueFalse5.Put these lines in order so the program counts a True Positive (inside the loop they are indented):
- 1tp = tp + 1
- 2for row in csv.DictReader(f):
- 3if actual == "scab" and row["predicted"] == "scab":
- 4actual = row["actual"]
Precision fears false alarms. Recall fears misses. Choose by the harm.
Typing practice
Skill: typing Python with quotes and brackets (5 min)
-
On typing.com: the symbols lessons.
-
Type three times, eyes on the screen:
if actual == "scab" and predicted == "scab": -
Two equals signs to compare, one to store. Goal this term: 38–44 WPM at 95%.
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.