What is threshold moving as a remedy for class imbalance?
Last updated
Threshold moving keeps the model trained on the original data and changes only the cutoff applied to its scores. Instead of predicting positive at p ≥ 0.5, you pick the threshold that optimizes the metric or cost you actually care about.
Why 0.5 is wrong under imbalance: A model trained on a rare class rarely outputs high probabilities for it, so 0.5 yields very low recall even when ranking (ROC/PR AUC) is good.
How to choose the threshold:
Empirically: sweep thresholds on a validation set and maximize F1, recall at a precision floor, or expected profit.
Decision-theoretically: with calibrated probabilities, predict positive when p > C_FP / (C_FP + C_FN).
In scikit-learn:
TunedThresholdClassifierCV(orprecision_recall_curvemanually).
Strengths and caveats:
No data distortion, no retraining, calibration preserved.
Only helps if the model ranks well; tune on held-out data, never the test set.
[lang:python]
from sklearn.metrics import precision_recall_curve
import numpy as np
p = model.predict_proba(X_val)[:, 1]
prec, rec, thr = precision_recall_curve(y_val, p)
f1 = 2 * prec * rec / (prec + rec + 1e-12)
best_t = thr[np.argmax(f1[:-1])]
y_pred = (model.predict_proba(X_test)[:, 1] >= best_t).astype(int)