๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #147

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.

Want to go beyond the notes?

Join CodingNow 2.0's Deep 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 Deep Learning.

This free note from CodingNow 2.0 explains F1 Score in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep 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