The F1-score combines precision and recall into a single number using their harmonic mean — specifically chosen to punish an imbalance between the two, unlike a plain average which can be misleadingly high even when one of them is terrible.
Formula
Worked Example
Using \(\text{Precision}=0.6\) and \(\text{Recall}=0.75\) from the same confusion matrix:
from sklearn.metrics import f1_score
y_true = [1]*20 + [0]*80
y_pred = [1]*15 + [0]*5 + [1]*10 + [0]*70
print(f1_score(y_true, y_pred)) # 0.6667
Why Harmonic Mean, Not Plain (Arithmetic) Mean
Compare precision=1.0, recall=0.01: the plain average is \((1.0+0.01)/2 = 0.505\) — looking deceptively reasonable. The harmonic mean, \(F1 = 2(1.0)(0.01)/(1.0+0.01) \approx 0.0198\) — correctly reflecting that a model catching almost nothing (recall=0.01) is a genuinely bad model, no matter how precise its rare positive predictions are. Harmonic mean is dominated by the smaller of the two values, exactly the behavior you want when either metric being terrible should tank the combined score.
The F-Beta Generalization
F1 is the special case \(\beta=1\), weighting precision and recall equally. \(F_2\) (\(\beta=2\)) weights recall more heavily; \(F_{0.5}\) weights precision more heavily — useful when you want a single combined score but the business genuinely cares more about one side of the tradeoff.
from sklearn.metrics import fbeta_score
print(fbeta_score(y_true, y_pred, beta=2)) # weights recall more
print(fbeta_score(y_true, y_pred, beta=0.5)) # weights precision more
Practical Use Cases
- Any classification problem where both false positives and false negatives matter, and you need one number for model comparison
- Standard reporting metric for imbalanced classification, alongside precision and recall individually
Common Mistakes
- Reporting only F1 without also showing precision and recall separately — F1 hides which of the two is driving the score.
- Using F1 when the business genuinely cares much more about one side (recall or precision) than the other — F-beta with an appropriate beta is more honest in that case.
Interview Relevance
Q: "Why use the harmonic mean instead of a simple average for F1?" The harmonic mean is dominated by the smaller value, so a model with one very high and one very low metric (e.g. precision=1.0, recall=0.01) gets correctly penalized with a low F1 — a plain average would misleadingly report a moderate, seemingly-okay score.
Practice Question
Given precision=0.9 and recall=0.2, compute F1 by hand and compare it to the plain average of the two numbers.