PythiloLesson 2 of 8 · progress kept in this browser

Read a confusion matrix before accuracy

Accuracy can hide failures when one class is rare. Count true positives, false positives and false negatives, then compute precision and recall from those counts. F1 combines both and is zero when no true positive exists. An empty denominator needs a deliberate convention; this exercise uses zero instead of raising or returning NaN.

tp, fp, fn = 2, 1, 3
print(2 * tp / (2 * tp + fp + fn))
Your task

Write binary_metrics(truth, predicted), equal-length lists of 0/1 labels. Return (precision, recall, f1), with zero for any zero denominator.