Machine Learning Β· Chapter 26 of 40

Accuracy, Precision, Recall, F1

ACCURACY: fraction correct. PRECISION: of predicted positives, how many are true. RECALL: of true positives, how many did we catch. F1: harmonic mean of precision and recall.

Choose the metric that matches the business cost of errors.

Example 1 (python)
from sklearn.metrics import precision_score, recall_score, f1_score
print(precision_score(y_test, y_pred))
print(recall_score(y_test, y_pred))
print(f1_score(y_test, y_pred))
Output
0.90
0.84
0.87

Per-class or averaged.

Example 2 (python)
# Spam filter: want HIGH precision (don't block real mail)
# Cancer detection: want HIGH recall (don't miss cases)

Different problems, different metric priorities.

Key points

  • Accuracy = correct / total.
  • Precision = TP / (TP + FP).
  • Recall = TP / (TP + FN).
  • F1 balances precision and recall.
πŸ’‘ Note: On imbalanced data (99% negative), a model that predicts 'negative' always is 99% accurate β€” but useless. Metric choice matters.

πŸ“ Quick Quiz

1. Precision is:

2. Recall is:

3. F1 is the: