Sub topic 9
Moving the threshold: precision versus recall
This chapter assumes you can read a confusion matrix and compute precision and recall from TP, FP, and FN counts. It also assumes basic Python and the idea of a classifier that outputs a score.
Where predictions come from
Most classifiers do not output a label directly. They output a score, often a probability-like number between 0 and 1. The label comes from a rule:
- If the score is at least the decision threshold, predict 1 (positive).
- Otherwise, predict 0 (negative).
Many libraries default to a threshold of 0.5. That default is a convention, not a law. You can pick any threshold, and each choice produces a different confusion matrix. So the same model gives many different precision and recall values.
Why the two metrics move in opposite directions
Raising the threshold makes the model more cautious. Fewer examples clear the bar, so it predicts fewer positives.
- Fewer false positives, so precision tends to rise.
- More missed positives, so recall tends to fall.
Lowering the threshold does the reverse. The model flags more examples. It catches more real positives, so recall rises. It also flags more negatives by mistake, so precision tends to fall.
The word "tends" matters. Precision does not always rise smoothly as the threshold goes up. It can wobble, because removing one positive prediction changes both the numerator and the denominator. Recall, however, never rises when you raise the threshold, since the set of predicted positives only shrinks.
A worked sweep
The code below uses eight examples. Three are truly positive and five are negative. It tries three thresholds and computes precision and recall for each.
y_true = [1, 1, 1, 0, 0, 0, 0, 0]
scores = [0.9, 0.6, 0.4, 0.7, 0.3, 0.2, 0.1, 0.05]
for threshold in (0.3, 0.5, 0.8):
y_pred = [int(s >= threshold) for s in scores]
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))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
precision = tp / (tp + fp) if tp + fp else 0.0
recall = tp / (tp + fn)
print(threshold, round(precision, 2), round(recall, 2))
# 0.3 0.6 1.0
# 0.5 0.67 0.67
# 0.8 1.0 0.33The results:
| Threshold | Predicted positives | Precision | Recall |
|---|---|---|---|
| 0.3 | 5 | 0.60 | 1.00 |
| 0.5 | 3 | 0.67 | 0.67 |
| 0.8 | 1 | 1.00 | 0.33 |
At 0.3, the model finds all three real positives but also flags two negatives (scores 0.7 and 0.3). At 0.8, only the 0.9 example is flagged. It is correct, so precision is 1.0, but two real positives are missed.
Common errors
- Treating 0.5 as the correct threshold. It is only a default. If errors have unequal costs, or the classes are imbalanced, another value is usually better.
- Reading precision of 1.0 as a good model. In the sweep, a threshold of 0.8 gives perfect precision from a single correct prediction. Always read precision together with recall.
- Dividing by zero. If the threshold is so high that nothing is predicted positive, TP + FP is 0 and precision is undefined. The code guards this with
if tp + fp else 0.0. Decide how you want to report this case and say so. - Tuning the threshold on the test set. Choose the threshold on validation data, then report the final numbers once on the test set. Otherwise the reported metrics are optimistic.
Choosing a threshold
Link the threshold to the cost of errors from the previous discussion:
- If a missed positive is costly (disease screening), lower the threshold and accept more false alarms.
- If a false alarm is costly (blocking a legitimate payment, sending a spam filter's catch to the trash), raise the threshold.
A practical method is to compute precision and recall across many thresholds on validation data. Then pick the one that meets a requirement, such as "recall of at least 0.9 with the best precision possible."
Exercise
Using the scores and labels from the worked sweep, what are the precision and recall at threshold 0.65? Which examples are predicted positive?
Show answer
Scores at or above 0.65 are 0.9 (true positive) and 0.7 (false positive). So 2 examples are predicted positive, with TP = 1, FP = 1, and FN = 2. Precision = 1/2 = 0.5 and recall = 1/3 ≈ 0.33. Note that precision at 0.65 (0.5) is lower than at 0.5 (0.67) even though the threshold is higher, which shows precision does not always rise monotonically.
Exercise
A fraud team requires that at least 95% of real fraud cases are caught. Should they move the threshold up or down from a setting where recall is 0.85, and what will they likely give up?
Show answer
Move it down. A lower threshold flags more transactions, which raises recall toward 0.95. They will likely give up precision, because more legitimate transactions get flagged as fraud.