How do you calculate the F1 score?
F1 combines precision and recall with a harmonic mean. F1 = 2 × precision × recall / (precision + recall), which simplifies to 2TP / (2TP + FP + FN) (Google Machine Learning Crash Course, last updated 12 January 2026). Precision is TP / (TP + FP), the share of flags that were real; recall is TP / (TP + FN), the share of real positives caught.
A worked example: 1,000 insurance claims, 50 of them fraudulent. The model flags 60, and 40 of those are fraud, so TP = 40, FP = 20, FN = 10. Precision is 40 / 60 = 0.67, recall is 40 / 50 = 0.80, and F1 is 80 / (80 + 20 + 10) = 0.73. The harmonic mean sits closer to the weaker of the two numbers, so a model cannot buy a high F1 with one strong metric and one poor one.
F1 score vs accuracy: which should you trust?
Accuracy rewards predicting the majority class and F1 does not, as three models on the same 1,000 claims show:
| Model | Accuracy | Precision | Recall | F1 |
|---|---|---|---|---|
| Flags nothing | 0.95 | undefined | 0.00 | 0.00 |
| Flags everything | 0.05 | 0.05 | 1.00 | 0.10 |
| The model above | 0.97 | 0.67 | 0.80 | 0.73 |
Accuracy rates the useless first model at 95 percent because it gets every legitimate claim right. F1 ignores true negatives, so the majority class cannot inflate it. That is also its blind spot: F1 says nothing about how the model treats the negatives, so read the false positive rate next to it when wrongly flagged customers carry a cost.
What is a good F1 score?
There is no universal threshold; compare against the trivial baseline. A model that flags everything scores 2p / (1 + p), where p is the share of positives: about 0.10 when 5 percent of cases are positive, 0.67 on balanced data. So 0.6 can be a strong result on rare fraud and worse than guessing on a balanced classification task. Published results give a sense of scale: SQuAD reported an F1 of 51.0% for a logistic regression model against 86.8% for humans (Rajpurkar et al., read 16 November 2026). A good F1 beats the trivial baseline on data scored against trusted ground truth. When misses and false alarms cost different amounts, F-beta weights them: in scikit-learn, “beta = 2 makes recall twice as important as precision” (fbeta_score docs, read 16 November 2026).
How does F1 work for multiclass models and in sklearn?
f1_score in scikit-learn 1.9.1 defaults to average='binary', scoring only the positive label (f1_score docs, read 16 November 2026). For more than two classes you choose an average:
average |
How it combines classes | Use it when |
|---|---|---|
macro |
Unweighted mean of per-class F1 | Rare classes matter as much as common ones |
micro |
Global TP, FP and FN counts | You want one overall rate |
weighted |
Mean weighted by each class’s support | You want class frequency reflected |
Macro F1 exposes a model that ignores a rare class; micro F1 hides it. When a class has no predicted or true positives, the default zero_division='warn' returns 0 and raises a warning. Report the per-class scores next to the average, since one number can hide the class that fails.