Sub topic 5
Precision: how trustworthy a positive prediction is
This chapter assumes you can read a binary confusion matrix and name its four cells (TP, FP, FN, TN). It also assumes you are comfortable with ratios and short Python snippets. Here you will learn what precision measures, how to compute it, and where it goes wrong.
The definition
Precision answers one question: when the model predicts positive, how often is it right?
precision = TP / (TP + FP)The denominator is every case the model labeled positive. That is one column of the matrix, the column for the predicted positive class. Precision ignores the actual positives that the model missed. Those are the false negatives, and they belong to recall, which has its own chapter.
A worked example
Take the matrix from the earlier chapters. Rows are actual labels and columns are predicted labels.
from sklearn.metrics import confusion_matrix, precision_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
tn, fp, fn, tp = cm.ravel()
print(tn, fp, fn, tp) # 4 2 1 3
precision = tp / (tp + fp)
print(precision) # 0.6
print(precision_score(y_true, y_pred)) # 0.6The model predicted positive five times (3 + 2). Three of those were correct, so precision is 3 / 5 = 0.6. Put another way, 40% of the alarms were false.
When precision matters most
Precision matters when a false positive is costly. Some examples:
- A spam filter that moves real email to the junk folder.
- A fraud system that freezes a customer's card.
- A screening step that sends people to an expensive follow-up procedure.
In each case, a user or an analyst has to deal with every flagged item. Low precision wastes their time and erodes their trust in the system.
Common errors
- Reading the wrong axis. If your tool puts predictions on the rows, then TP + FP is a row sum, not a column sum. Check the axes before dividing.
- Dividing by the wrong total. Dividing TP by the number of actual positives (TP + FN) gives recall, not precision.
- Judging precision alone. A model that predicts positive only once, and gets it right, has precision 1.0 while missing nearly every real positive.
- Ignoring the empty case. If the model never predicts positive, TP + FP is 0 and precision is undefined.
The undefined case
When there are no positive predictions, scikit-learn warns and returns 0.0 by default. You can choose the value explicitly.
from sklearn.metrics import precision_score
y_true = [1, 0, 1, 0]
y_pred = [0, 0, 0, 0] # the model never predicts positive
print(precision_score(y_true, y_pred, zero_division=0)) # 0.0
print(precision_score(y_true, y_pred, zero_division=1)) # 1.0State which convention you used when you report results. Otherwise, readers cannot tell whether a score of 0.0 means "all wrong" or "never predicted positive".
Summary
| Question | Cells used | Formula |
|---|---|---|
| How trustworthy is a positive prediction? | Predicted-positive column | TP / (TP + FP) |
| How many real positives were found? | Actual-positive row | TP / (TP + FN) |
Exercise
A model makes 40 positive predictions. Of these, 30 are correct. There are 60 actual positives in the data. What is the precision, and why does the number 60 not matter for it?
Show answer
Precision is 30 / 40 = 0.75. The 60 actual positives only affect recall (30 / 60 = 0.5). Precision uses only the predicted-positive column, TP + FP = 40.
Exercise
A confusion matrix has predictions on the rows and actual labels on the columns. Which cells do you sum for the precision denominator?
Show answer
Sum the row for the positive prediction, which holds TP and FP. Precision is TP divided by that row total.