internalsGet Pro

Module 12 · Lesson 2 of 12Free

Precision and recall: two ways to be wrong

Level
Beginner
Topic
Model evaluation and experimentation
Reading time
13 min

When you finish this page you can

  1. Build a confusion matrix for a yes-or-no list and read accuracy, precision and recall from it.
  2. Explain why accuracy barely moves when a churn model reaches about 2,100 more leavers at a 13% leave rate.
  3. Predict how precision and recall move when a list grows or shrinks, and say when F1 is a fair summary.
  4. Choose the number to report for a decision, and name what would change that choice.
  1. Baselines: the simple model to beat13 min
  2. Choosing a metric that matches the goal13 min

Video lesson · coming soon

A better model that accuracy barely notices

is our example online store. Every Monday, Joseph’s growth team emails a ₹100 voucher to the 20,000 customers most likely to stop buying. A model ranks all 200,000 customers in the notebook sample, and the top 20,000 get the voucher.

Farah, Kartly’s ML engineer, has a new model. On last quarter’s customers, where Kartly already knows who left, its list reaches about 10,200 real leavers. The current model reaches about 8,100. That is about 2,100 more leavers a week for the same money.

Then Farah computes accuracy, the share of customers the list gets right. The new model scores 87.2%. A “model” that sends no vouchers at all scores 87.0%.

All counts in this lesson are the course’s made-up win-back scenario: 200,000 customers, of whom 13% (26,000) really leave.

This lesson answers one question: how do you score a yes-or-no list so the number shows the mistakes the business pays for?

Check yourselfAnswer in your head first, then reveal
The new model’s list of 20,000 gets 25,600 of the 200,000 customers wrong. How many of those mistakes are vouchers sent to customers who stayed, and how many are leavers it never emailed?Show answerHide answer
9,800 vouchers went to stayers (20,000 sent minus 10,200 leavers reached). 15,800 leavers were missed (26,000 minus 10,200). Accuracy adds them into one count, as if a wasted ₹100 voucher and a lost customer were the same mistake.

Four boxes behind every yes-or-no

Every customer lands in one of four boxes. The list either flags them or not, and they either leave or stay. This 2×2 table of counts is a .

Flagged: gets a voucher Not flagged
Really left 10,200 true positives 15,800 false negatives
Really stayed 9,800 false positives 164,200 true negatives
  • A true positive is a leaver on the list: the voucher reached the right person.
  • A is a stayer on the list: ₹100 offered to someone who wasn’t leaving.
  • A is a leaver left off the list: a customer lost without a try.
  • A true negative is a stayer left alone.

scikit-learn uses the same layout: rows are the true labels, columns are the predictions. For two classes, confusion_matrix(...).ravel() returns the counts in the order true negatives, false positives, false negatives, true positives.2

Precision reads down the flagged column; recall reads along the leavers row. Accuracy is dominated by the 164,200 true negatives.

Accuracy is the two right boxes divided by everyone: (10,200 + 164,200) ÷ 200,000 = 87.2%. The 164,200 stayers left alone make up most of that score. Baselines showed the result: when 87% stay, answering “stays” for everyone already scores 87%.

Google’s course gives the same advice: avoid accuracy for imbalanced data, where one class is much rarer than the other.3 So Farah needs numbers that look only at the boxes Joseph cares about.

Precision reads the list, recall reads the leavers

Joseph pays per voucher. So his first question is: of the 20,000 vouchers, how many reached a leaver? That is .

precision=TPTP+FP\text{precision} = \frac{TP}{TP + FP}
  • TPTP is the number of true positives: leavers on the list.
  • FPFP is the number of false positives: stayers on the list.

For the new model, 10,200 ÷ 20,000 = 51%.

His second question is: of all 26,000 leavers, how many did Monday’s email reach? That is .

recall=TPTP+FN\text{recall} = \frac{TP}{TP + FN}
  • TPTP is again the leavers on the list.
  • FNFN is the number of false negatives: leavers left off it.

