Probability versus threshold
Quick answer
A probability estimate and a class decision are separate outputs. A threshold converts a positive-class probability into a decision; the appropriate threshold depends on the costs and constraints of the application. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Count decisions at two thresholds
y=[0,1,0,1]p=[.2,.4,.6,.8]for t in [.5,.3]: pairs=list(zip(y,[int(v>=t) for v in p])) print([pairs.count(k) for k in [(0,0),(0,1),(1,0),(1,1)]])Expected output:
[1, 1, 1, 1][1, 1, 0, 2]The lower threshold catches the second positive without changing any probability.
How it works
Define a binary rule explicitly: predict 1 when p >= t, otherwise 0, where t is between zero and one. scikit-learn’s native binary decision normally uses a strict positive decision score; our custom rule includes equality and must be tested at the boundary. A confusion matrix counts true negatives (TN), false positives (FP), false negatives (FN) and true positives (TP); rows represent actual labels and columns predicted labels in our order [0,1]. With probabilities [0.2,0.4,0.6,0.8] and labels [0,1,0,1], t=0.5 produces one of each cell; t=0.3 produces TN=1, FP=1, FN=0, TP=2. Lowering the threshold cannot reduce the number predicted positive for fixed scores, but can increase false alarms. If a missed device fault costs 4 units and an unnecessary inspection costs 1, minimize FP+4FN on validation data. For calibrated probabilities and only these constant costs, predicting positive has expected cost 1(1-p), predicting negative costs 4*p; the expected-cost rule uses threshold 1/(1+4)=0.2. Calibration means among cases receiving an estimate near p, roughly fraction p are positive under the evaluation distribution. Logistic regression is not guaranteed calibrated; measure it separately on held-out data, especially under drift or weighting. Do not tune a threshold on the final test set. Changing only t leaves the underlying probability estimates and their log loss unchanged. Thresholds are not necessarily suitable for multiclass tasks; use an explicit decision policy.
Verify a confusion matrix and log loss
from sklearn.metrics import confusion_matrix,log_lossy=[0,1,0,1]p=[.2,.4,.6,.8]for t in [.5,.3]: print(confusion_matrix(y,[int(v>=t) for v in p],labels=[0,1]).tolist())print(round(log_loss(y,p,labels=[0,1]),6))Expected output:
[[1, 1], [1, 1]][[1, 1], [0, 2]]0.569717Both threshold rules share the same log loss because the original probabilities are unchanged.
Common mistakes
Do not assume p=0.7 means a 70% guarantee for one device. Do not use test labels to choose a threshold. Always document positive class, equality policy, matrix order and cost assumptions.
Practice and next steps
Compute FP+4*FN for the two thresholds above, choose the lower-cost policy on this toy validation set, then run the chapter project. Complete the knowledge check and coding exercise. Next: K-nearest neighbor distances.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Probability versus threshold — knowledge check
Apply probability versus threshold to new examples.
What happens to log loss when only the decision threshold changes?
Which cell represents a missed positive?
With p=0.5 and t=0.5, what does our >= rule predict?
Under calibrated probabilities and costs FP=1,FN=4, what threshold minimizes expected cost?
When can the threshold be chosen?