Sub topic 2
The decision threshold
This chapter assumes you know binary classification, can read a model's predicted probabilities or scores (for example from logistic regression), and can write basic NumPy. Here you will see how a score becomes a yes/no label. The rest of the tutorial builds on that step.
Scores are not labels
Most classifiers do not output a class directly. They output a score for each example, and a higher score means the model considers the positive class more likely. The score may be a probability between 0 and 1, or an unbounded value such as the margin of a support vector machine.
To get a label, you compare the score to a cutoff called the decision threshold:
- If score >= threshold, predict positive (1).
- Otherwise, predict negative (0).
The model is fixed. The threshold is a separate choice you make afterward, and each value gives a different set of predicted labels.
Moving the threshold
Take four examples with true labels [0, 0, 1, 1] and scores [0.1, 0.4, 0.35, 0.8]. The code below applies several thresholds to them.
import numpy as np
y_true = np.array([0, 0, 1, 1])
y_score = np.array([0.1, 0.4, 0.35, 0.8])
for t in [0.9, 0.5, 0.4, 0.35, 0.1]:
y_pred = (y_score >= t).astype(int)
print(f"threshold={t:.2f} predictions={y_pred} true={y_true}")| Threshold | Predictions | Effect |
|---|---|---|
| 0.90 | 0 0 0 0 | Nothing is positive |
| 0.50 | 0 0 0 1 | Only the highest score is positive |
| 0.40 | 0 1 0 1 | One negative is now flagged |
| 0.35 | 0 1 1 1 | Both positives found, one negative flagged |
| 0.10 | 1 1 1 1 | Everything is positive |
Two things stand out in the table.
- Lowering the threshold never removes a positive prediction. It only adds them. The number of predicted positives moves from 0 up to 4 as the threshold falls.
- No single threshold is perfect here. The positive scoring 0.35 sits below the negative scoring 0.4, so catching both positives requires flagging a negative too.
This trade-off is what a ROC curve records. Each threshold gives one pair of error rates, and sweeping the threshold traces out the whole curve. Later chapters define those rates.
Common errors
Treating 0.5 as the correct threshold. The 0.5 default in many libraries is a convention. It is reasonable only when the classes are roughly balanced, the probabilities are well calibrated, and both kinds of mistake cost about the same. On the example above, 0.5 misses one of the two positives.
Assuming the score is a probability. Scores from some models, such as a decision function, can be negative or larger than 1. A threshold of 0.5 means nothing for them. A ROC curve only needs the scores to rank examples correctly, so it works for any of these.
Mixing up the comparison. This tutorial predicts positive when score >= threshold. Libraries differ on whether the boundary case counts as positive, which matters when scores tie exactly at the threshold. Always check the convention before comparing hand-computed results with a library's.
Reversing the score direction. Some scores are higher for the negative class, such as an anomaly distance used as a "normal-ness" score. Pass scores where higher means more positive, or flip the sign first.
Exercise
Using the scores [0.1, 0.4, 0.35, 0.8] with the rule "predict positive when score >= threshold", what are the predictions at threshold 0.36? Why is the result the same as at threshold 0.4?
Show answer
The predictions are [0, 1, 0, 1]. Only 0.4 and 0.8 are at or above 0.36. No score lies between 0.36 and 0.4, so any threshold in that range gives the same predictions as 0.4.