Search

Search papers and lessons

Precision and Recall

Level 2 · 10 min read

Precision is how many of the things you flagged were right; recall is how many of the right things you managed to flag. Accuracy alone hides the difference.

On this page

  • Colour = topic mix
  • Probability
  • Learning basics
  • Sign in to see your progress

TL;DR

A classifier answers yes or no for each case. Precision asks: of the cases it said yes to, how many really were yes? Recall asks: of the cases that really were yes, how many did it find? The two pull against each other, because flagging more cases usually finds more of the real ones but also drags in more mistakes.

Where you'll see it: judging spam filters, medical screening tests, fraud alerts, search results, and any model whose positive class is rare.

Intuition

Accuracy is the most obvious score for a classifier: the fraction of answers that were correct. It is also easy to fool. Suppose only 1 email in 100 is spam. A filter that marks every email as "not spam" is 99% accurate and completely useless. Accuracy counts all the easy correct answers in the large majority class, and it says nothing about the rare class you actually care about.

Precision and recall look only at the positive class, the one you are trying to catch. Precision punishes false alarms. Recall punishes misses. Each is blind to the other's failure, so you need both.

A useful picture is a sieve with two jobs. A fine mesh keeps almost everything the sieve should keep, but it also lets through little stones that do not belong: that is high recall with low precision. A coarse mesh keeps only clean, obvious items, but it drops good items too: that is high precision with low recall. You choose the mesh size, and the choice depends on which mistake costs more.

All the counts come from a confusion matrix. Every prediction falls into one of four cells:

flowchart TD
  A["All predictions"] --> B["Predicted positive"]
  A --> C["Predicted negative"]
  B --> D["True positives TP"]
  B --> E["False positives FP"]
  C --> F["False negatives FN"]
  C --> G["True negatives TN"]

Precision uses the predicted-positive branch (TPTP and FPFP). Recall uses the cases that really are positive (TPTP and FNFN).

The math

Definitions

Definition: Precision and recall

Let TPTP be the number of true positives (positive cases predicted positive), FPFP the number of false positives (negative cases predicted positive), and FNFN the number of false negatives (positive cases predicted negative). Then

precision=TPTP+FP,recall=TPTP+FN.\text{precision} = \frac{TP}{TP + FP}, \qquad \text{recall} = \frac{TP}{TP + FN}.

Here TP+FPTP + FP is the total number of positive predictions, and TP+FNTP + FN is the total number of real positives. So precision is a fraction of the predictions, and recall is a fraction of the real cases.

Precision is a conditional probability, which is why it is worth reading the conditional probability lesson first. If you write Y=1Y = 1 for "really positive" and Y^=1\hat{Y} = 1 for "predicted positive", then

precision=P(Y=1∣Y^=1),recall=P(Y^=1∣Y=1).\text{precision} = P(Y = 1 \mid \hat{Y} = 1), \qquad \text{recall} = P(\hat{Y} = 1 \mid Y = 1).

Read the vertical bar as "given that". Precision conditions on the prediction: once the model says yes, how likely is it to be right? Recall conditions on the truth: once the case really is positive, how likely is the model to say yes? Swapping the two gives a different number, which is the most common source of confusion here.

The F1 score

A single number is often wanted for comparing models. The F1 score is the harmonic mean of precision PP and recall RR:

F1=2PRP+R=2 TP2 TP+FP+FN.F_1 = \frac{2PR}{P + R} = \frac{2\,TP}{2\,TP + FP + FN}.

The harmonic mean is pulled toward the smaller value. A model with precision 1.0 and recall 0.1 gets an F1 of about 0.18, not the 0.55 an ordinary average would give. F1 is low unless both numbers are reasonable.

The threshold trade-off

Most classifiers output a score, such as a probability, and a threshold tt turns the score into a yes or no: predict positive when score ≥t\ge t. Raising tt makes the model stricter, so FPFP goes down and FNFN goes up. Lowering tt does the reverse. Precision and recall therefore move in opposite directions as you slide the threshold. Only the threshold changes; the model does not.

Worked examples

Example
A screening test with 200 patients

A test is run on 200 patients. 20 of them really have the condition. The test returns these counts:

  • true positives TP=18TP = 18 (sick, flagged)
  • false negatives FN=2FN = 2 (sick, missed)
  • false positives FP=12FP = 12 (healthy, flagged)
  • true negatives TN=168TN = 168 (healthy, cleared)

Check the total: 18+2+12+168=20018 + 2 + 12 + 168 = 200.

  1. Accuracy is (TP+TN)/200=(18+168)/200=186/200=0.93(TP + TN) / 200 = (18 + 168) / 200 = 186 / 200 = 0.93.
  2. Precision is TP/(TP+FP)=18/(18+12)=18/30=0.60TP / (TP + FP) = 18 / (18 + 12) = 18 / 30 = 0.60.
  3. Recall is TP/(TP+FN)=18/(18+2)=18/20=0.90TP / (TP + FN) = 18 / (18 + 2) = 18 / 20 = 0.90.
  4. F1 is 2×0.60×0.90/(0.60+0.90)=1.08/1.50=0.722 \times 0.60 \times 0.90 / (0.60 + 0.90) = 1.08 / 1.50 = 0.72.
  5. Interpretation: 93% of answers are correct, yet only 60% of positive results are real. The test finds 9 in 10 sick patients, which is good for a screening test. Most of the cost is in the 12 healthy people who were flagged and will need follow-up.
Example
Moving the threshold on a spam filter

Ten emails have been scored by a spam model. Four are spam. Scores, from highest to lowest, with the true label in brackets:

