Logistic Classification
Threshold Probabilities
Convert predicted probabilities to 0/1 class labels by thresholding.
Default threshold 0.5: class = 1 if p>=0.5 else 0. A second loop at 0.7
shows how raising the threshold tightens the positive-class criterion.
Library: (np.array(probs) >= 0.5).astype(int). RESULT: labels at 0.5.
By hand
With NumPy
(np.array(probs) >= threshold).astype(int) applies the comparison
element-wise and converts the boolean array to ints in one expression.
naive.py
probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]
labels = []
for p in probs:
labels.append(1 if p >= 0.5 else 0)
labels_07 = []
for p in probs:
labels_07.append(1 if p >= 0.7 else 0)
print('RESULT:', labels)
library.py
import numpy as np
from dalib.display import set_display
set_display()
probs = [0.1, 0.4, 0.6, 0.75, 0.85, 0.3]
labels = (np.array(probs) >= 0.5).astype(int).tolist()
labels_07 = (np.array(probs) >= 0.7).astype(int).tolist()
print('threshold=0.5:', labels)
print('threshold=0.7:', labels_07)
print('RESULT:', labels)
threshold=0.5: [0, 0, 1, 1, 1, 0]
threshold=0.7: [0, 0, 0, 1, 1, 0]
RESULT: [0, 0, 1, 1, 1, 0]
Implementation notes
- Threshold 0.5 is the default: it minimises classification error when classes are balanced and the model is well-calibrated.
- Raising the threshold (e.g. to 0.7) requires higher confidence to predict positive — reduces false positives but increases false negatives. Lowering it does the opposite. This trade-off is the precision/recall curve.
- Cross-reference:
logistic-predict-given-weights(this chapter) computes the probabilities that are thresholded here. - Cross-reference:
log-loss-small(this chapter) evaluates probabilities before thresholding — log loss works on raw probs, not 0/1 labels.