Sub topic 8
F1: combining precision and recall
This chapter assumes you can read a binary confusion matrix and compute precision and recall from the TP, FP and FN counts. It also assumes you can read short Python snippets.
Why one number is useful
Precision and recall pull in opposite directions. A model that predicts "positive" rarely tends to have high precision and low recall. A model that predicts "positive" often tends to do the reverse. When you compare two models, or two thresholds, it helps to have a single score that is low when either metric is low.
The F1 score is that number. It is the harmonic mean of precision and recall:
F1 = 2 × precision × recall / (precision + recall)
Why a harmonic mean
The ordinary average (arithmetic mean) hides a weak metric. Suppose a model has precision 1.0 and recall 0.1. The arithmetic mean is 0.55, which sounds acceptable. The model finds only one real positive in ten, so that score is misleading.
The harmonic mean stays close to the smaller of the two values:
| Precision | Recall | Arithmetic mean | F1 |
|---|---|---|---|
| 0.60 | 0.75 | 0.675 | 0.667 |
| 1.00 | 0.10 | 0.550 | 0.182 |
| 0.50 | 0.50 | 0.500 | 0.500 |
F1 equals 1.0 only when precision and recall are both 1.0. It is 0 if either one is 0.
F1 from the cell counts
You can skip the intermediate metrics and compute F1 directly from the matrix:
F1 = 2 × TP / (2 × TP + FP + FN)
This form shows what F1 uses. It counts TP, FP and FN, and it ignores TN. For the example matrix from this tutorial (TP = 3, FP = 2, FN = 1, TN = 4), F1 = 6 / (6 + 2 + 1) = 0.667.
Computing it in Python
The code below uses the labels from the earlier examples. It computes F1 by hand and with scikit-learn, and the two results should match.
from sklearn.metrics import confusion_matrix, f1_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
precision = tp / (tp + fp) # 0.6
recall = tp / (tp + fn) # 0.75
f1_manual = 2 * precision * recall / (precision + recall)
f1_counts = 2 * tp / (2 * tp + fp + fn)
print(round(f1_manual, 3), round(f1_counts, 3)) # 0.667 0.667
print(round(f1_score(y_true, y_pred), 3)) # 0.667Common errors
- Ignoring true negatives. Because F1 never looks at TN, it cannot tell you how well the model handles the negative class. Pair it with specificity when negatives matter.
- Treating F1 as universally right. F1 weights precision and recall equally. If a missed positive costs far more than a false alarm, as in disease screening, you may prefer a recall-weighted score such as F2, which
sklearn.metrics.fbeta_scorecomputes withbeta=2. - Dividing by zero. If a model never predicts the positive class, precision is 0/0. scikit-learn warns and returns 0. Set
zero_division=0inf1_scoreto make that choice explicit and silence the warning. - Comparing F1 across different thresholds without saying so. F1 depends on the decision threshold, so report which threshold produced it.
Exercise
Exercise
A classifier has precision 0.5 and recall 1.0. Compute its F1, and explain why it is lower than the arithmetic mean of 0.75.
Show answer
F1 = 2 × 0.5 × 1.0 / (0.5 + 1.0) = 1.0 / 1.5 ≈ 0.667. The harmonic mean is pulled toward the smaller value, here the precision of 0.5, so a weak metric cannot be hidden by a strong one.