Threshold and class imbalance
A binary classifier can emit a score before making a class decision. A threshold converts the score to a label. Here a score at or above the threshold becomes 1; a lower score becomes 0. A score in [0, 1] need not be a calibrated probability. Threshold choice is a decision policy: it changes which packages are flagged without retraining the underlying scoring model.
Move the threshold
Four fictional packages have damage scores [0.9, 0.6, 0.4, 0.2] and verified labels [1, 0, 1, 0]. At 0.5, the first two are flagged: TP=1, FP=1, FN=1, TN=1. At 0.7, only the first is flagged: TP=1, FP=0, FN=1, TN=2. At 0.4, the first three are flagged: TP=2, FP=1, FN=0, TN=1. Equality is deliberately positive in this lesson.
def classify(scores, threshold): if not 0 <= threshold <= 1: raise ValueError("Threshold outside [0, 1]") if any(not 0 <= s <= 1 for s in scores): raise ValueError("Score outside [0, 1]") return [int(s >= threshold) for s in scores]
scores = [0.9, 0.6, 0.4, 0.2]for t in (0.5, 0.7, 0.4): print(t, classify(scores, t))The printed lists match the three policies above. A higher threshold cannot increase the number of predicted positives on a fixed score list. It cannot increase TP or FP, and cannot decrease FN or TN. Recall therefore cannot rise as the threshold rises when actual positives exist. Precision may rise or fall depending on which items are removed; a universal inverse precision–recall relationship is not guaranteed. A threshold of 1 still flags scores exactly equal to 1.
Class imbalance and decision costs
When damage is rare, an always-intact rule can look good by accuracy while missing all damage. Compare to that baseline and report support, precision, recall, F1, and operational workload. Suppose an unnecessary inspection costs 2 fictional units and missing damage costs 5. The cost is 2 × FP + 5 × FN. At thresholds 0.5, 0.7 and 0.4 the costs are 7, 5 and 2 respectively. These are assumptions for a teaching example, not measured costs or a deployment recommendation.
def error_cost(actual, predicted, fp_cost, fn_cost): if len(actual) != len(predicted): raise ValueError("Lengths must match") if any(v not in (0, 1) for v in actual + predicted): raise ValueError("Binary labels required") if fp_cost < 0 or fn_cost < 0: raise ValueError("Costs must be nonnegative") return sum(fp_cost if y == 0 and p == 1 else fn_cost if y == 1 and p == 0 else 0 for y, p in zip(actual, predicted))
actual = [1,0,1,0]print(error_cost(actual, [1,1,0,0], 2, 5))print(error_cost(actual, [1,0,0,0], 2, 5))print(error_cost(actual, [1,1,1,0], 2, 5))The function returns 7, 5, and 2 for the three prediction lists. It assumes finite nonnegative costs and binary aligned lists; correct decisions carry zero incremental cost here. It is not a full business model: inspection capacity, downstream costs and uncertainty may change the preferred threshold. Tune thresholds using validation data and a stated objective or recall/workload constraint; lock the policy before evaluating on untouched test data. Do not select the best threshold on the test set.
ROC and precision–recall curves
Sweep distinct score thresholds, grouping ties. The ROC curve plots false-positive rate FP/(FP+TN) on the horizontal axis and recall on the vertical axis. The precision–recall curve plots recall horizontally and precision vertically. At 0.5 our ROC point is (0.5, 0.5), and PR point is (0.5, 0.5); at 0.7 the points are (0, 0.5) and (0.5, 1); at 0.4 the points are (0.5, 1) and (1, 2/3). An all-negative endpoint may require a threshold above the maximum score, outside our helper contract if that maximum equals 1. ROC AUC summarizes ranking ability: with ties counted as half, it is the probability that a randomly chosen positive outranks a randomly chosen negative. For these four scores, three of four positive-negative pairs are correctly ranked, giving AUC 0.75. Both classes must be present. A PR curve can expose low precision on rare positives even if ROC AUC looks strong; its baseline depends on prevalence. Average precision and trapezoidal PR area are different summaries. Undefined endpoint conventions differ across libraries.
Calibration and evaluation boundaries
Calibration concerns whether predicted probabilities match observed frequencies: among many items scored near 0.8, roughly 80% should be positive if the probabilities are calibrated for that population. Good ranking alone does not ensure calibration. A reliability diagram groups probabilities and compares mean predicted probability with observed positive fraction; small groups have substantial uncertainty. Calibration should use appropriate held-out data and may change under population shift. No four-item example establishes calibration or production safety. Practice: compare threshold 0.5 to 0.4 using the original four packages; report FP, FN, recall and workload. The lower threshold adds one true positive and no false positive here, but that pattern is not guaranteed on other data. Next: classical model families and decision trees.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Threshold and class imbalance quiz
Apply binary classification evaluation to new scenarios.
Score 0.4 at threshold 0.4: what label does our helper return?
For fixed scores and actual labels, which count cannot increase as the threshold rises under the score >= threshold rule?
FP=2, FN=1; costs 2 and 5 respectively. Total error cost?
What is plotted on the ROC horizontal axis?
After training a scoring model, which labeled split should guide threshold selection before final evaluation?