Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
What is a gold label?
Show the answer
A trusted reference decision used to check evaluator or system output.
Precision and recall ask different questions
Declare pass as the positive class. True positive (TP): reference pass and predicted pass. False positive (FP): reference fail but predicted pass. False negative (FN): reference pass but predicted fail. True negative (TN): both fail. In the paired-list examples, True means pass and False means fail.
zip(gold, predicted) pairs observations by position, not by item ID, and silently stops at the shorter list. The introductory counting tasks assume equal-length aligned boolean lists. Check alignment before pairing real data. sum counts boolean comparisons; (not g) and p identifies a false positive.
Precision is TP / (TP + FP): among predicted passes, how many match the reference? Recall is TP / (TP + FN): among reference passes, how many were found? These are different denominators. The small exercise functions assume a nonzero denominator; the independent report and this worked example return None when it is zero. Never equate undefined with a measured zero. A reference can itself contain mistakes.
# Keep predicted and reference denominators distinct.
def ratio(numerator, denominator):
if denominator == 0:
return None
return numerator / denominator
tp, fp, fn = 3, 1, 3
print("Precision:", ratio(tp, tp + fp))
print("Recall:", ratio(tp, tp + fn))
print("No predicted passes:", ratio(0, 0))
Precision is 0.75 while recall is 0.5. No predicted positives makes precision undefined, not automatically zero.
Calculate from the counts before rounding. Test all-positive, all-negative and empty cases, and describe the retained sample rather than treating a reference as infallible truth.
Predict, explain, then test
Answer these three self-reviewed checks before opening the comparisons. They are not scored. Coding checks use unfamiliar inputs under each task’s stated assumptions.
Distinguish errors
Reference fail and predicted pass contributes to which count?
Compare your answer · self-reviewed
FP, because the prediction declares the positive pass class incorrectly relative to the reference.
Choose a ratio
With TP=3, FP=1 and FN=3, what are precision and recall?
Compare your answer · self-reviewed
Precision is 3/4 = 0.75; recall is 3/6 = 0.5. Their denominators answer different questions.
Inspect pairing
What happens if gold has three entries and predicted has two when zip is used?
Compare your answer · self-reviewed
Only two pairs are visited. Validate length and item alignment before calculating real metrics.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.