How to Calculate F-Score: Step-by-Step Guide
- Precision
- Result
- Recall
- Result
- True Positives
- 80 count
- You entered
This guide explains how to calculate the F-score (also called F1-score), a single number that combines precision and recall to measure a classification model’s accuracy. You’ll need the counts of true positives, false positives, and false negatives from your model’s predictions, and the steps below will walk you through the math.
Formula
The F1-score is the harmonic mean of precision and recall:
F1 = 2 × (Precision × Recall) / (Precision + Recall)
Where:
- Precision = True Positives / (True Positives + False Positives) — of all items predicted as positive, how many were actually positive.
- Recall = True Positives / (True Positives + False Negatives) — of all actual positive items, how many were correctly predicted.
In terms of raw counts, the formula simplifies to:
F1 = 2 × TP / (2 × TP + FP + FN)
All counts are unitless (number of observations).
Worked Example
Using the default values: TP = 80, FP = 20, FN = 10.
- Calculate precision: 80 / (80 + 20) = 80 / 100 = 0.8
- Calculate recall: 80 / (80 + 10) = 80 / 90 = 0.8889
- Apply the F1 formula: 2 × (0.8 × 0.8889) / (0.8 + 0.8889) = 2 × 0.7111 / 1.6889 = 1.4222 / 1.6889 = 0.8421
- Round to three decimals: 0.842
The calculator gives the same result.
When to Use the F-Score
Use the F1-score when you need a single metric to compare models, especially when you care about both false positives and false negatives. It’s most useful when classes are imbalanced (e.g., rare disease detection, fraud detection) because accuracy alone can be misleading. For example, if 95% of cases are negative, a model that always predicts negative gets 95% accuracy but an F1-score of 0.
The F1-score ranges from 0 (worst) to 1 (best). A score of 1 means perfect precision and recall. A score of 0.5 could mean either precision or recall is 0.5 with the other at 1, or both are 0.5 — the harmonic mean penalizes extreme imbalance.
Common Mistakes
- Using accuracy instead of counts: You must have the raw counts (TP, FP, FN), not just accuracy percentage.
- Confusing precision and recall: Precision is about false positives; recall is about false negatives. Swap them and the F1 changes.
- Using arithmetic mean instead of harmonic mean: The harmonic mean is lower than the arithmetic mean and is the correct one for F1.
- Rounding too early: Round precision and recall to 2-3 decimals before computing F1 can introduce error. Keep full precision until the final step.
- Forgetting the denominator: The simplified formula 2×TP / (2×TP + FP + FN) is easier and avoids rounding issues.
Tips
- Always double-check your class labels: a true positive is a positive case correctly predicted as positive; a false positive is a negative case predicted as positive; a false negative is a positive case predicted as negative.
- If you have a confusion matrix, TP is the top-left cell (for binary classification), FP is top-right, FN is bottom-left.
- For multi-class problems, compute F1 per class (treating that class as positive) and then average (macro or weighted).
- If you need to compare models, use the F1-score alongside precision and recall to understand trade-offs.
Troubleshooting
F1-score is 0: This happens when TP = 0. Check if your model predicts no positives at all, or if you swapped TP and FP.
F1-score is 1: This means FP = 0 and FN = 0. Perfect prediction. Verify your counts are correct — it’s rare in real data.
F1-score seems too high or too low: Re-check your counts. A common error is using TN (true negatives) in the formula. Only TP, FP, and FN are used.
Different results from other tools: Some tools compute a weighted F1 or use micro vs macro averaging. For binary classification, the formula above is standard.
Frequently Asked Questions
What is the difference between F1-score and accuracy?
Accuracy is (TP + TN) / total. F1 ignores true negatives and focuses on positive class performance. F1 is more informative when classes are imbalanced.
Can F1-score be negative?
No. Precision and recall are between 0 and 1, so their harmonic mean is also between 0 and 1.
What does an F1-score of 0.5 mean?
It means the harmonic mean of precision and recall is 0.5. For example, precision = 0.5 and recall = 0.5, or precision = 0.33 and recall = 1.0.
How do I calculate F1 for multi-class classification?
Compute F1 for each class (treating that class as positive, all others as negative), then average the F1 scores. Use macro-average (simple mean) or weighted average by class support.
Why use harmonic mean instead of arithmetic mean?
The harmonic mean penalizes extreme values. If precision is 1.0 but recall is 0.0, the arithmetic mean is 0.5, but the harmonic mean is 0 — which correctly reflects that the model is useless for finding positives.
Is F1-score always the best metric?
No. If false positives are much costlier than false negatives, you might prefer precision. If false negatives are costlier, prefer recall. F1 balances both equally.
Related
Formula used
in tp | True Positives | count | 80 | 0 in fp | False Positives | count | 20 | 0 in fn | False Negatives | count | 10 | 0 out f1 | F1-Score | - | (2 * tp) / (2 * tp + fp + fn) | 3 out precision | Precision | - | tp / (tp + fp) | 3 out recall | Recall | - | tp / (tp + fn) | 3
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.