MODEL EVALUATION · 8 MIN
Accuracy, Precision & Recall
Accuracy alone can mislead; precision and recall show what kind of mistakes a model makes.
PIXEL explains
- Precision = TP / (TP + FP): of everything flagged, how much was right.
- Recall = TP / (TP + FN): of everything real, how much was caught.
- With rare classes, a model that always says 'no' can have high accuracy and zero recall.
Real world: A fraud detector must catch frauds (recall) without blocking honest customers (precision).
Code
y = [1, 0, 1, 1, 0, 0]
pred = [1, 0, 0, 1, 1, 0]
tp = sum(a == b == 1 for a, b in zip(y, pred))
fp = sum(a == 0 and b == 1 for a, b in zip(y, pred))
fn = sum(a == 1 and b == 0 for a, b in zip(y, pred))
print(round(tp / (tp + fp), 2), round(tp / (tp + fn), 2))Output ›
0.67 0.67Try it in the online compiler →Checkpoint quiz
Missing many real cancer cases means low…