F1 Score and Classification Metrics Calculator

Enter the four cells of a confusion matrix and this calculator returns every headline metric a model review asks for: precision, recall, F1, F-beta at any β you choose, specificity, accuracy, balanced accuracy and the Matthews correlation coefficient. It shows the arithmetic step by step and sweeps β so you can see how much the score moves when you decide that a missed positive costs more than a false alarm. Where a metric is genuinely undefined — no predicted positives, no actual positives — it says so instead of quietly substituting zero.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
True positives (TP)Cases that are positive and were predicted positive.90
False positives (FP)Cases that are negative but were predicted positive — the false alarms.10
False negatives (FN)Cases that are positive but were predicted negative — the misses.30
True negatives (TN)Cases that are negative and were predicted negative.870
β for the F-beta scoreSet β = 1 for plain F1; β = 2 if a miss costs about four times a false alarm; β = 0.5 for the reverse.1

It returns

  • F1 score — The harmonic mean of precision and recall — high only when both are high.
  • Precision
  • Recall (sensitivity, TPR)
  • F-beta score
  • Specificity (TNR)
  • Accuracy
  • Balanced accuracy
  • Matthews correlation coefficient — Ranges from −1 (perfectly wrong) through 0 (chance) to +1 (perfect).

The formula

F1=2PRP+R
Fβ=(1+β2)PRβ2P+R
MCC=TPTNFPFN(TP+FP)(TP+FN)(TN+FP)(TN+FN)
F1=2TP2TP+FP+FN

In plain text: F1 = 2·P·R / (P + R)

  • PPrecision — TP ÷ (TP + FP), the share of positive predictions that were right (0–1)
  • RRecall — TP ÷ (TP + FN), the share of actual positives that were found (0–1)
  • βWeight given to recall relative to precision in the F-beta score (ratio)
  • TP, FP, FN, TNThe four cells of the confusion matrix (counts)

F1 is the harmonic mean of precision and recall, which is why it is dragged down by the smaller of the two rather than averaging them out. It ignores true negatives entirely.

Updated Category Model Evaluation & Metrics Verified against published test cases Reading time 13 min

What F1 measures and why accuracy is not enough

F1 is the harmonic mean of precision and recall. Precision asks: of the cases the model flagged, how many were really positive? Recall asks: of the cases that were really positive, how many did the model flag? A model can max out either one on its own — predict positive exactly once and be right to get precision 1.0, predict positive on everything to get recall 1.0 — so neither is a score by itself. F1 combines them in a way that only rewards doing both.

The reason to use a harmonic mean rather than a plain average is that the harmonic mean is pulled toward the smaller number. Precision 1.0 with recall 0.02 averages to 0.51 but gives an F1 of 0.039. That is the behaviour you want from a single summary figure: it should not let a model hide a catastrophic weakness behind a strength.

Accuracy fails on imbalanced data for a related reason. If 1% of your cases are positive, a model that predicts “negative” unconditionally scores 99% accuracy, and it is worthless. F1 ignores the true-negative cell entirely, so that model scores zero. This is the single most common reason a review asks for F1 rather than accuracy, and it is why fraud, defect, churn and disease-screening problems are always reported with precision and recall side by side.

The formulas, and what each one is blind to

Everything comes out of four counts. Precision is TP ÷ (TP + FP): the denominator is everything the model called positive. Recall is TP ÷ (TP + FN): the denominator is everything that actually was positive. Specificity is TN ÷ (TN + FP), which is recall for the negative class. Accuracy is (TP + TN) ÷ N.

F1 written out in cells is 2·TP ÷ (2·TP + FP + FN), which makes its one blind spot obvious: TN never appears. Add a million true negatives to your test set and F1 does not move. That is a feature when the negative class is uninteresting background, and a bug when getting the negatives right is part of the job.

F-beta generalises F1 by weighting: Fβ = (1 + β²)·P·R ÷ (β²·P + R). The parameter is defined so that β is the number of times you consider recall as important as precision — so β = 2 weights recall four times as heavily in the algebra, because the weight enters as β². Use β > 1 when a miss is expensive (a missed tumour, a missed intrusion) and β < 1 when a false alarm is expensive (a wrongly blocked payment, an auto-flagged account).

