Understand the concept
Accuracy is the fraction of correct predictions. For rare positive cases, a model predicting only the majority class can achieve high accuracy while detecting no positives. Compare against a simple baseline before treating a model as useful.
With class 1 as positive, precision = TP / (TP + FP): how many predicted positives were correct? Recall = TP / (TP + FN): how many actual positives were found? F1 is their harmonic mean. A confusion matrix exposes the counts behind these metrics.
Choose metrics with the use case, label balance, and error costs in mind. Changing a threshold can change precision and recall; select it using validation data. For undefined ratios, document the convention. This example defines a zero denominator as a score of 0 and uses only the standard library.
Run the example
actual = [1, 1, 1, 0, 0, 0]
predicted = [1, 0, 1, 1, 0, 0]
tp = sum(a == 1 and p == 1 for a, p in zip(actual, predicted))
fp = sum(a == 0 and p == 1 for a, p in zip(actual, predicted))
fn = sum(a == 1 and p == 0 for a, p in zip(actual, predicted))
precision = tp / (tp + fp) if tp + fp else 0
recall = tp / (tp + fn) if tp + fn else 0
print(tp, fp, fn)
print(f"Precision: {precision:.2f}, Recall: {recall:.2f}")Output / what to expect
2 1 1
Precision: 0.67, Recall: 0.67Try it yourself
For 100 examples with 5 positives, an all-negative model gets how much accuracy and positive-class recall? What does accuracy hide?
View a suggested solution
accuracy = 95 / 100
recall = 0 / 5
print(accuracy, recall) # 0.95 0.0
# High accuracy hides the fact that every positive was missed.Reference: Official documentation ↗