F1 Score: 1 Balanced Number for Precision and Recall

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.

f1 score harmonic mean chart

📌
In one line. The F1 score is the harmonic mean of precision and recall — high only when both are high, which is why it is the go-to metric for imbalanced classification.

The F1 score formula

F1 = 2 × (Precision × Recall) / (Precision + Recall)  =  2TP / (2TP + FP + FN)

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.

💡
Why it matters. The F1 score is only high when both precision and recall are high; you cannot game it by maxing out one metric. That property is exactly why F1 is trusted on imbalanced problems.

A worked F1 score example

Take the disease-test model with precision = 0.78 and recall = 0.90:

0.78Precision
0.90Recall
0.84F1 Score
0.83Simple average

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.
⚠️
When NOT to use F1. If one error is far costlier than the other, F1’s equal weighting misleads — use F-beta (below) or optimise recall directly. And because F1 ignores true negatives entirely, pair it with ROC-AUC or balanced accuracy when the negative class matters.

F-beta: tuning the balance

= (1 + β²) × (Precision × Recall) / (β² × Precision + Recall)
  • β = 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?
There is no universal cutoff. Compare against a baseline (e.g., predicting the majority class) and the business cost of errors. On hard, imbalanced problems even 0.6–0.7 can be strong.
Is the F1 score better than accuracy?
On imbalanced data, yes — accuracy can look high while the model ignores the rare class, whereas F1 exposes that. On balanced data with equal error costs, accuracy is fine.
Does the F1 score use true negatives?
No. F1 is built from TP, FP and FN only. If negative-class performance matters, pair F1 with ROC-AUC or balanced accuracy.

Related guides

Scroll to Top