Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #1710

F1-Score

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

\[ F1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision}+\text{Recall}} \]

Worked Example

Using \(\text{Precision}=0.6\) and \(\text{Recall}=0.75\) from the same confusion matrix:

\[ F1 = 2 \times \frac{0.6 \times 0.75}{0.6+0.75} = 2 \times \frac{0.45}{1.35} = \frac{0.9}{1.35} \approx 0.667 \]
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

\[ F_\beta = (1+\beta^2)\times \frac{\text{Precision}\times\text{Recall}}{(\beta^2\times\text{Precision})+\text{Recall}} \]

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.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →