The F1 score combines precision and recall into a single number โ using a harmonic mean specifically, which penalizes a large imbalance between the two far more heavily than a simple average would.
Formula
Why the Harmonic Mean, Not a Simple Average
Consider a model with precision \(=1.0\) and recall \(=0.01\) (it only ever predicts positive for one clearly-obvious example, but is right every time it does). A simple average would give \(\frac{1.0+0.01}{2}=0.505\) โ a misleadingly middling-looking score for a model that's actually nearly useless (catching almost none of the true positives). The harmonic mean instead gives \(F_1 = 2\times\frac{1.0\times0.01}{1.0+0.01}\approx0.0198\) โ correctly reflecting that this model performs badly overall, since the harmonic mean is dominated by whichever of the two values is smaller.
Numerical Example
Continuing the spam example: Precision \(\approx0.774\), Recall \(=0.8\):
Code
from sklearn.metrics import f1_score
y_true = [1,1,1,1,1,0,0,0,0,0]
y_pred = [1,1,1,0,0,0,0,1,0,0]
print(f1_score(y_true, y_pred))
The F-beta Generalization
\(F_1\) is the special case \(\beta=1\), weighting precision and recall equally. \(F_2\) (\(\beta=2\)) weights recall more heavily โ appropriate when missing a positive (false negative) is considered worse than a false alarm; \(F_{0.5}\) weights precision more heavily, for the opposite priority.
Common Mistakes
- Assuming F1 always equally weighting precision and recall is the "correct" default for every task โ as with the F-beta generalization, when one type of error is clearly costlier than the other, weighting the metric accordingly (F2, F0.5) is more appropriate than blindly defaulting to F1.
- Averaging precision and recall arithmetically instead of using the harmonic mean โ this understates how badly a model with one very low component metric is actually performing.
Interview Relevance
Q: "Why does F1 use the harmonic mean of precision and recall instead of a simple average?" The harmonic mean is heavily influenced by whichever of the two values is smaller, so a model with one very low metric (even if the other is very high) receives an appropriately low F1 score โ reflecting genuinely poor overall performance. A simple arithmetic average would mask this imbalance, producing a misleadingly moderate-looking score.
Practice Question
A model has precision 0.6 and recall 0.9. Compute its F1 score, and compare it to what a simple arithmetic average of the two would give.