0.95 (spam), 0.80 (spam), 0.70 (not spam), 0.60 (spam), 0.40 (not spam), 0.30 (spam), 0.20, 0.15, 0.10, 0.05 (not spam).

Take three thresholds. Predict spam when the score is at least tt.

  1. At t=0.65t = 0.65: flagged are 0.95, 0.80, 0.70. Of these, 2 are spam and 1 is not, so TP=2TP = 2, FP=1FP = 1, and FN=2FN = 2 (the spam at 0.60 and 0.30 were missed). Precision =2/3≈0.67= 2/3 \approx 0.67. Recall =2/4=0.50= 2/4 = 0.50.
  2. At t=0.50t = 0.50: flagged are 0.95, 0.80, 0.70, 0.60. Now TP=3TP = 3, FP=1FP = 1, FN=1FN = 1. Precision =3/4=0.75= 3/4 = 0.75. Recall =3/4=0.75= 3/4 = 0.75.
  3. At t=0.25t = 0.25: flagged are the top six. TP=4TP = 4, FP=2FP = 2 (the 0.70 and the 0.40), FN=0FN = 0. Precision =4/6≈0.67= 4/6 \approx 0.67. Recall =4/4=1.00= 4/4 = 1.00.
  4. Interpretation: the middle threshold balances the two numbers. The lowest threshold catches everything but lets through two legitimate emails. A spam filter that sends real mail to the junk folder is usually considered worse than one that lets some spam through, so a team might pick a higher threshold even at lower recall.

Tradeoffs and failure modes

Precision and recall are the right tools when the positive class is rare or when the two kinds of error have different costs. Accuracy is still fine when classes are balanced and errors cost about the same.

SituationProblemBetter choice
Rare positive class (fraud, disease)Accuracy looks high for a useless modelReport precision, recall and F1 together
Missing a positive is very costly (cancer screening)Precision-first tuning misses casesFavour recall; set a lower threshold and review flags
A false alarm is very costly (blocking a real payment)Recall-first tuning floods reviewersFavour precision; set a higher threshold
Need one number to compare modelsPrecision or recall alone hides the otherUse F1, or a weighted FβF_\beta if one side matters more
Scores from different thresholds to compare modelsOne threshold gives one point, not a curveLook at the precision-recall curve across all thresholds

Some failure modes to watch for:

  • Choosing the threshold on the test set. The best threshold on the data you tuned on is optimistic. Pick it on a validation set, then report once on the test set.
  • Ignoring base rates. A precision of 0.6 means something very different when 50% of cases are positive than when 0.5% are. Precision moves with the base rate, recall does not.
  • Undefined precision. If a model never predicts positive, TP+FP=0TP + FP = 0 and precision is undefined. Report it as zero or as not applicable, and say which.

History

Precision and recall come from information retrieval, the field that studies how search systems find documents. In the Cranfield experiments of the late 1950s and 1960s, Cyril Cleverdon and colleagues compared indexing systems by how many relevant documents a search returned and how many of the returned documents were relevant. Those two measures became the standard pair. C. J. van Rijsbergen's 1979 book Information Retrieval defined the F-measure as a single combined score. The same counts appear in medicine as sensitivity and positive predictive value, and machine learning adopted the vocabulary for rare-event problems where accuracy is misleading.

Check yourself

QuestionA model has TP = 30, FP = 10, FN = 20, TN = 940. What are its precision, recall and accuracy?Show answer

Precision =30/(30+10)=0.75= 30 / (30 + 10) = 0.75. Recall =30/(30+20)=0.60= 30 / (30 + 20) = 0.60. Accuracy =(30+940)/1000=0.97= (30 + 940) / 1000 = 0.97. The accuracy is high because most cases are negative, while the model misses 40% of the real positives. This is the usual reason to report precision and recall alongside accuracy.

QuestionWhich quantity answers the question: given that the model flagged a transaction as fraud, how likely is it to be fraud? Write it as a conditional probability.Show answer

That is precision, P(Y=1∣Y^=1)P(Y = 1 \mid \hat{Y} = 1). The condition is the prediction, and the event is the true label. Recall would be P(Y^=1∣Y=1)P(\hat{Y} = 1 \mid Y = 1), which conditions on the truth instead.

QuestionA model has precision 0.9 and recall 0.1. Compute F1 and explain why it is so low even though precision looks good.Show answer

F1=2×0.9×0.1/(0.9+0.1)=0.18/1.0=0.18F_1 = 2 \times 0.9 \times 0.1 / (0.9 + 0.1) = 0.18 / 1.0 = 0.18. The model is careful when it does flag something, but it flags very little, so it misses 90% of the real positives. The harmonic mean is pulled toward the smaller value, so F1 reflects that weakness, while the arithmetic mean of 0.5 would hide it.

QuestionWhy does raising the decision threshold usually increase precision and decrease recall, and why might a team still choose a low threshold?Show answer

A higher threshold means a prediction needs stronger evidence, so fewer cases are flagged. Flagged cases are more likely to be true, which raises precision, but fewer real positives are caught, which lowers recall. A team might choose a low threshold when missing a positive is much more costly than a false alarm, such as in an early cancer screen where a follow-up test is cheap compared with a missed diagnosis.

QuestionA fraud detector flags 1 in 500 transactions and 2% of flagged transactions turn out to be fraud. Fraud happens in 0.1% of transactions. Is its recall above or below 50%? Compute it.Show answer

Start with the flags. Per 100,000 transactions there are 200 flags and 2% of them are fraud, so TP=4TP = 4 and FP=196FP = 196. There are 100 real frauds, so FN=96FN = 96 and recall =4/100=4%= 4 / 100 = 4\%. This is well below 50%. No extra number is needed: the base rate fixes the count of real frauds, which is the denominator of recall.

Sources