Sub topic 5
Precision: how trustworthy are positive predictions
This chapter assumes you can read a confusion matrix (TP, FP, TN, FN), you know what a supervised binary classifier is, and you can write basic Python. Here you will learn what precision measures, how to compute it, and which mistakes people commonly make with it.
The working model
Precision answers one question: when the model says "positive," how often is it right?
precision = TP / (TP + FP)The denominator, TP + FP, is every example the model labeled positive. The numerator is the part of those that really are positive. Precision looks only at the model's positive predictions. True negatives and false negatives never enter the formula.
That makes precision a measure of the cost of false alarms. Each false positive lowers it. A missed positive (a false negative) does not change it at all.
Worked example
Use the counts from the earlier confusion matrix example: TP = 3, FP = 1, TN = 5, FN = 1.
The model made 4 positive predictions (3 + 1). Three were correct, so precision is 3 / 4 = 0.75. Put another way, one in four positive alerts is a false alarm.
tp, fp, tn, fn = 3, 1, 5, 1
precision = tp / (tp + fp)
print(precision) # 0.75Notice that tn and fn are unused. Changing them would leave the result the same.
Reading precision correctly
Precision is a rate over predictions. A precision of 0.75 means that about 75% of the positive predictions will be correct, if the test data resembles the data the model sees in use.
Some practical readings:
- Precision of 1.0 means no false positives. It does not mean the model found every positive. A model that flags a single obvious case and ignores the rest can have perfect precision.
- Low precision means many false alarms. Whoever acts on the predictions (a reviewer, a customer, an automated system) spends effort on cases that turn out to be negative.
- Precision depends on how common the positive class is. If positives are rare, even a small false positive rate can produce many false positives compared with true positives, and precision falls.
The last point matters in practice. Suppose a screening model has 1% false positive rate and finds 90% of true cases. On 10,000 people with 100 true cases, it finds 90 of them and falsely flags about 99 of the 9,900 healthy people. Precision is 90 / (90 + 99), roughly 0.48, even though the model looks accurate by most informal standards.
Common errors
- Treating precision as overall quality. Precision ignores missed positives. Always read it next to recall, which is the next chapter's topic.
- Dividing by zero. If the model makes no positive predictions, TP + FP is 0 and the formula is undefined. Handle this case explicitly. The helper below returns 0.0 in that case, which is one common convention.
- Mixing up the denominator. Recall uses TP + FN. Precision uses TP + FP. A useful memory aid is that the P in precision stands for "predicted positive."
- Computing it for the wrong class. In a binary problem, precision is usually reported for the positive class, the one you care about finding. Swapping which class is labeled 1 changes the result.
- Comparing across different datasets. Precision shifts when the share of positives changes. A model's precision on a balanced test set will not match its precision on production data where positives are rare.
def precision(y_true, y_pred):
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, y_pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
predicted_positive = tp + fp
if predicted_positive == 0:
return 0.0 # no positive predictions; precision is undefined
return tp / predicted_positive
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
print(precision(y_true, y_pred)) # 0.75
print(precision([1, 0, 1], [0, 0, 0])) # 0.0Checking your understanding
Exercise
A spam filter flags 50 emails as spam. Of those, 45 really are spam and 5 are legitimate. It also lets 15 real spam emails through to the inbox. What is its precision, and does the 15 missed spam emails change it?
Show answer
Precision = TP / (TP + FP) = 45 / (45 + 5) = 45 / 50 = 0.9. The 15 missed spam emails are false negatives. They do not appear in the precision formula, so they do not change it. They would lower recall instead.
Exercise
Model A makes 2 positive predictions and both are correct. Model B makes 100 positive predictions and 90 are correct. Which has higher precision, and why might you still prefer Model B?
Show answer
Model A has precision 2 / 2 = 1.0 and Model B has 90 / 100 = 0.9, so Model A has higher precision. Model B may still be better because it finds far more true positives. Precision says nothing about how many positives were missed, so you need recall as well.