Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #336

F1 Score

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

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

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\):

\[ F_1 = 2\times\frac{0.774\times0.8}{0.774+0.8} = 2\times\frac{0.619}{1.574} \approx 0.787 \]

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_\beta = (1+\beta^2)\cdot\frac{\text{Precision}\times\text{Recall}}{\beta^2\cdot\text{Precision}+\text{Recall}} \]

\(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.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep 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 →