For the new model, 10,200 ÷ 26,000 = 39%. scikit-learn defines both with these formulas. It describes precision as not labelling a negative as positive, and recall as finding all the positives.1

List of 20,000 Leavers reached Precision Recall Accuracy
Nobody leaves (no list) 0 none 0% 87.0%
60-day rule 6,000 30% 23% 83.0%
Current model 8,100 40% 31% 85.1%
New model 10,200 51% 39% 87.2%

Precision has no value for the empty list: there is nothing to divide by.

Precision also ties straight to money. When not to use machine learning worked out that the churn system pays for itself above about 34.8% precision. Accuracy has no such line.

The same 2,100 extra leavers. Precision shows the 11-point gain Joseph pays for; accuracy, spread over all 200,000 customers, shows 2.1.

Here is the surprise. With the list fixed at 20,000, every number in the table moves with one count: leavers reached. Precision is that count over 20,000. Recall is that count over 26,000. Each extra leaver reached also removes one wrong answer from each of two boxes, so accuracy rises too.

So at a fixed list size, accuracy, precision and recall rank the three lists in the same order. Accuracy’s fault is its scale. The 2,100 extra leavers add about 11 points of precision but only 2.1 points of accuracy, and the do-nothing answer sits above two real lists.

The numbers only start to disagree when the list size changes.

A longer list trades precision for recall

Joseph asks: what if he sent 40,000 vouchers? The model ranks customers by their chance of leaving. Customers further down the list are less likely to leave, so each extra voucher is less likely to hit.

Check yourselfAnswer in your head first, then reveal
Joseph doubles the list from 20,000 to 40,000, using the same new model. Does precision go up or down? And recall?Show answerHide answer
Precision falls to about 38% and recall rises to about 59%. The list now reaches about 15,200 leavers, 5,000 more, but it took 20,000 more vouchers to find them: only 25% of the extra vouchers hit. Not “both go up”: a longer list always finds more leavers and more stayers.

Here is the new model at six list sizes, in this lesson’s scenario:

List size Leavers reached Vouchers to stayers Leavers missed Precision Recall F1
5,000 3,550 1,450 22,400 71% 14% 0.23
10,000 6,200 3,800 19,700 62% 24% 0.34
20,000 10,200 9,800 15,800 51% 39% 0.44
40,000 15,200 24,800 10,700 38% 59% 0.46
80,000 21,000 59,000 4,900 26% 81% 0.40
200,000 26,000 174,000 0 13% 100% 0.23

The two kinds of mistake trade against each other. A short list wastes few vouchers but misses most leavers. A list of all 200,000 misses nobody, and its precision falls to the , 13%. Google’s course describes the same pull: lowering the cut adds false positives and removes false negatives, so improving one number usually worsens the other.3

Choosing a list size is the same as choosing a cut on the model’s chance of leaving. That cut is the decision threshold. scikit-learn’s predict uses a cut of 0.5, whatever list size that gives.

Whether the extra 20,000 vouchers pay is a money question, not a metric question. Design: a churn prediction model for an online store sets the list size from the voucher’s cost. Watching the trade-off at every possible cut, as a curve, is ROC and precision-recall curves.

F1 squeezes both into one number

Sometimes a team wants one number for a dashboard. The usual pick is the F1 score, the harmonic mean of precision and recall.3

F1=2×precision×recallprecision+recallF_1 = \frac{2 \times \text{precision} \times \text{recall}}{\text{precision} + \text{recall}}
  • F1F_1 is a score from 0 to 1.
  • precision and recall are the two shares above, written as fractions.

A harmonic mean stays low unless both numbers are high. At 20,000 vouchers: 2 × 0.51 × 0.39 ÷ (0.51 + 0.39) ≈ 0.44.

