
The F1 score is the harmonic mean of precision and recall — F1 = 2 × (Precision × Recall) / (Precision + Recall) — and it ranges from 0 (total failure) to 1 (perfect classification). It’s the metric I reach for first on any imbalanced classification problem, because accuracy alone will happily tell you a fraud model is “99% accurate” while it misses every actual fraud case.
The Formula, Broken Down
F1 combines two metrics that each tell only half the story:
- Precision = TP / (TP + FP) — of everything the model flagged as positive, how much was actually positive?
- Recall = TP / (TP + FN) — of everything that was actually positive, how much did the model catch?
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * (precision * recall) / (precision + recall)
F1 uses the harmonic mean rather than a simple average specifically because it punishes imbalance between precision and recall much more harshly. A model with 0.95 precision and 0.05 recall averages to 0.50 arithmetically, but its F1 score collapses to about 0.095 — which is a far more honest reflection of a model that’s essentially useless despite the high precision number.
Worked Example
Say a model returns TP=80, FP=20, FN=40 on a validation set:
tp, fp, fn = 80, 20, 40
precision = tp / (tp + fp) # 0.80
recall = tp / (tp + fn) # 0.667
f1 = 2 * (precision * recall) / (precision + recall)
print(round(f1, 2)) # 0.73
In scikit-learn, you’d never hand-compute this in production — I use f1_score directly against predictions:
from sklearn.metrics import f1_score, classification_report
f1 = f1_score(y_true, y_pred)
print(classification_report(y_true, y_pred))
classification_report is the single function I run immediately after every model fit — it prints precision, recall, and F1 per class in one call, which catches class-level problems that a single aggregate accuracy number hides completely.
How to Interpret the Score
| F1 Score | Interpretation |
|---|---|
| 0.0 | Complete failure — model catches nothing correctly |
| ~0.5 | Weak — both precision and recall are modest |
| ~0.7 | Acceptable for most general applications |
| 0.8+ | Strong performance |
| 1.0 | Perfect — rare outside of toy datasets |
These bands are a starting point, not a law. What counts as “good” depends heavily on the domain: I hold medical diagnosis models to a much higher bar — typically above 0.85 — than a social media auto-tagging feature, where an F1 around 0.60 might be perfectly acceptable given the low cost of an occasional wrong tag.
F1 vs. Precision vs. Recall: When to Use Which
| Metric | Optimizes for | Best used when |
|---|---|---|
| Precision | Fewer false alarms | False positives are costly — e.g. spam filters flagging real email |
| Recall | Fewer missed cases | False negatives are costly — e.g. missing a cancer diagnosis |
| F1 Score | Balance of both | Imbalanced datasets, or when both error types carry real cost |
On a fraud detection project I worked on, the finance team initially wanted us to optimize for recall alone — catch every fraud case. Pushed to its logical extreme, that model flagged nearly a third of legitimate transactions as fraudulent. Switching the target metric to F1 forced a realistic tradeoff conversation and produced a model the operations team could actually work with.
Why F1 Beats Accuracy on Imbalanced Data
Imagine a dataset where only 2% of transactions are fraudulent. A model that predicts “not fraud” for every single transaction scores 98% accuracy while catching zero fraud. Its F1 score, by contrast, is 0 — because recall is 0. This is precisely why F1 is the standard metric in fraud detection, medical diagnosis, and rare-event classification: accuracy rewards the lazy majority-class prediction; F1 does not.
Frequently Asked Questions
What is a good F1 score?
Above 0.7 is generally considered acceptable for most applications, and above 0.8 is strong. But the right threshold is domain-specific — high-stakes fields like medical diagnosis often require 0.85 or higher, while lower-stakes classification tasks can tolerate scores around 0.6.
Why use F1 instead of accuracy?
Accuracy is misleading on imbalanced datasets because a model can score high just by predicting the majority class every time. F1 accounts for both false positives and false negatives, making it far more informative when classes are unevenly distributed.
What is the F1 score range?
F1 ranges from 0 to 1, where 0 means the model fails completely (zero precision or zero recall) and 1 means perfect precision and recall simultaneously.
Related Reading
Model evaluation only matters once you’re clear on what kind of problem you’re solving — see our comparison of AI vs. machine learning vs. data science. For a real industry example where classification models are deployed at scale, read our piece on machine learning in logistics.
Elizabeth Sramek is a data scientist at Automatic Statistician, where she works on automated statistical modeling, data visualization, and applied machine learning workflows.
