Precision, Recall & F1
Count true positives before dividing. A zero denominator returns zero; accuracy alone hides an imbalanced classifier.
tp, fp, fn = 2, 1, 3
print(tp / (tp+fp))
print(tp / (tp+fn))From arithmetic to an experiment
Count true positives before dividing. A zero denominator returns zero; accuracy alone hides an imbalanced classifier. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
prf(actual, predicted) returns (precision, recall, F1) for binary 0/1 labels. For each undefined ratio return 0.0.
The explanation opens here once every check passes.