
The 0.5 Threshold Isn't Part of the Model: Tuning the Decision Cut-Off
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
The common misconception
predict uses, so getting different decisions must mean training a different model. It's an easy belief to hold:
predictreturns labels directly. The cut-off appears nowhere in the call, so it feels built in.Tutorials fix lopsided decisions by changing the training: class weights, resampling, more data. 0.5 sounds principled: predict "yes" when "yes" is more likely than "no".
What actually happens
0.5 is a rule applied after the model
predict_proba or a score above 0 from decision_function, and those rules are most certainly not ideal for most use cases. Training never looks at them. LogisticRegression fits its weights by minimizing log loss over the probabilities; no appears anywhere in that process. The fitted model is the probability function. The threshold is a policy you put on top.
The right cut-off comes from costs
from sklearn.datasets import make_classificationfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import confusion_matrixfrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=10_000, n_features=10, n_informative=4, weights=[0.8], class_sep=0.7, random_state=2)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)model = LogisticRegression().fit(X_tr, y_tr)tn, fp, fn, tp = confusion_matrix(y_te, model.predict(X_te)).ravel()print(f"cutoff 0.5: {fp} declined good, {fn} missed defaults")print(f"cost {fp + 5 * fn}") # a missed default costs 5x a wrong decline# cutoff 0.5: 48 declined good, 417 missed defaults# cost 2133from sklearn.datasets import make_classificationfrom sklearn.frozen import FrozenEstimatorfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import confusion_matrix, make_scorerfrom sklearn.model_selection import FixedThresholdClassifier, train_test_splitfrom sklearn.model_selection import TunedThresholdClassifierCVdef cost(y_true, y_pred): # a missed default costs 5x a wrong decline tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel() return fp + 5 * fnX, y = make_classification(n_samples=10_000, n_features=10, n_informative=4, weights=[0.8], class_sep=0.7, random_state=2)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)model = LogisticRegression().fit(X_tr, y_tr)frozen = FrozenEstimator(model) # fit() won't retrain itby_costs = FixedThresholdClassifier(frozen, threshold=1 / 6).fit(X_tr, y_tr)print(f"cutoff 1/6: cost {cost(y_te, by_costs.predict(X_te))}")scorer = make_scorer(cost, greater_is_better=False)tuned = TunedThresholdClassifierCV(LogisticRegression(), scoring=scorer)tuned.fit(X_tr, y_tr) # picks the cutoff by 5-fold cross-validationtuned_cost = cost(y_te, tuned.predict(X_te))print(f"cutoff {tuned.best_threshold_:.3f}: cost {tuned_cost}")# cutoff 1/6: cost 1390# cutoff 0.187: cost 1390FrozenEstimator makes the point explicit: calling fit on the wrapper doesn't touch the trained weights.
TunedThresholdClassifierCV gets there without the formula. It tries cut-offs across internal 5-fold cross-validation, scores each with the business cost, and lands on 0.187, with the same test cost of 1390. Its default scorer is balanced accuracy, so pass your own: the cut-off is only as good as the metric it optimizes.
When the formula and the data disagree
One model, many cut-offs
predict; training never sees it | |
How to apply it in your project
Price both mistakes Agree with the business on what a false positive and a false negative cost, even roughly. Start from the formula With calibrated probabilities, the cost of a false positive divided by both costs is a sound first cut-off. Tune it on validation data Use TunedThresholdClassifierCVwith a scorer built from your costs, or a curve computed on a validation split.Ship the cut-off with the model Wrap the trained model in FixedThresholdClassifier(FrozenEstimator(model), threshold=...), and version both.Revisit when costs change New prices, capacity or policy mean a new cut-off. Recompute it on recent data; the model can stay.
Key takeaways
A classifier learns probabilities; 0.5 is a rule applied to them afterward. Moving the cut-off needs no retraining: the same model's cost fell from 2133 to 1390. The right cut-off comes from costs: false-positive cost over the sum of both, for calibrated probabilities. Tune it on validation data, never on training data, and version it with the model.
📚 المصادر
Tuning the decision threshold for class prediction — scikit-learn User Guide Post-tuning the decision threshold for cost-sensitive learning — scikit-learn examples Classification: Thresholds and the confusion matrix — Google Machine Learning Crash Course Elkan, The Foundations of Cost-Sensitive Learning (IJCAI 2001) Probability calibration — scikit-learn User Guide
التعليقات (0)