Look at the F1 column. Of the sizes shown, it is highest at 40,000 vouchers. That choice treats a wasted voucher and a missed leaver as equally bad, and nobody at Kartly priced them that way. scikit-learn also offers F-beta, where a β above 1 weights recall more.1 It still needs someone to pick β, and Joseph thinks in rupees, not β.

F1 is fair when both mistakes cost about the same, or when you compare models before anyone knows the costs.

Which number Farah reports

Joseph has a fixed list of 20,000 vouchers a week. Which number should Farah use to compare the two churn models?

Accuracy

87.0% for sending nothing, 87.2% for the new model. Joseph sees a 0.2-point gain and wonders why he pays for a model at all.

F1

0.44 for the new model. Ranks the two models correctly at 20,000, but the number means nothing in vouchers, and anyone who uses it to pick a list size lands on about 40,000 for no money reason.

Recall alone

39% against 31%. Ranks the models correctly at a fixed list, but says nothing about wasted vouchers, so it can’t tell Joseph whether the list pays.

Why this choice

Joseph’s budget fixes the list, and he pays per voucher, so precision in the top 20,000 is his number. Recall goes beside it because it shows what the list leaves behind. Both come from the same confusion matrix, so Farah reports the four counts too.

The choice changes with the decision. If missing a positive costs far more than a false alarm, recall leads. For fraud, a miss costs about ₹2,500 and a review ₹40, so Imbalanced classes flags at a 1.6% chance. If the list size is free, money sets the cut, and precision and recall describe the result. If Farah wants to compare rankings before any cut is chosen, she needs the curves in the next lesson.

Precision moves when the leavers do

Precision depends on how common leavers are. Bayes’ rule showed why: precision is the chance that a flagged customer leaves, and that chance follows the base rate.

Suppose a quiet quarter in which only 8% of customers leave, 16,000 people. The model scores each customer the same way as before. The list of 20,000 now holds about 6,900 leavers, so precision falls from 51% to about 34%. Recall rises to about 43%, because the fixed list now covers a larger share of fewer leavers. These are illustrative numbers for this lesson’s scenario, assuming leavers and stayers keep the same spread of scores.

Same model, same list size. Fewer leavers in the population means fewer on the list.

So a drop in precision doesn’t always mean the model got worse. Farah checks the base rate before she retrains.

Two more facts shape how she measures in production. The labels arrive late: whether a Monday customer left is known only 60 days after. And customers who got a voucher can’t show what they would have done without one, so she measures on the 1,000 customers held out each week.

Try it yourself

This runs in a free Colab or Kaggle notebook, or on a laptop, in about a second. It makes up the same 200,000 customers and two models as ML engineering roles did, then scores the lists:

Python
import numpy as np
from sklearn.metrics import (accuracy_score, confusion_matrix, f1_score,
                             precision_score, recall_score)

rng = np.random.default_rng(42)
n = 200_000                                   # customers in the notebook sample
left = (rng.random(n) < 0.13).astype(int)     # made-up: 13% really stopped buying
current_score = 1.0 * left + rng.normal(0, 1, n)
new_score = 1.3 * left + rng.normal(0, 1, n)  # separates leavers a little better


def top_list(score: np.ndarray, size: int) -> np.ndarray:
    """1 for the `size` customers with the highest scores, 0 for the rest."""
    flagged = np.zeros(len(score), dtype=int)
    flagged[np.argsort(score)[-size:]] = 1
    return flagged


nobody = np.zeros(n, dtype=int)
print(f"nobody leaves: accuracy {accuracy_score(left, nobody):.3f}")
for name, score in [("current", current_score), ("new", new_score)]:
    flagged = top_list(score, 20_000)
    tn, fp, fn, tp = confusion_matrix(left, flagged).ravel().tolist()
    print(f"{name}: tp {tp:,} fp {fp:,} fn {fn:,} tn {tn:,} "
          f"accuracy {accuracy_score(left, flagged):.3f}")

