Precision, Recall and F1

Move the threshold and watch the confusion matrix rearrange itself. One model, and every score in the table changes.

Precision, Recall and F1

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Precision: of the things you flagged, how many were right. It punishes false positives.
Recall: of the things that were there, how many you found. It punishes false negatives.
They trade off. Lower the threshold and recall rises while precision falls; raise it and the reverse.
F1 is their harmonic mean, which stays low unless both are high. An arithmetic mean would not.

Overview

Precision and recall describe different errors in a classifier’s positive predictions. F1 combines them into one score, but it does not remove the need to choose a threshold or decide which errors matter more.

In this guide
  1. Start with the four counts
  2. Move the threshold
  3. When to prioritise each measure
  4. Sources and next steps

Start with the four counts

Predicted positivePredicted negative
Actually positiveTrue positive (TP)False negative (FN)
Actually negativeFalse positive (FP)True negative (TN)

Precision = TP / (TP + FP)

Recall = TP / (TP + FN)

Precision asks: of the cases flagged, how many were truly positive? False positives lower it. Recall asks: of all true positives, how many were found? False negatives lower it.

For example, if a system flags 100 cases and 80 are truly positive, precision is 80%. If there are 200 actual positive cases overall, recall is 80/200 = 40%. The first rate uses predicted positives as its denominator; the second uses actual positives.

Move the threshold

A classifier often assigns each case a score. Lowering the threshold usually labels more cases positive: recall tends to rise, while precision may fall as more false alarms enter the flagged set. Raising the threshold usually makes predictions more selective: precision may rise while recall falls. The exact changes depend on the scores and data.

  1. Set Threshold to 0.10 and observe the positive predictions and both metrics.
  2. Raise it to 0.90. The model scores stay fixed; only the cutoff changes. Compare the additional misses with the reduction in false alarms.

Choose a threshold using validation data, not the test set. A threshold that performs well on one sample can be too optimistic if it was selected using the same data.

When to prioritise each measure

Recall matters when missing a positive case is costly, such as in an initial safety screen. Precision matters when each alert triggers expensive review or disruptive action. Many systems need both, with an explicit minimum for one and an optimisation target for the other.

F1 is the harmonic mean of precision and recall:

F1 = 2 × precision × recall / (precision + recall)

If precision is 1.0 and recall is 0.01, the arithmetic mean is 0.505, but F1 is about 0.020. The harmonic mean stays closer to the smaller value, so F1 is high only when both measures are reasonably high. F1 weights precision and recall equally. Fβ changes that balance, weighting recall more when β is above 1 and precision more when β is below 1.

F1 ignores true negatives and does not encode the real-world cost of errors. Accuracy can also mislead when classes are imbalanced: if only 1% of cases are positive, always predicting negative gives 99% accuracy and zero recall. Review the confusion matrix and class imbalance alongside the metric.

Sources and next steps

See scikit-learn’s classification metrics guide for metric definitions and averaging choices. Continue with threshold tuning to connect these scores to an operating decision.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What happens to precision and recall as the threshold falls?

  2. Why is F1 a harmonic rather than arithmetic mean?

  3. Why is accuracy misleading when 1% of cases are positive?

Cheat sheet

Precision, Recall and F1

Build a confusion matrix, then adjust the threshold and watch precision trade against recall while F1 tries to balance them both.

MACHINE LEARNING · vizlearn.in/machine_learning/precision_recall_and_f1.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.