F1 score

On this page 7

The F1 score is a classification metric equal to the harmonic mean of precision and recall, computed as F1 = 2 * precision * recall / (precision + recall). In model evaluation for artificial intelligence, it summarizes in one number how well a classifier finds the positive cases while avoiding false alarms, and it ranges from 0 (worst) to 1 (perfect).

F1 is popular because accuracy is misleading on imbalanced data. When only 2 percent of transactions are fraudulent, a model that labels everything as legitimate reaches 98 percent accuracy and is useless. F1 ignores true negatives entirely, so it rewards a model only for correctly handling the rare positive class.

How the F1 score is calculated

F1 is built from the four cells of a confusion matrix: true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Precision is TP divided by all predicted positives, and recall is TP divided by all actual positives. F1 combines them, and can also be written as 2TP / (2TP + FP + FN), which shows that true negatives never enter the formula.

The harmonic mean is chosen deliberately. Unlike the arithmetic mean, it is dominated by the smaller of the two values, so a model cannot score well by maximizing one metric and neglecting the other. The table below illustrates the effect.

PrecisionRecallArithmetic meanF1 score
0.900.900.900.90
0.950.500.730.66
1.000.100.550.18
0.500.500.500.50
0.200.950.580.33
Harmonic mean versus arithmetic mean

A classifier that predicts positive for every example has recall of 1.0 and precision equal to the positive class rate. On a dataset with 5 percent positives, that trivial model scores an F1 of about 0.10, which is a useful baseline to beat when reporting results.

Variants: F-beta, macro, micro and weighted F1

F1 is the special case of the F-beta score, (1 + beta^2) * P * R / (beta^2 * P + R), in which precision and recall are weighted equally. Setting beta above 1 favors recall, and setting it below 1 favors precision. F2 is common in screening tasks where missed cases are costly; F0.5 is common where false alarms are expensive.

In multi-class and multi-label problems, per-class F1 values must be averaged, and the averaging method changes the result considerably.

Averaging methodHow it is computedWhen to use it
Macro F1Unweighted mean of per-class F1Every class matters equally, including rare ones
Micro F1F1 from pooled TP, FP and FNOverall performance per example
Weighted F1Per-class F1 weighted by class frequencyReporting that reflects class distribution
Samples F1F1 per example, then averagedMulti-label tasks such as tagging
Averaging methods for multi-class F1

For single-label multi-class classification, micro F1 equals accuracy, which surprises many practitioners. Reports should therefore always state the averaging method, because macro and micro F1 can differ by tens of points on skewed data.

F1 compared with other evaluation metrics

F1 is a threshold-dependent point metric: it evaluates one specific decision threshold. Other metrics summarize behavior across thresholds or include true negatives, and the right choice depends on what the evaluation dataset and the application need.

MetricUses true negativesThreshold-freeBest use
F1 scoreNoNoImbalanced tasks with a fixed decision rule
AccuracyYesNoBalanced classes, equal error costs
PrecisionNoNoCost of false alarms is high
RecallNoNoCost of misses is high
ROC AUCYesYesRanking quality across thresholds
PR AUCNoYesRanking quality on rare positives
Matthews correlationYesNoBalanced single number for binary tasks
F1 versus neighboring metrics

F1 is also widely used beyond classic classifiers. Token-level F1 measures overlap between predicted and reference answers in extractive question answering, and entity-level F1 is the standard score for named entity recognition. These definitions share the formula but count matches differently, so scores from different benchmarks are not directly comparable.

Using F1 in practice

Most classifiers output a probability, and F1 depends on the threshold that converts it into a label. The default of 0.5 is rarely optimal on imbalanced data. Teams typically sweep thresholds on a validation set, pick the one that maximizes F1 or meets a business constraint, and then report F1 on a separate test set to avoid optimistic bias.

SituationRecommended metricReason
Fraud or defect detectionF1 or PR AUCPositives are rare
Medical screeningF2 or recall at fixed precisionMissed cases are costly
Spam filteringF0.5 or precisionBlocking legitimate mail is costly
Many classes of unequal sizeMacro F1 with per-class reportRare classes stay visible
Comparing models before deploymentPR AUC plus F1 at chosen thresholdSeparates ranking from decision
Choosing a metric for the situation

Common mistakes and limitations

  • Ignoring unequal costs. F1 weights false positives and false negatives equally, which rarely matches real costs.
  • Comparing F1 across datasets. The score depends on the positive class rate, so the same model scores differently on different data.
  • Averaging fold F1 values carelessly. The mean of per-fold F1 differs from F1 computed on pooled predictions.
  • Undefined values. When a class is never predicted, precision is undefined, and libraries silently return 0.
  • Choosing the threshold on test data. This inflates the reported score.

F1 also says nothing about calibration. Two models with identical F1 can produce very different probability estimates, which matters when scores feed downstream decisions. For a complete picture, F1 should be reported together with precision, recall and the confusion matrix it was derived from.

The idea in brief

Think of a metal detector on a beach. A good detector finds most of the buried coins and does not beep at every bottle cap. The F1 score rates how well a model does both at once.

If the detector beeps at everything, it finds every coin but wastes your time. If it beeps only when it is certain, it misses most coins. F1 stays high only when both finding and avoiding false alarms are good, which makes it a fair single grade for tasks where the interesting cases are rare.

A practical scenario

A manufacturer trains a vision model to flag defective circuit boards. Only about 3 percent of boards are defective, so a model that passes every board would score 97 percent accuracy while catching nothing.

The team evaluates with F1 instead. At the default threshold of 0.5, precision is 0.92 but recall is only 0.61, giving an F1 of about 0.73. By lowering the threshold to 0.3 on the validation set, recall rises to 0.84 and precision falls to 0.81, which lifts F1 to about 0.82. Because a missed defect costs more than an extra manual inspection, the team finally reports F2 as well and chooses the threshold that keeps recall above 0.85, accepting a modest increase in manual checks.