Balanced accuracy is (recall + specificity) ÷ 2, the average of the two class-wise recalls. It corrects accuracy for imbalance but says nothing about precision, so a model with a huge false-positive count can still score well when negatives are plentiful. The Matthews correlation coefficient is the only one of these that uses all four cells symmetrically; it is the Pearson correlation between the predicted and true label vectors, which is why it runs from −1 to +1 with 0 at chance rather than from 0 to 1.

Worked example: 1,000 transactions, 120 of them fraudulent

A fraud model is evaluated on 1,000 transactions. It flags 100, of which 90 are genuinely fraudulent. There were 120 frauds in total. That fixes all four cells: TP = 90, FP = 10, FN = 120 − 90 = 30, TN = 1,000 − 90 − 10 − 30 = 870.

  1. Precision. 90 ÷ (90 + 10) = 90 ÷ 100 = 0.9000. Nine out of ten flags were real.
  2. Recall. 90 ÷ (90 + 30) = 90 ÷ 120 = 0.7500. Three quarters of the fraud was caught.
  3. F1. 2 × 0.9 × 0.75 = 1.35 on top; 0.9 + 0.75 = 1.65 underneath; 1.35 ÷ 1.65 = 0.8182. Note it sits below the arithmetic mean of 0.825, pulled toward the weaker recall.
  4. Accuracy. (90 + 870) ÷ 1,000 = 0.9600 — much friendlier than F1, because the 870 true negatives dominate a count that F1 never sees.
  5. Specificity. 870 ÷ (870 + 10) = 870 ÷ 880 = 0.9886.
  6. Balanced accuracy. (0.75 + 0.9886) ÷ 2 = 0.8693.
  7. MCC. The numerator is 90×870 − 10×30 = 78,300 − 300 = 78,000. The denominator is √(100 × 120 × 880 × 900) = √9,504,000,000 = 97,488.46. So MCC = 78,000 ÷ 97,488.46 = 0.8001.

Now suppose the business decides a missed fraud costs about four times a false alarm. That is β = 2: F₂ = 5 × 0.9 × 0.75 ÷ (4 × 0.9 + 0.75) = 3.375 ÷ 4.35 = 0.7759. The score falls relative to F1 because the model’s weaker side is recall, and β = 2 puts more weight there.

How to read the numbers you get back

There is no universal threshold for a “good” F1, because the value depends on the base rate and the difficulty of the task as much as on the model. An F1 of 0.55 can be excellent on a 0.5%-prevalence fraud problem and terrible on balanced sentiment classification. The only interpretations that transfer are relative ones: against a baseline, against the previous model, and against the cost structure of the decision.

Build the baseline explicitly. For a set with prevalence p, a classifier that flags everything gets precision p, recall 1, and F1 = 2p ÷ (1 + p). At 10% prevalence that is 0.182 — so an F1 of 0.25 on such a set is barely better than predicting positive at random with no model at all. Quote your F1 next to that number and the reader can judge it.

Read precision and recall separately before you read F1, because two very different models can share an F1. Precision 0.9 with recall 0.75 and precision 0.75 with recall 0.9 both give 0.8182, and they behave nothing alike in production: one under-flags, the other over-flags. F1 is a ranking tool for model selection, not a description of behaviour.

Use MCC when you want one number that cannot be gamed by class imbalance. Because it uses all four cells, a model that gets a good F1 by carpet-bombing the positive class shows up immediately: in the rare-positive row of the table below, F1 is 0.18 and MCC is 0.20 while accuracy reads a comfortable 0.91. A negative MCC is a specific, actionable signal — the predictions are anti-correlated with the labels, and the usual cause is an inverted label mapping rather than a bad model.

The same metrics on five different confusion matrices

Every row is computed from its four cells with the formulas above. Watch how far accuracy can drift from F1 and MCC.
TP / FP / FN / TNPrecisionRecallF1AccuracyBalanced acc.MCC
90 / 10 / 30 / 8700.90000.75000.81820.96000.86930.8001
18 / 2 / 2 / 780.90000.90000.90000.96000.93750.8750
50 / 0 / 50 / 9001.00000.50000.66670.95000.75000.6882
1 / 1 / 8 / 900.50000.11110.18180.91000.55000.2047
0 / 0 / 20 / 80undefined0.0000undefined0.80000.5000undefined

