Sub topic 10
How the decision threshold reshapes the matrix
This chapter assumes you can read a binary confusion matrix (TN, FP, FN, TP), know precision, recall and specificity, and can follow short Python snippets. Many classifiers don't output a label directly. They output a score, and the threshold turns that score into a label. Changing the threshold changes the matrix even though the model is the same.
From score to label
A model such as logistic regression returns a score for each example, often read as a probability of the positive class. A rule then decides the label:
- If score >= threshold, predict positive (1).
- Otherwise, predict negative (0).
The default threshold is often 0.5, but nothing makes it correct for your problem. The threshold is a setting you choose after training.
Watching the cells move
The actual labels never change, so the row totals stay fixed. In the data below, there are always 6 actual negatives and 4 actual positives. Only the split inside each row changes.
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
scores = [0.9, 0.2, 0.4, 0.8, 0.1, 0.7, 0.6, 0.55, 0.3, 0.35]
for threshold in (0.35, 0.5, 0.65):
y_pred = [1 if s >= threshold else 0 for s in scores]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
precision = tp / (tp + fp)
recall = tp / (tp + fn)
specificity = tn / (tn + fp)
print(f"{threshold:.2f} TN={tn} FP={fp} FN={fn} TP={tp} "
f"precision={precision:.2f} recall={recall:.2f} "
f"specificity={specificity:.2f}")The output is:
0.35 TN=3 FP=3 FN=0 TP=4 precision=0.57 recall=1.00 specificity=0.50
0.50 TN=4 FP=2 FN=1 TP=3 precision=0.60 recall=0.75 specificity=0.67
0.65 TN=5 FP=1 FN=2 TP=2 precision=0.67 recall=0.50 specificity=0.83| Threshold | FP | FN | Precision | Recall | Specificity |
|---|---|---|---|---|---|
| 0.35 | 3 | 0 | 0.57 | 1.00 | 0.50 |
| 0.50 | 2 | 1 | 0.60 | 0.75 | 0.67 |
| 0.65 | 1 | 2 | 0.67 | 0.50 | 0.83 |
Raising the threshold made the model more cautious. Each step moved examples out of the predicted-positive column and into the predicted-negative column. Some of those moves removed false positives (good). Others removed true positives, which became false negatives (bad).
Choosing a threshold
The right threshold depends on which error costs more.
- Missing a positive is costly (disease screening, fraud alerts). Lower the threshold to raise recall. Accept more false alarms.
- A false alarm is costly (automatic account bans, spam deletion). Raise the threshold to raise precision and specificity. Accept more misses.
Precision does not always rise with the threshold. It did here, but on other data it can fluctuate, because raising the threshold may remove true positives and false positives at different rates.
Common errors
- Comparing models at different thresholds. A model evaluated at 0.5 and another at 0.3 produce matrices that are not comparable. Fix the threshold, or compare across all thresholds.
- Assuming 0.5 is neutral. On imbalanced data, scores for the rare class are often low, so 0.5 may give very low recall.
- Tuning on the test set. Pick the threshold on validation data, then report the matrix on held-out data.
Exercise
Using the scores above, suppose you lower the threshold from 0.35 to 0.25. Which actual positive and which actual negatives cannot be affected in the way you would expect, and what happens to FN? (Hint: the lowest positive score is 0.4.)
Show answer
FN stays at 0, because every actual positive already scores 0.4 or higher and was already predicted positive at 0.35. Lowering the threshold can only add false positives. Negatives scoring 0.3 and 0.3 are not duplicated: the negatives with scores 0.3 (index 8) join the predicted positives, so FP rises from 3 to 4, and the negative scoring 0.2 joins at 0.2 or below. Precision falls to 4/8 = 0.50 at 0.25, and recall stays at 1.00.