🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #183

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.

Want to go beyond the notes?

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

Enroll Now — Free Demo Available

F1-Score – FAQs

Quick answers about learning F1-Score in Machine Learning.

This free note from CodingNow 2.0 explains F1-Score in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including F1-Score, is 100% free with no signup required.
With focused practice, most students grasp F1-Score in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now