Rows one and two share an accuracy of 0.9600 and differ by 0.08 in F1. The last row is the degenerate case every metric except accuracy and specificity refuses to score: with no predicted positives, three of the four MCC marginals collapse and precision is 0 ÷ 0.

Macro, micro and weighted F1 for more than two classes

With more than two classes you compute the metrics one class at a time — treating that class as positive and everything else as negative — and then aggregate. The aggregation choice changes the answer substantially, so it belongs in the report alongside the number.

Macro F1 is the unweighted mean of the per-class F1 scores. Every class counts equally regardless of size, so a small class you handle badly drags the score down hard. Micro F1 pools the cells first — sum TP, FP and FN across classes, then compute one F1 — so large classes dominate. Weighted F1 averages the per-class scores using class support as weights, landing between the two.

Work a three-class example. Class A has TP 50, FP 10, FN 5; class B has TP 30, FP 20, FN 10; class C has TP 5, FP 5, FN 20. Using F1 = 2·TP ÷ (2·TP + FP + FN): class A is 100 ÷ 115 = 0.8696, class B is 60 ÷ 90 = 0.6667, class C is 10 ÷ 35 = 0.2857. Macro F1 is (0.8696 + 0.6667 + 0.2857) ÷ 3 = 0.6073.

Micro pools instead: TP = 85, FP = 35, FN = 35, so micro precision = micro recall = 85 ÷ 120 = 0.7083 and micro F1 = 0.7083. In single-label multiclass every mistake is simultaneously one class’s false positive and another class’s false negative, so the FP and FN totals are always equal and micro F1 equals plain accuracy — here 85 correct out of 120 predictions. The 0.10 gap between macro and micro is entirely the story of class C: 25 cases, handled at 0.29, given a third of the weight under macro and a fifth under micro.

Mistakes that make a reported F1 meaningless

  • Quoting F1 without the base rate. The same number means different things at 1% and 50% prevalence. Report prevalence and the all-positive baseline of 2p/(1+p) alongside it.
  • Tuning the threshold on the test set. F1 depends on the decision threshold, so picking the threshold that maximises F1 on the same data you report it from is leakage. Tune on validation, report on test.
  • Averaging F1 across folds. The mean of per-fold F1 scores is not the F1 of the pooled predictions, because F1 is not linear in the cells. Pool the confusion matrices, then compute once.
  • Silently treating undefined as zero. When a model predicts no positives, precision is 0 ÷ 0. Most libraries substitute 0 and emit a warning; if that warning is suppressed, a broken run looks like a merely bad one.
  • Comparing macro F1 from one paper with micro F1 from another. They answer different questions and can differ by ten points on the same predictions, as the three-class example above shows.
  • Using F1 when true negatives matter. F1 cannot see the TN cell at all. If correctly clearing negatives is part of the product’s value, quote balanced accuracy or MCC instead.
  • Reporting a single point when you have scores. If the model outputs probabilities, threshold-free summaries such as average precision or the area under the precision-recall curve carry far more information than F1 at one arbitrary cut.

Medicine got here first and uses different names for the same arithmetic. Recall is sensitivity and specificity is the negative-class recall, both handled by the sensitivity and specificity calculator; precision is the positive predictive value computed by the PPV and NPV calculator. The clinical literature prefers likelihood ratios precisely because, unlike precision and F1, they do not change when the prevalence of the condition changes — a property worth having when the same model is deployed on populations with different base rates.

If you are comparing two models rather than describing one, remember that a difference in F1 on a finite test set is an estimate with uncertainty attached, and the same significance machinery used for an A/B test applies to the comparison. And if the classifier in question is a neural network you are about to deploy, the metrics here answer whether it is good enough while the GPU memory calculator and the parameter count calculator answer whether you can afford to serve it.

The F-measure itself is old: van Rijsbergen introduced the effectiveness measure that F is derived from in Information Retrieval in 1979, in the context of ranking documents, where the true-negative cell is meaningless because it counts every document in the collection that the query correctly ignored. That origin explains the metric’s one blind spot better than any later justification does.

