Chapter 7 // Classification

Measuring Classifiers.

A classifier is right 90% of the time. Is it any good?

Reading time
17 min
Data

For regression, one number such as RMSE told us most of what we needed. Classification is trickier. There are two different ways to be wrong, they rarely cost the same, and the most obvious score can hide a useless model. This chapter builds the small set of measurements that people actually use.

Ninety percent and useless

A clinic screens 200 patients for an illness. Twenty of them have it. Someone proposes this classifier:

Whatever the patient’s data says, answer “healthy”.

It is right about all 180 healthy patients and wrong about the 20 ill ones. That is 180 out of 200, an accuracy of 90%. It has also failed every single person the screening exists to find.

Accuracy counts all mistakes as equal and all examples as equal. When one class is rare, as here, the data is , and a model can score well by ignoring the rare class entirely. Fraud, faults, and disease are all like this: the cases that matter are the uncommon ones.

Checkpoint

One email in a hundred is a scam. A filter that lets every email through has what accuracy?

Four kinds of outcome

A real model gives each patient a risk score, and we flag those above a threshold, as in . Every patient then lands in one of four groups, depending on what the model said and what was true.

The table of those four counts is the . Two of its cells are mistakes. A healthy patient who is flagged is a , a false alarm. An ill patient who is cleared is a , a miss.

Figure

One threshold, four outcomes

ILL (20)HEALTHY (180)Healthy, score 0.21Healthy, score 0.38Healthy, score 0.10Healthy, score 0.27Healthy, score 0.19Healthy, score 0.26Healthy, score 0.18Healthy, score 0.12Healthy, score 0.19Healthy, score 0.63Healthy, score 0.14Healthy, score 0.12Healthy, score 0.13Healthy, score 0.29Healthy, score 0.36Healthy, score 0.50Healthy, score 0.17Healthy, score 0.42Healthy, score 0.31Healthy, score 0.38Healthy, score 0.19Healthy, score 0.19Healthy, score 0.08Healthy, score 0.03Healthy, score 0.62Healthy, score 0.08Healthy, score 0.35Healthy, score 0.55Healthy, score 0.27Healthy, score 0.48Healthy, score 0.16Healthy, score 0.35Healthy, score 0.31Healthy, score 0.23Healthy, score 0.08Healthy, score 0.09Healthy, score 0.09Healthy, score 0.06Healthy, score 0.05Healthy, score 0.03Healthy, score 0.08Healthy, score 0.19Healthy, score 0.11Healthy, score 0.28Healthy, score 0.12Healthy, score 0.69Healthy, score 0.09Healthy, score 0.63Healthy, score 0.03Healthy, score 0.10Healthy, score 0.48Healthy, score 0.40Healthy, score 0.13Healthy, score 0.48Healthy, score 0.24Healthy, score 0.28Healthy, score 0.43Healthy, score 0.28Healthy, score 0.07Healthy, score 0.09Healthy, score 0.07Healthy, score 0.33Healthy, score 0.31Healthy, score 0.38Healthy, score 0.06Healthy, score 0.09Healthy, score 0.45Healthy, score 0.06Healthy, score 0.25Healthy, score 0.23Healthy, score 0.17Healthy, score 0.34Healthy, score 0.47Healthy, score 0.25Healthy, score 0.10Healthy, score 0.09Healthy, score 0.07Healthy, score 0.07Healthy, score 0.24Healthy, score 0.16Healthy, score 0.40Healthy, score 0.26Healthy, score 0.18Healthy, score 0.38Healthy, score 0.56Healthy, score 0.34Healthy, score 0.40Healthy, score 0.04Healthy, score 0.21Healthy, score 0.11Healthy, score 0.10Healthy, score 0.43Healthy, score 0.08Healthy, score 0.06Healthy, score 0.01Healthy, score 0.55Healthy, score 0.05Healthy, score 0.27Healthy, score 0.14Healthy, score 0.76Healthy, score 0.22Healthy, score 0.10Healthy, score 0.33Healthy, score 0.24Healthy, score 0.43Healthy, score 0.09Healthy, score 0.05Healthy, score 0.53Healthy, score 0.23Healthy, score 0.29Healthy, score 0.28Healthy, score 0.42Healthy, score 0.04Healthy, score 0.38Healthy, score 0.25Healthy, score 0.34Healthy, score 0.31Healthy, score 0.17Healthy, score 0.05Healthy, score 0.81Healthy, score 0.05Healthy, score 0.06Healthy, score 0.45Healthy, score 0.12Healthy, score 0.43Healthy, score 0.21Healthy, score 0.30Healthy, score 0.02Healthy, score 0.20Healthy, score 0.08Healthy, score 0.09Healthy, score 0.38Healthy, score 0.04Healthy, score 0.20Healthy, score 0.10Healthy, score 0.11Healthy, score 0.09Healthy, score 0.07Healthy, score 0.11Healthy, score 0.27Healthy, score 0.34Healthy, score 0.02Healthy, score 0.19Healthy, score 0.25Healthy, score 0.27Healthy, score 0.20Healthy, score 0.19Healthy, score 0.16Healthy, score 0.39Healthy, score 0.06Healthy, score 0.36Healthy, score 0.66Healthy, score 0.60Healthy, score 0.09Healthy, score 0.05Healthy, score 0.15Healthy, score 0.07Healthy, score 0.29Healthy, score 0.05Healthy, score 0.34Healthy, score 0.03Healthy, score 0.14Healthy, score 0.10Healthy, score 0.43Healthy, score 0.03Healthy, score 0.34Healthy, score 0.04Healthy, score 0.27Healthy, score 0.21Healthy, score 0.06Healthy, score 0.08Healthy, score 0.64Healthy, score 0.67Healthy, score 0.02Healthy, score 0.20Healthy, score 0.29Healthy, score 0.34Healthy, score 0.43Healthy, score 0.12Healthy, score 0.10Ill, score 0.66Ill, score 0.82Ill, score 0.39Ill, score 0.71Ill, score 0.83Ill, score 0.88Ill, score 0.59Ill, score 0.80Ill, score 0.87Ill, score 0.62Ill, score 0.56Ill, score 0.62Ill, score 0.87Ill, score 0.47Ill, score 0.77Ill, score 0.50Ill, score 0.84Ill, score 0.37Ill, score 0.80Ill, score 0.92flagged →
Model: flaggedModel: cleared
Really ill
17
Caught
true positive
3
Missed
false negative
Really healthy
15
False alarm
false positive
165
Correctly cleared
true negative
Threshold
0.50
Accuracy
91.0%
Ill patients missed
3 of 20
0.50
Each dot is a patient. Ill patients are in the top row, healthy ones in the bottom row, and everyone to the right of the orange line is flagged. Try it: start at 0.50 and read the four counts. Then lower the threshold until no ill patient is missed, and see what that costs in false alarms.

At a threshold of 0.50 the model catches 17 of the 20 ill patients and misses 3. It also raises 15 false alarms. Its accuracy is 91%, one point better than the classifier that did nothing, yet it is doing something completely different and far more useful. Accuracy could not tell them apart. The four counts can.

Precision and recall

Two questions get the useful information out of the matrix. Each one looks at a different slice.

Of the patients we flagged, how many are really ill? That is . At 0.50 we flagged 32 people and 17 were ill:

precision=caughtcaught+false alarms=1717+15=53%\text{precision} = \frac{\text{caught}}{\text{caught} + \text{false alarms}} = \frac{17}{17 + 15} = 53\%

Of the patients who are really ill, how many did we flag? That is :

recall=caughtcaught+missed=1717+3=85%\text{recall} = \frac{\text{caught}}{\text{caught} + \text{missed}} = \frac{17}{17 + 3} = 85\%

The do-nothing classifier has a recall of zero, which exposes it immediately.

Figure

A trade, not a free choice

0.5, 0.850.5, 0.53recallprecision
  • Precision: of those flagged, how many are ill
  • Recall: of those ill, how many are flagged
  • F1
Flagged
32
Precision
17 of 32 = 53%
Recall
17 of 20 = 85%
F1
0.65
0.50
Precision and recall for every threshold from 0.05 to 0.95. Try it: slide the threshold to the right and watch precision climb while recall falls. Find a threshold where both are above 60%.

The two pull in opposite directions. Flag almost everyone and recall is perfect but precision is dreadful. Flag only the most certain cases, say above 0.70, and precision rises to 85% while recall drops to 55%. No threshold maximises both.

When a single number is wanted, the combines them. It is high only when precision and recall are both high, so neither can be sacrificed for the other.

Every threshold at once

Precision and recall describe a model at one threshold. To judge the model itself, whatever threshold is used later, plot all the thresholds together.

For each threshold, mark two things: across, the share of healthy patients wrongly flagged; up, the share of ill patients caught. Joining the points gives the .

Figure

The whole model in one curve

guessing at random← perfect is this corner0.08, 0.85threshold 0.50
  • This model, every threshold
  • Your threshold
  • A model that guesses
Threshold
0.50
Ill patients caught
85%
Healthy patients flagged
8%
Area under the curve
0.958
0.50
Each point on the orange curve is one threshold. The dark dot is the threshold you choose. Try it: move the threshold from 0.95 down to 0.05 and watch the dot travel from the bottom left to the top right.

A perfect model reaches the top left corner: every ill patient caught, no healthy one flagged. A model that guesses lies along the diagonal. The closer a curve bows toward the corner, the better the model separates the two groups.

The area under the curve, the , sums this up in one number. It has a concrete meaning: pick one ill patient and one healthy patient at random, and the AUC is the chance that the model gives the ill one the higher score. Here it is 0.958. A perfect model scores 1, and guessing scores 0.5.

Choosing the threshold

The ROC curve shows which trade-offs are available. It does not say which to take. That depends on what each mistake costs, and the costs come from the situation, not from the data.

ExamplePricing the mistakes

Suppose a false alarm means an extra blood test, cost 1 unit. A missed illness means late treatment, cost 50 units. Compare two thresholds:

ThresholdMissedFalse alarmsAccuracyTotal cost
0.5031591.0%3 × 50 + 15 = 165
0.3504179.5%0 × 50 + 41 = 41

The lower threshold has much worse accuracy and a quarter of the cost.

Figure

Put a price on each mistake

ILL (20)HEALTHY (180)Healthy, score 0.21Healthy, score 0.38Healthy, score 0.10Healthy, score 0.27Healthy, score 0.19Healthy, score 0.26Healthy, score 0.18Healthy, score 0.12Healthy, score 0.19Healthy, score 0.63Healthy, score 0.14Healthy, score 0.12Healthy, score 0.13Healthy, score 0.29Healthy, score 0.36Healthy, score 0.50Healthy, score 0.17Healthy, score 0.42Healthy, score 0.31Healthy, score 0.38Healthy, score 0.19Healthy, score 0.19Healthy, score 0.08Healthy, score 0.03Healthy, score 0.62Healthy, score 0.08Healthy, score 0.35Healthy, score 0.55Healthy, score 0.27Healthy, score 0.48Healthy, score 0.16Healthy, score 0.35Healthy, score 0.31Healthy, score 0.23Healthy, score 0.08Healthy, score 0.09Healthy, score 0.09Healthy, score 0.06Healthy, score 0.05Healthy, score 0.03Healthy, score 0.08Healthy, score 0.19Healthy, score 0.11Healthy, score 0.28Healthy, score 0.12Healthy, score 0.69Healthy, score 0.09Healthy, score 0.63Healthy, score 0.03Healthy, score 0.10Healthy, score 0.48Healthy, score 0.40Healthy, score 0.13Healthy, score 0.48Healthy, score 0.24Healthy, score 0.28Healthy, score 0.43Healthy, score 0.28Healthy, score 0.07Healthy, score 0.09Healthy, score 0.07Healthy, score 0.33Healthy, score 0.31Healthy, score 0.38Healthy, score 0.06Healthy, score 0.09Healthy, score 0.45Healthy, score 0.06Healthy, score 0.25Healthy, score 0.23Healthy, score 0.17Healthy, score 0.34Healthy, score 0.47Healthy, score 0.25Healthy, score 0.10Healthy, score 0.09Healthy, score 0.07Healthy, score 0.07Healthy, score 0.24Healthy, score 0.16Healthy, score 0.40Healthy, score 0.26Healthy, score 0.18Healthy, score 0.38Healthy, score 0.56Healthy, score 0.34Healthy, score 0.40Healthy, score 0.04Healthy, score 0.21Healthy, score 0.11Healthy, score 0.10Healthy, score 0.43Healthy, score 0.08Healthy, score 0.06Healthy, score 0.01Healthy, score 0.55Healthy, score 0.05Healthy, score 0.27Healthy, score 0.14Healthy, score 0.76Healthy, score 0.22Healthy, score 0.10Healthy, score 0.33Healthy, score 0.24Healthy, score 0.43Healthy, score 0.09Healthy, score 0.05Healthy, score 0.53Healthy, score 0.23Healthy, score 0.29Healthy, score 0.28Healthy, score 0.42Healthy, score 0.04Healthy, score 0.38Healthy, score 0.25Healthy, score 0.34Healthy, score 0.31Healthy, score 0.17Healthy, score 0.05Healthy, score 0.81Healthy, score 0.05Healthy, score 0.06Healthy, score 0.45Healthy, score 0.12Healthy, score 0.43Healthy, score 0.21Healthy, score 0.30Healthy, score 0.02Healthy, score 0.20Healthy, score 0.08Healthy, score 0.09Healthy, score 0.38Healthy, score 0.04Healthy, score 0.20Healthy, score 0.10Healthy, score 0.11Healthy, score 0.09Healthy, score 0.07Healthy, score 0.11Healthy, score 0.27Healthy, score 0.34Healthy, score 0.02Healthy, score 0.19Healthy, score 0.25Healthy, score 0.27Healthy, score 0.20Healthy, score 0.19Healthy, score 0.16Healthy, score 0.39Healthy, score 0.06Healthy, score 0.36Healthy, score 0.66Healthy, score 0.60Healthy, score 0.09Healthy, score 0.05Healthy, score 0.15Healthy, score 0.07Healthy, score 0.29Healthy, score 0.05Healthy, score 0.34Healthy, score 0.03Healthy, score 0.14Healthy, score 0.10Healthy, score 0.43Healthy, score 0.03Healthy, score 0.34Healthy, score 0.04Healthy, score 0.27Healthy, score 0.21Healthy, score 0.06Healthy, score 0.08Healthy, score 0.64Healthy, score 0.67Healthy, score 0.02Healthy, score 0.20Healthy, score 0.29Healthy, score 0.34Healthy, score 0.43Healthy, score 0.12Healthy, score 0.10Ill, score 0.66Ill, score 0.82Ill, score 0.39Ill, score 0.71Ill, score 0.83Ill, score 0.88Ill, score 0.59Ill, score 0.80Ill, score 0.87Ill, score 0.62Ill, score 0.56Ill, score 0.62Ill, score 0.87Ill, score 0.47Ill, score 0.77Ill, score 0.50Ill, score 0.84Ill, score 0.37Ill, score 0.80Ill, score 0.92flagged →
Model: flaggedModel: cleared
Really ill
17
Caught
true positive
3
Missed
false negative
Really healthy
15
False alarm
false positive
165
Correctly cleared
true negative
Threshold
0.50
Misses × 50
150
False alarms × 1
15
Total cost
165
0.50
The same patients, with each miss costing 50 and each false alarm costing 1. Try it: find the threshold with the lowest total cost. Notice that it is not the one with the highest accuracy.

The default threshold of 0.50 treats both mistakes as equally bad. That is almost never true. A spam filter should rarely bin a real email, so it wants high precision. A screening test should rarely miss an illness, so it wants high recall. Decide which mistake is worse, by how much, and choose the threshold to match.

Checkpoint

A bank's fraud model blocks cards. Blocking a genuine customer is annoying; missing a fraud is expensive but refundable. Customers are leaving because of wrongly blocked cards. What should change?

In code

from sklearn.metrics import (classification_report, confusion_matrix,
                             precision_score, recall_score, roc_auc_score)

# y_true: 1 for ill, 0 for healthy.   scores: the model's risk score for each patient.
y_pred = (scores >= 0.5).astype(int)            # apply a threshold yourself

print(confusion_matrix(y_true, y_pred))         # [[165  15]     rows: truth (healthy, ill)
                                                #  [  3  17]]    columns: prediction
print(precision_score(y_true, y_pred))          # 0.53
print(recall_score(y_true, y_pred))             # 0.85
print(roc_auc_score(y_true, scores))            # 0.958: uses the scores, not a threshold

print(classification_report(y_true, y_pred))    # precision, recall and F1 for both classes

Note that scikit-learn puts the negative class first, so the matrix reads cleared-and-healthy in the top left. Always check which way round a confusion matrix is before reading it.

Key Takeaways

  • Accuracy misleads when one class is rare. A model can score well by ignoring the cases that matter.
  • The confusion matrix separates the two kinds of mistake: false alarms and misses.
  • Precision asks how trustworthy a flag is. Recall asks how many real cases were found. Raising one lowers the other.
  • The ROC curve shows every threshold at once, and AUC scores the model without choosing one.
  • The right threshold depends on what each mistake costs. 0.50 is rarely it.
You can now
  • Explain why accuracy misleads when one class is rare.
  • Fill in a confusion matrix and work out precision, recall and F1 from it.
  • Read an ROC curve and say what AUC measures.
  • Choose a threshold from the costs of the two kinds of mistake.

Exercises

Try each one before opening its solution. Getting stuck for a few minutes is where most of the learning happens.

Exerciseby hand

At a threshold of 0.50 the screening model catches 17 of the 20 ill patients and raises 15 false alarms among the 180 healthy ones. Fill in the four cells of the confusion matrix, then work out accuracy, precision, recall and F1.

Hint

True negatives are the healthy patients who were not flagged: 180 minus the false alarms.

Solution

True positives 17, false negatives 3, false positives 15, true negatives 180−15=165180 - 15 = 165.

  • Accuracy: (17+165)/200=91%(17 + 165) / 200 = 91\%
  • Precision: 17/(17+15)=17/32≈53%17 / (17 + 15) = 17 / 32 \approx 53\%
  • Recall: 17/20=85%17 / 20 = 85\%
  • F1: 2×0.53×0.85/(0.53+0.85)≈0.652 \times 0.53 \times 0.85 / (0.53 + 0.85) \approx 0.65

About half of the flags are false alarms, and three ill patients in twenty are missed. The accuracy figure hides both facts.

Exerciseby hand

For the same 200 patients, work out accuracy, precision and recall for a model that flags everyone, and for a model that flags no one.

Hint

For each model, fill in the four cells first. One of the measures ends up as 0 divided by 0.

Solution

Flags everyone. All 20 ill patients are caught, so recall is 100%. Of the 200 flags, 20 are right, so precision is 10%. Accuracy is also 10%.

Flags no one. No ill patient is caught, so recall is 0%. Accuracy is 180/200=90%180 / 200 = 90\%. Precision is 0 divided by 0, which has no value: the model never made a flag to be judged.

The two extremes show why one number is never enough. Each of them is perfect on one measure and useless in practice.

Exerciseby hand

A false alarm costs 1 (a follow-up test) and a missed illness costs 20. Threshold A gives 15 false alarms and 3 misses. Threshold B gives 40 false alarms and 1 miss. Which is cheaper? Which has the higher accuracy?

Hint

Multiply each kind of mistake by its cost and add. For accuracy, count the mistakes of both kinds together.

Solution

Cost of A: 15×1+3×20=7515 \times 1 + 3 \times 20 = 75. Cost of B: 40×1+1×20=6040 \times 1 + 1 \times 20 = 60. B is cheaper.

Accuracy of A: 18 mistakes in 200, so 91%. Accuracy of B: 41 mistakes in 200, so 79.5%. A is more accurate.

Accuracy counts every mistake the same. When a miss is twenty times worse than a false alarm, the better model is the one that makes more mistakes of the cheap kind.

Exercisethink

What does an AUC of 0.5 tell you about a model? What about an AUC of 0.3, and what would you do with such a model?

Hint

AUC is the chance that a random positive case gets a higher score than a random negative one.

Solution

AUC is the chance that the model gives a randomly chosen positive case a higher score than a randomly chosen negative one. At 0.5 the scores carry no information: the model ranks no better than a coin.

An AUC of 0.3 means the model usually ranks positives below negatives. It has found a real pattern and has it backwards. Flip its scores (use one minus the score) and the AUC becomes 0.7. In practice an AUC well below 0.5 nearly always means a bug, such as labels swapped somewhere.

Exercisewith the figure

In the figure “A trade, not a free choice”, read precision and recall at a threshold of 0.50 and at 0.70. Then find the lowest threshold at which no ill patient is missed, and read the precision there.

Hint

At 0.50 the model catches 17 of 20 with 15 false alarms.

Solution

At 0.50, recall is 85% and precision about 53%. At 0.70, precision rises to 85% and recall falls to 55%: the flags are more trustworthy and nearly half the ill patients are missed.

To miss nobody the threshold has to drop well below 0.50, and precision falls a long way, because most of the people flagged are now healthy. For a screening test that can be the right choice: a false alarm costs a follow-up test, and a miss costs far more.

Going further

When to prefer a precision-recall curve

With a very rare positive class, the ROC curve can look excellent while precision is poor, because the false alarm rate is a share of a huge number of negatives. Plotting precision against recall for every threshold gives a more honest picture in that case.

Is 70% really 70%?

A risk score of 0.7 is only useful as a probability if about 70% of patients given that score turn out to be ill. A model for which this holds is called calibrated. A model can rank patients well, with a high AUC, and still be badly calibrated.

More than two classes

With several classes the confusion matrix grows to one row and one column per class. Precision and recall are then computed for each class in turn and averaged.

References and further reading

Last updated Spotted a mistake? Suggest an editWhat changed

In this chapter
  1. Top of the chapter
  2. 7.1Ninety percent and useless
  3. 7.2Four kinds of outcome
  4. 7.3Precision and recall
  5. 7.4Every threshold at once
  6. 7.5Choosing the threshold
  7. 7.6In code
  8. Exercises
Search

    The Book of Machine Learning
    Search
    Reading settings
    Text size
    Colours

    Saved in this browser only.

    Keyboard shortcuts
    /
    Search the book
    ← →
    Previous and next chapter
    g
    Glossary
    c
    Table of contents
    e
    Jump to the exercises
    t
    Reading settings
    ?
    Show this list
    Esc
    Close a window or pop-in