F1 Score Calculator

By · Last updated:

F1 Score Calculator Fβ = (1 + β²)PR / (β²P + R)

Start from precision and recall, from TP, FP and FN counts, or from one row of counts per class. You get F1, F2, F0.5 or any Fβ, the macro, weighted and micro averages for multi-class results, and your point on the precision-recall plane with the iso-F1 curves.

Examples:
Next step: One F score describes one decision threshold. The ROC AUC calculator judges the model across every threshold at once. If you also have the true negatives, the confusion matrix calculator turns the same counts into accuracy and specificity.

The F1 score is the harmonic mean of precision and recall, F1 = 2PR / (P + R): a single number from 0 to 1 that is high only when both are high. This F1 score calculator returns F1, F2, F0.5 or any F-beta score from precision and recall or from TP, FP and FN counts, and the macro, micro and weighted F1 for multi-class results.

What is the F1 score?

Precision is the share of predicted positives that are right. Recall is the share of actual positives that were found. A classifier can push either one up at the expense of the other: flag everything and recall reaches 1 while precision collapses, flag only the surest case and the reverse happens. The F1 score folds the two into one figure that rewards a model only when it does both jobs.

This page starts where the confusion matrix calculator stops. That tool covers accuracy, precision, recall and specificity for the binary case; this one covers the weighting parameter β, averaging across classes, and the trade-off between precision and recall.

What is the F-beta score formula?

\[ F_\beta = \frac{(1+\beta^2)\,P\,R}{\beta^2 P + R} = \frac{(1+\beta^2)\,TP}{(1+\beta^2)\,TP + \beta^2\,FN + FP} \]

\(P\) is precision, \(TP / (TP + FP)\). \(R\) is recall, \(TP / (TP + FN)\). \(TP\), \(FP\) and \(FN\) are the true positives, false positives and false negatives. \(\beta\) sets how much recall matters relative to precision: recall is treated as \(\beta\) times as important. With \(\beta = 1\) the formula reduces to the F1 score:

\[ F_1 = \frac{2PR}{P+R} = \frac{2\,TP}{2\,TP + FP + FN} \]

True negatives appear nowhere in either form. That is the point of the metric, and also its blind spot.

Worked example: 70 true positives, 10 false positives, 5 false negatives

These are the calculator’s default inputs. Precision is 70 / (70 + 10) = 0.875 and recall is 70 / (70 + 5) = 0.9333. From the counts, F1 = (2 × 70) / (2 × 70 + 10 + 5) = 140 / 155 = 0.9032.

Changing β moves the score toward whichever of the two it favours. F2 = (5 × 70) / (5 × 70 + 4 × 5 + 10) = 350 / 380 = 0.9211, closer to the recall of 0.9333. F0.5 = (1.25 × 70) / (1.25 × 70 + 0.25 × 5 + 10) = 87.5 / 98.75 = 0.8861, closer to the precision of 0.875.

Why does the harmonic mean punish a gap between precision and recall?

An arithmetic mean lets a strong number cover for a weak one. The harmonic mean does not: it is pulled toward the smaller value, and it is 0 as soon as either input is 0. Every row below has the same arithmetic mean, 0.6, yet F1 falls from 0.6 to 0.3333 as the two numbers separate.

PrecisionRecallArithmetic meanF1F2F0.5
0.60.60.60.60000.60000.6000
0.70.50.60.58330.53030.6481
0.80.40.60.53330.44440.6667
0.90.30.60.45000.34620.6429
1.00.20.60.33330.23810.5556

The plot in the tool shows the same thing as geometry. Each solid curve joins every (recall, precision) pair with the same F1. The curves bend toward the top-right corner, so moving along one axis alone soon stops helping: with recall stuck at 0.2, even perfect precision cannot lift F1 above 0.3333.

When should you use F2 or F0.5 instead of F1?

Move β when the two kinds of error do not cost the same. F2 suits screening tasks, where a missed positive is worse than a false alarm: a first-pass fraud review or a defect check whose flagged items get a second look. In the table, F2 scores the high-precision, low-recall rows far lower than F1 does.

F0.5 suits tasks where a false alarm is the expensive error, such as a spam filter that must not hide real mail or an automated action nobody reviews. It tolerates lower recall as long as what gets flagged is right. Pick β from the cost of the errors before looking at the results, not afterwards to make a model look better.

Macro vs micro vs weighted F1: which average should you report?

With more than two classes there is one F1 per class and three common ways to combine them. Take three labels with these counts (the “Three labels” preset):

ClassTPFPFNPrecisionRecallF1Support
A501050.83330.90910.869655
B305150.85710.66670.750045
C105100.66670.50000.571420
  • Macro F1 is the plain mean: (0.8696 + 0.7500 + 0.5714) / 3 = 0.7303. The small class C counts as much as A, so its weak score drags the average down.
  • Weighted F1 weights each class by its support: (55 × 0.8696 + 45 × 0.7500 + 20 × 0.5714) / 120 = 0.7750.
  • Micro F1 pools the counts first: TP = 90, FP = 20, FN = 30, so 180 / (180 + 20 + 30) = 0.7826. Large classes dominate.

On imbalanced data, report macro F1 when every class matters equally, and micro or weighted F1 when you care about the typical sample. A wide gap between macro and micro is itself a finding: the model is weak on a minority class.

In single-label classification, where each sample has one true class and one predicted class, every mistake is a false positive for one class and a false negative for another. Total FP then equals total FN, and micro F1 equals accuracy. The “Single-label 3-class” preset shows it: 90 correct out of 114 gives micro F1 = accuracy = 0.7895. The three-label table above has 20 false positives against 30 false negatives, so it can only come from multi-label data, where the identity does not hold.

