Class 10 · Unit 3: Evaluating models · Lesson 3.4 · 40 min
Precision, recall and F1
A flood warning and a spam filter make opposite mistakes on purpose. Which one should never miss?
Today you will: Calculate precision, recall and F1 · Count a confusion matrix in Python · Choose the metric a problem needs
Story
In August the Jhelum rises fast. The district is trying out a flood-warning model: every evening it says flood or no flood for each village by the river.
Rahat: “It cried wolf four times last month. People have stopped listening.”
Hiba: “And how many floods did it miss?”
Rahat: “None.”
Hiba: “Then I'd keep it. A false alarm costs a night's sleep. A missed flood costs a house.”
Rahat thinks about his own phone. Last week its spam filter hid a message from school.
Rahat: “So my spam filter should be the other way round. It had better be sure before it hides anything.”
Watch
Two questions, two metrics — — scan code 10.3.4 in the printed book.
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
- Precision TP ÷ (TP + FP). Of everything the model flagged, how much was real? Matters when a false alarm is costly.
- Recall (or sensitivity) — TP ÷ (TP + FN). Of everything that was real, how much did it catch? Matters when a miss is costly.
- F1 score 2 × Precision × Recall ÷ (Precision + Recall). One number that balances both, useful on unbalanced data.
- Dictionary row 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.
On the laptop
You need: Thonny · programs/metrics.py · datasets/leaf-predictions.csv
Mission 1: By hand first (7 min, in 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.
Mission 2: 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.
- ☐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.
Mission 3: 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
Finished early?
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
1. A disease test must not miss sick people. Which metric matters most?
- ○ Precision
- ○ Recall
- ○ Accuracy
- ○ Error rate
2. Precision = TP ÷ (TP + ______).
3. TP = 11, FP = 6. What is the precision?
- ○ 0.35
- ○ 0.65
- ○ 0.73
- ○ 0.83
4. F1 score combines precision and recall into one number.
- ○ True
- ○ False
5. Put these lines in order so the program counts a True Positive (inside the loop they are indented):
- 1. tp = tp + 1
- 2. for row in csv.DictReader(f):
- 3. if actual == "scab" and row["predicted"] == "scab":
- 4. actual = row["actual"]
Remember
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%.