The F1 score is the metric people reach for when they need a single number that balances precision and recall. Instead of reporting two figures and arguing about which matters more, F1 combines them into one value between 0 and 1 — and it does so in a way that refuses to reward a model that is good at one while terrible at the other.

The F1 score formula
The F1 score is the harmonic mean of precision and recall. Both inputs come straight from the confusion matrix: precision = TP / (TP + FP) and recall = TP / (TP + FN). For the formal definition, see the Wikipedia F-score article.
Why the harmonic mean instead of a simple average?
This is the key idea. Suppose a model has precision = 1.0 but recall = 0.0 (it makes one perfect prediction and misses everything else). A plain arithmetic average would give a misleadingly cheerful 0.5. The harmonic mean gives 0 — because it is dominated by the smaller value.
A worked F1 score example
Take the disease-test model with precision = 0.78 and recall = 0.90:
F1 = 2 × (0.78 × 0.90) / (0.78 + 0.90) ≈ 0.84. It sits between the two inputs but leans toward the lower one (precision), signalling that false alarms are the model’s weaker area.
When to use the F1 score
- Imbalanced classes — when one class is rare and accuracy is misleading, F1 focuses on the positive class you care about.
- You need one number — for model selection, leaderboards, or hyperparameter tuning.
- False positives and false negatives both matter — F1 assumes they are roughly equally costly.
F-beta: tuning the balance
- β = 1 → the standard F1 (equal weight).
- β = 2 (F2) → weights recall higher; use when misses are costly.
- β = 0.5 (F0.5) → weights precision higher; use when false alarms are costly.
Macro, micro and weighted F1
For multi-class problems you compute F1 per class and then average:
- Macro-F1 — unweighted mean across classes; treats every class equally.
- Weighted-F1 — averages by class frequency; reflects the overall dataset.
- Micro-F1 — pools all TP, FP, FN first; on single-label problems it equals accuracy.
Computing the F1 score in Python
from sklearn.metrics import f1_score
y_true = [1, 0, 1, 1, 0, 1, 0, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0]
print("F1:", f1_score(y_true, y_pred))
print("Macro F1:", f1_score(y_true, y_pred, average="macro"))Frequently asked questions
What is a good F1 score?
Is the F1 score better than accuracy?
Does the F1 score use true negatives?
Related guides
- Precision vs Recall — the two inputs to F1.
- Confusion Matrix — where it all comes from.
- ROC Curve & AUC — the threshold-free alternative.
- Classification Metrics — which metric to use and when.