Why does accuracy mislead on rare classes?

Suppose 10 of 1,000 samples are positive. A model that predicts “negative” every time is 99% accurate and finds nothing; its F1 is 0. A model with 2 true positives, 3 false positives and 8 false negatives is still 98.9% accurate, but its precision is 0.4, its recall is 0.2 and its F1 is 0.2667. Accuracy is propped up by the 987 true negatives, which the F score leaves out. Precision here is a conditional probability, P(actually positive | predicted positive), and it is the large negative class that drags it down.

Where is the F score used in machine learning?

  • Choosing a threshold. A classifier that ends in a sigmoid output gives a probability, and each cut-off gives a different (recall, precision) point. Sweeping the cut-off and keeping the one with the best F1 or F2 is a standard tuning step.
  • Model selection. scikit-learn accepts scoring="f1_macro" in cross-validation and grid search, so hyperparameters can be chosen on the metric you will report.
  • Evaluation, not training. F1 is built from counts, so it has no useful gradient. Models are trained on a smooth loss such as cross-entropy and judged on F1 afterwards.
  • Information retrieval and tagging. Search relevance, named-entity recognition and multi-label tagging are usually reported with micro or macro F1, because true negatives are either undefined or overwhelmingly common.

The other tools for scoring a classifier are on the model evaluation calculators page.

Common mistakes

  • Averaging precision and recall arithmetically. (P + R) / 2 is not F1 and is always at least as large.
  • Computing macro F1 from averaged precision and recall. Macro F1 is the mean of the per-class F1 scores. Averaging precision and recall first and then applying the formula gives a different number.
  • Not saying which average was used. “F1 = 0.78” on a multi-class problem is ambiguous. Macro, micro and weighted gave 0.7303, 0.7826 and 0.7750 on the same counts above.
  • Reading 0/0 as a real zero. A class that is never predicted has undefined precision. scikit-learn and this calculator report 0, which lowers macro F1. Check whether that class had any samples.
  • Scoring the wrong class. Binary F1 describes the class you call positive. Swap the labels and the score changes.
  • Comparing F1 across datasets with different class balance. Precision depends on prevalence, so F1 does too.

How do you calculate F1 in Python with scikit-learn?

sklearn.metrics.f1_score takes true and predicted labels and an average argument; fbeta_score adds beta. The first block rebuilds the worked example and the second the single-label preset.

import numpy as np
from sklearn.metrics import f1_score, fbeta_score, precision_score, recall_score

# 70 TP, 10 FP, 5 FN and 15 TN (the TN count does not affect any F score)
y_true = np.array([1] * 70 + [0] * 10 + [1] * 5 + [0] * 15)
y_pred = np.array([1] * 70 + [1] * 10 + [0] * 5 + [0] * 15)

print(round(precision_score(y_true, y_pred), 6))        # 0.875
print(round(recall_score(y_true, y_pred), 6))           # 0.933333
print(round(f1_score(y_true, y_pred), 6))               # 0.903226
print(round(fbeta_score(y_true, y_pred, beta=2), 6))    # 0.921053
print(round(fbeta_score(y_true, y_pred, beta=0.5), 6))  # 0.886076

# Single-label, 3 classes. Rows are the true class, columns the predicted class.
cm = np.array([[50, 3, 2],
               [8, 30, 7],
               [2, 2, 10]])
y_true = np.repeat(np.arange(3), cm.sum(axis=1))
y_pred = np.concatenate([np.repeat(np.arange(3), row) for row in cm])

print(f1_score(y_true, y_pred, average=None).round(6))         # [0.869565 0.75 0.606061]
print(round(f1_score(y_true, y_pred, average="macro"), 6))     # 0.741875
print(round(f1_score(y_true, y_pred, average="weighted"), 6))  # 0.790008
print(round(f1_score(y_true, y_pred, average="micro"), 6))     # 0.789474, the accuracy

Core PyTorch has no F1 function; the usual route is the separate torchmetrics package (MulticlassF1Score), and Keras provides keras.metrics.F1Score and keras.metrics.FBetaScore. Both take an average argument with the same meaning.

Frequently asked questions

What is the difference between macro, micro and weighted F1?

Macro F1 is the plain mean of the per-class F1 scores, so every class counts equally. Weighted F1 is the same mean with each class weighted by its support, the number of true samples in that class. Micro F1 adds up TP, FP and FN over all classes first and computes one F1 from the totals, so every sample counts equally.

Is micro F1 the same as accuracy?

Yes, when each sample has exactly one true class and exactly one predicted class and every class is included. Each mistake is then one false positive and one false negative, so micro precision, micro recall, micro F1 and accuracy are all the same number. In multi-label problems they differ.

When should I use F2 or F0.5 instead of F1?

Use F2 when a missed positive costs more than a false alarm, as in screening or fraud review, because beta = 2 weights recall more heavily. Use F0.5 when false alarms cost more, as in a spam filter that must not block real mail, because beta = 0.5 weights precision more heavily.

What is the F1 score when precision and recall are both 0?

The formula becomes 0 divided by 0, which has no value. scikit-learn returns 0 by default and raises a warning, and this calculator does the same and shows a note. The zero_division argument of f1_score lets you choose a different value.

Related calculators

An F score is tied to one decision threshold. To compare models without fixing a threshold, use the ROC curve and AUC calculator; to see the four cells behind precision and recall, go back to the binary confusion matrix.

More tools that pair with this one: