Measure What Accuracy Hides

Accuracy, the share of predictions that are right, is a single number, and it can hide the very thing you built a model for. When the answer you care about is rare, a model can be right almost every time and still catch none of those cases. This lesson takes a yes-or-no prediction apart into its four possible outcomes and builds two scores from them, precision and recall. Together they show how many of the cases that matter a model really finds, and how often it is right when it says it found one.

When high accuracy means nothing

Picture a subscription service with 100 customers. Over the next month, 10 of them will leave and 90 will stay. You want a model that points out the ones about to leave, so that someone can talk to them in time.

Now take the laziest possible model: it ignores everything it knows about the customers and answers "stays" for every one of them. It is right about the 90 who stay and wrong about the 10 who leave, so its accuracy is \( 90 / 100 = 90\% \). That sounds good, yet the model has not found a single customer who leaves, which was the whole point.

  • 90 stay
  • 10 leave
  • always answers "stays"
  • right on 90 of 100
  • finds 0 of the 10 who leave

The reason is arithmetic, not bad luck. Accuracy counts every right answer the same, and when 90 of the 100 answers are "stays", the easy answer already collects 90 points. The rarer the interesting answer, the higher the score a useless model gets: with 1 leaver in 100, always answering "stays" scores 99%.

So a high accuracy only means something next to the share of the most common answer. A model that scores 87% here is worse on accuracy than the lazy one, and it can still be far more useful, because it might find most of the leavers. To see that, you need to count the right and wrong answers separately for each kind of case.

True or false?

If 95 of 100 emails are not spam, a filter that lets every email through has an accuracy of 95%.

True. It is right about the 95 normal emails and wrong about the 5 spam ones, so 95 of 100 answers are right, while it catches no spam at all.

A model with 90% accuracy is always more useful than one with 85% accuracy on the same rows.

False. The 90% may come from always giving the common answer. The 85% model may find many of the rare cases that matter, which accuracy alone does not show.

Four ways a prediction can land

A yes-or-no prediction compares two things for each case: what the model said and what really happened. First choose which answer is the positive one. It is simply the answer you are looking for, here "leaves". It does not mean good news: a customer leaving is a positive case even though nobody wants it. The other answer, "stays", is the negative one.

Each case then lands in exactly one of four boxes. The word positive or negative describes what the model predicted, and the word true or false says whether that prediction was right:

  • True positive (TP): the model said "leaves" and the customer did leave. A case found.
  • False positive (FP): the model said "leaves" but the customer stayed. A false alarm.
  • False negative (FN): the model said "stays" but the customer left. A miss.
  • True negative (TN): the model said "stays" and the customer stayed.

Laid out as a grid with the real answers in the rows and the predictions in the columns, the four counts form a confusion matrix. Here is one for a real model on the same 100 customers:

Confusion matrix of a model on 100 customers
Really...Predicted: staysPredicted: leaves
stays (90)TN = 81FP = 9
leaves (10)FN = 4TP = 6

Read it row by row. Of the 90 customers who stay, the model let 81 pass and raised a false alarm on 9. Of the 10 who leave, it found 6 and missed 4. The diagonal from top-left to bottom-right holds the right answers, so accuracy is \( (81 + 6) / 100 = 87\% \): lower than the lazy model's 90%. The lazy model's matrix would read TN = 90, FP = 0, FN = 10, TP = 0. All ten of its mistakes sit in one box, the misses, and accuracy cannot show that, because it only counts how many answers were right, not which box the wrong ones fall in.

The layout above, real answers in rows and predictions in columns, is the one scikit-learn uses. It puts the answers in sorted order. When the answers are written "No" and "Yes", "No" comes first, so true negatives sit top-left and true positives bottom-right. Other books and websites sometimes swap the rows and columns, so check the labels before you read a matrix from elsewhere.

True or false?

A false positive is a case the model missed.

False. A false positive is a false alarm: the model said "yes" and the answer was "no". A missed case is a false negative.

Adding up the four boxes of a confusion matrix gives the number of cases that were predicted.

True. Every case lands in exactly one box, so the four counts always add up to the total, here 81 + 9 + 4 + 6 = 100.

Precision: how many alarms are real

Suppose someone acts on every customer the model flags, for example by calling them. The first question is how many of those calls were needed. The model flagged 15 customers, the whole "Predicted: leaves" column: 6 who really leave and 9 who would have stayed. Precision is the share of the flagged cases that really are positive:

$$\text{precision} = \frac{TP}{TP + FP} = \frac{6}{6 + 9} = 0.40$$

A precision of 0.40 means that 4 out of every 10 alarms are real and 6 are false. Precision runs from 0 to 1, and it only looks at the cases the model flagged. It says nothing about the customers the model never flagged. So a model that flags just its one surest case, and gets it right, has a precision of 1, even though it misses almost everyone who leaves.

Recall: how many real cases are found

The second question looks from the other side: of the customers who really leave, how many did the model catch? That is the "Really leaves" row: 6 found and 4 missed. Recall is the share of the really positive cases that the model finds:

$$\text{recall} = \frac{TP}{TP + FN} = \frac{6}{6 + 4} = 0.60$$

Both scores share the same top number, the true positives, and differ only in what they divide by. Precision divides by a column of the matrix (everything predicted positive), recall by a row (everything really positive). Keeping that picture in mind is the easiest way to stop the two from getting swapped.

Recall is where the lazy model is exposed. It found no leavers, so its recall is \( 0 / (0 + 10) = 0 \). Its precision is not even defined: it flagged nobody, so the fraction is \( 0 / 0 \). scikit-learn reports such a precision as 0 and prints a warning, unless you tell it otherwise with the zero_division parameter.

Same 100 customers, two models
  • Accuracy
  • Recall
  • Always "stays"
  • Real model

Side by side the picture turns around. On accuracy the lazy model wins by three points; on recall it scores nothing, while the real model finds 6 of the 10 customers who leave.

True or false?

A model that flags every single case as positive has a recall of 1.

True. It flags every real positive, so it misses none and FN = 0. Its precision, though, falls to the share of positives in the data.

Precision and recall always have the same denominator, the number of positive cases.

False. Precision divides by the cases predicted positive (TP + FP), recall by the cases that really are positive (TP + FN). Only the true positives on top are shared.

The same counts in scikit-learn

scikit-learn computes all of this from two lists: the real answers and the predicted ones. The example below uses eight cases with the text answers "No" and "Yes", where "Yes" is the positive answer.

from sklearn.metrics import confusion_matrix, precision_score, recall_score

y_true = ["No", "No", "Yes", "No", "Yes", "No", "Yes", "No"]
y_pred = ["No", "Yes", "Yes", "No", "No", "Yes", "Yes", "No"]

matrix = confusion_matrix(y_true, y_pred, labels=["No", "Yes"])
print(matrix)
# [[3 2]
#  [1 2]]

tn, fp, fn, tp = matrix.ravel().tolist()
print(tn, fp, fn, tp)
# 3 2 1 2

print(precision_score(y_true, y_pred, pos_label="Yes"))
# 0.5
print(recall_score(y_true, y_pred, pos_label="Yes"))
# 0.6666666666666666

confusion_matrix returns the grid with the real answers in rows and the predictions in columns; labels=["No", "Yes"] fixes the order so "No" comes first. ravel() flattens the grid row by row and tolist() turns it into plain Python numbers, which gives the four counts in the order true negatives, false positives, false negatives, true positives. The true positives come last, not first.

precision_score and recall_score need to know which answer is positive. Their pos_label parameter defaults to 1, which is not one of the answers "No" and "Yes", so with text answers you pass pos_label="Yes". The results match the grid: 2 alarms out of 4 were real, a precision of 0.5, and 2 of the 3 real "Yes" cases were found, a recall of about 0.67.

Which mistake costs more

Precision and recall measure the two kinds of mistake. A false alarm lowers precision; a miss lowers recall. They usually pull against each other: a model that flags more cases catches more of the real ones, raising recall, but also raises more false alarms, lowering precision. A model that flags fewer cases does the opposite. No single setting maximizes both, so you have to decide which mistake you can live with.

That decision comes from the problem, not from the model. Ask what each mistake costs the people involved:

ProblemCost of a false alarmCost of a missWatch most
Screening for a serious illnessan extra testan illness found too laterecall
Spam filtera real email lostone spam email in the inboxprecision
Customers about to leavean offer nobody neededa customer lost without a wordboth, weighed by cost

In screening, a missed illness costs far more than a follow-up test, so you accept more false alarms to push recall up. In a spam filter it is the other way round: losing one important email is worse than seeing a few spam messages, so precision comes first. For customers about to leave, a false alarm costs the price of a call or an offer and a miss costs a customer, and the business sets the balance between the two.

Whichever you favour, read the two numbers together and next to the confusion matrix. A recall of 0.95 means little if precision is 0.05, and a precision of 1 means little if the model found only one case. Accuracy can sit beside them, but on its own it rewards the model for the common answer that nobody needed help with.

True or false?

Whether precision or recall matters more is decided by the cost of each kind of mistake in the problem, not by the model.

True. The same model can be good enough for one use and poor for another. What a false alarm and a miss each cost is a judgment about the problem.

A model tuned to catch more of the real positive cases will usually raise fewer false alarms as well.

False. Flagging more cases usually catches more real positives and more negatives by mistake, so recall rises while precision tends to fall.