Frequently asked questions

What is a good F1 score?

There is no fixed threshold, because F1 depends on how rare the positive class is and how separable the classes are. Judge it against two references instead: the F1 of a trivial model that flags everything, which is 2p/(1+p) at prevalence p, and the F1 of whatever you are currently running. On a balanced, well-separated problem anything below 0.8 usually signals a real weakness; on a 1%-prevalence fraud problem an F1 of 0.4 can be a strong result.

What is the difference between F1 and accuracy?

Accuracy counts every correct prediction, including true negatives; F1 ignores true negatives completely. On the worked example on this page the same model scores 0.9600 accuracy and 0.8182 F1, and the gap is entirely the 870 true negatives that flatter one metric and are invisible to the other. Use accuracy when the classes are balanced and both kinds of correctness matter equally; use F1 when the positive class is rare and finding it is the job.

Should I use F1 or the Matthews correlation coefficient?

Use MCC when you want a single figure that cannot be inflated by class imbalance, and F1 when the negative class is background you do not care about scoring. MCC uses all four cells and is the correlation between predictions and labels, so it runs from −1 to +1 with 0 at chance; F1 uses three cells and runs from 0 to 1 with no defined chance level. Reporting both costs nothing and is common practice in the methodological literature.

How do I choose β for the F-beta score?

Set β to the number of times a missed positive costs more than a false alarm, then check the result against your own cost table. β = 2 is the usual choice when misses are roughly four times as expensive as false alarms, because the weight enters the formula as β²; β = 0.5 reverses the emphasis. If you can quantify the two costs in money, skip F-beta and optimise expected cost directly — F-beta is a proxy for a cost ratio you could not measure.

Why is my precision undefined instead of zero?

Because the model made no positive predictions at all, so precision is 0 ÷ 0 rather than a number that happens to be small. Scikit-learn substitutes 0 and raises an UndefinedMetricWarning; this calculator reports it as undefined so the degenerate case is visible. Either way the diagnosis is the same: the decision threshold is above every score the model produced, or the model has collapsed to the majority class.

What is the difference between macro and micro F1?

Macro F1 averages the per-class F1 scores with equal weight, so small classes count as much as large ones; micro F1 pools the cells across classes first, so large classes dominate. On the three-class example in this article macro F1 is 0.6073 and micro F1 is 0.7083 on identical predictions. In single-label multiclass, micro F1 equals accuracy, because every error contributes one false positive and one false negative.

Can F1 be higher than both precision and recall?

No. The harmonic mean of two positive numbers always lies between them, and equals them only when they are equal. That is exactly why F1 is used: it cannot exceed the weaker of the two, so a model cannot buy a good score by pushing one component to 1.0. If your F1 comes out above both, the arithmetic is wrong — the usual cause is dividing by (P + R) after already halving one of them.

Does F1 depend on the decision threshold?

Yes, entirely. Every cell of the confusion matrix is a function of where you cut the model’s score, so F1 describes one operating point rather than the model. Lowering the threshold raises recall and lowers precision; the F1-maximising cut is usually not 0.5. If you want a threshold-free summary, use average precision or the area under the precision-recall curve, and report F1 at the threshold you will actually deploy.

How do I compute these metrics for an imbalanced dataset?

The formulas do not change; what changes is which of them you quote. Report prevalence first, then precision, recall and F1 for the rare class, then MCC. Do not quote accuracy without the all-negative baseline beside it, which is simply the share of negatives in the set — the calculator prints that number in a warning whenever prevalence falls below 5%. Resampling the training set to fix imbalance is fine, but never resample the evaluation set, or every metric you report will describe a population that does not exist.

References

  • Information Retrieval, 2nd edition (the effectiveness measure behind the F-score) — C. J. van Rijsbergen, Butterworths, 1979
  • Comparison of the predicted and observed secondary structure of T4 phage lysozyme (original MCC paper) — B. W. Matthews, Biochimica et Biophysica Acta, 1975
  • The advantages of the Matthews correlation coefficient (MCC) over F1 score and accuracy in binary classification evaluation — D. Chicco & G. Jurman, BMC Genomics 21:6, 2020
  • scikit-learn user guide, Metrics and scoringscikit-learn developers