for size in [5_000, 10_000, 20_000, 40_000, 80_000]:
    flagged = top_list(new_score, size)
    print(f"new, list {size:>6,}: precision {precision_score(left, flagged):.2f} "
          f"recall {recall_score(left, flagged):.2f} f1 {f1_score(left, flagged):.2f}")

# a quiet quarter: 8% leave, same score spread for leavers and stayers
quiet_left = (rng.random(n) < 0.08).astype(int)
quiet_score = 1.3 * quiet_left + rng.normal(0, 1, n)
flagged = top_list(quiet_score, 20_000)
print(f"quiet quarter: leavers {quiet_left.sum():,} "
      f"reached {(flagged & quiet_left).sum():,} "
      f"precision {precision_score(quiet_left, flagged):.2f} "
      f"recall {recall_score(quiet_left, flagged):.2f}")

Output from one seeded run on a laptop CPU. Exact values and times differ with the scikit-learn version and machine.

Text
nobody leaves: accuracy 0.870
current: tp 8,090 fp 11,910 fn 17,855 tn 162,145 accuracy 0.851
new: tp 10,187 fp 9,813 fn 15,758 tn 164,242 accuracy 0.872
new, list  5,000: precision 0.71 recall 0.14 f1 0.23
new, list 10,000: precision 0.62 recall 0.24 f1 0.34
new, list 20,000: precision 0.51 recall 0.39 f1 0.44
new, list 40,000: precision 0.38 recall 0.59 f1 0.46
new, list 80,000: precision 0.26 recall 0.81 f1 0.40
quiet quarter: leavers 16,013 reached 6,858 precision 0.34 recall 0.43

What to look for: change 1.3 to 1.6 and rerun. Precision, recall and F1 all rise at every list size. A better model moves the whole trade-off, instead of sliding along it.

Where this shows up

A list size is one cut. The next lesson looks at every cut at once, so Farah can compare two models before Joseph picks one.

Common mistakes

  • Reporting accuracy for a rare class. At a 13% leave rate, sending nothing scores 87%.
  • Comparing precisions from lists of different sizes. A shorter list almost always has higher precision. Compare at the list size you will use.
  • Reading F1 as a business target. Its best list size assumes both mistakes cost the same.
  • Blaming the model for every precision drop. Check the base rate first.

Check your understanding

Check yourselfAnswer in your head first, then reveal
Joseph halves the list to 10,000 vouchers with the new model. What happens to precision and recall?Show answerHide answer
Precision rises to about 62% and recall falls to about 24%. The shorter list keeps the likeliest leavers, so a larger share of it hits, but it reaches about 4,000 fewer leavers.
Check yourselfAnswer in your head first, then reveal
Two models are scored on the same 200,000 customers with lists of 20,000. Model A reports 48% precision; model B reports 40% recall. Which reaches more leavers?Show answerHide answer
Model B. At a fixed list, both numbers are leaver counts in disguise: A reaches 48% of 20,000, which is 9,600; B reaches 40% of 26,000, which is 10,400.
Check yourselfAnswer in your head first, then reveal
Precision in the top 20,000 drops from 51% to 40% one quarter. The model and its features have not changed. What should Farah check first?Show answerHide answer
The base rate. If fewer customers left that quarter, fewer leavers fit on the list, and precision falls even with an unchanged model. Recall at the same list size may even rise.

Summary

  1. Score a yes-or-no list with its confusion matrix: precision for the share of the list that is right, recall for the share of positives it finds.
  2. Accuracy counts both mistakes as one and is dominated by the true negatives: at 13% leavers, sending nothing scores 87%.
  3. At a fixed list size, precision, recall and accuracy rank models alike; a longer list trades precision for recall.
  4. F1 is the harmonic mean of the two and fits only when both mistakes cost about the same.
  5. Precision follows the base rate, so check how common positives are before blaming the model.

Report a mistake

Tell us what’s wrong in this lesson and we’ll fix it.