
What predict_proba Really Means: Probability Calibration Explained
- كاتب موثّق
- scikit-learn 1.9
محتويات المقال
The common misconception
predict_proba as the chance a prediction is right: a 0.8 means an 80% chance. It's an easy belief to hold:
The method is literally called predict_proba, and its output behaves like probabilities: between 0 and 1, summing to 1 across classes.Logistic regression, the first classifier most people learn, often does give well-calibrated probabilities, so the habit forms early. Good accuracy and ROC AUC feel like they certify everything the model outputs.
What actually happens
Calibration is a statement about groups
predict_proba close to 0.8, about 80% actually belong to the positive class. That's a claim about a group of cases checked against outcomes, never about a single case. A calibrated 0.8 means "8 in 10 cases like this one". It also says nothing about how useful the model is: one that gave every case the base rate would be perfectly and useless. Ranking and calibration are two separate qualities, and a model can have either without the other.
Why scores drift from frequencies
Naive Bayes assumes features are independent given the class. When several features carry the same evidence, it counts that evidence several times and pushes scores toward 0 and 1. Max-margin methods such as SVMs and boosted trees push scores away from 0 and 1, a sigmoid-shaped distortion described by Niculescu-Mizil and Caruana; scikit-learn's guide shows random forests rarely reaching 0 or 1. Modern neural networks are often poorly calibrated, Guo and colleagues found, and they link it to depth, width, weight decay and batch normalization. Class weights and resampling change the class balance the model learns from, so its scores describe that rebalanced world, not yours.
from sklearn.calibration import calibration_curvefrom sklearn.datasets import make_classificationfrom sklearn.metrics import brier_score_loss, roc_auc_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.naive_bayes import GaussianNB# 2 informative features plus 10 redundant mixes of themX, y = make_classification(n_samples=20_000, n_features=20, n_informative=2, n_redundant=10, random_state=11)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.5, random_state=0)proba = GaussianNB().fit(X_tr, y_tr).predict_proba(X_te)[:, 1]low = proba < 0.05 # auto-dismissed as "under 5% likely"print(f"under 5%: {low.sum()} reports, {y_te[low].mean():.1%} violating")share, score = calibration_curve(y_te, proba, n_bins=5)print("mean score ", score.round(2))print("share positive", share.round(2))auc, brier = roc_auc_score(y_te, proba), brier_score_loss(y_te, proba)print(f"ROC AUC {auc:.3f}, Brier {brier:.3f}")# under 5%: 5078 reports, 13.7% violating# mean score [0.01 0.3 0.5 0.71 0.99]# share positive [0.16 0.55 0.57 0.7 0.93]# ROC AUC 0.934, Brier 0.121from sklearn.calibration import CalibratedClassifierCV, calibration_curvefrom sklearn.datasets import make_classificationfrom sklearn.metrics import brier_score_loss, roc_auc_scorefrom sklearn.model_selection import train_test_splitfrom sklearn.naive_bayes import GaussianNBX, y = make_classification(n_samples=20_000, n_features=20, n_informative=2, n_redundant=10, random_state=11)X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.5, random_state=0)model = CalibratedClassifierCV(GaussianNB(), method="isotonic", cv=5)proba = model.fit(X_tr, y_tr).predict_proba(X_te)[:, 1]low = proba < 0.05print(f"under 5%: {low.sum()} reports, {y_te[low].mean():.1%} violating")share, score = calibration_curve(y_te, proba, n_bins=5)print("mean score ", score.round(2))print("share positive", share.round(2))auc, brier = roc_auc_score(y_te, proba), brier_score_loss(y_te, proba)print(f"ROC AUC {auc:.3f}, Brier {brier:.3f}")# under 5%: 1839 reports, 4.7% violating# mean score [0.08 0.3 0.52 0.75 0.95]# share positive [0.08 0.31 0.52 0.75 0.95]# ROC AUC 0.934, Brier 0.098How CalibratedClassifierCV fits
cv=5, it fits a copy of the classifier on four fifths of the training data and fits the calibrator on the remaining fifth, five times, then averages the five calibrated models' probabilities. The calibrator never sees scores from rows the classifier trained on. Pick the method by your data:
"sigmoid"is Platt's method: a logistic regression on the scores, with few parameters, good for small data and sigmoid-shaped distortions."isotonic"fits any monotonic distortion, but the API reference warns it tends to overfit with well under 1,000 calibration samples."temperature", added in scikit-learn 1.8, divides the logits by one fitted value, and handles multiclass natively.
predict_proba returns probabilities | |
How to apply it in your project
Decide whether you need probabilities Ranking, or a cut-off tuned on validation data, works with any scores. Expected values, displayed confidence and fixed probability rules need calibration. Plot the curve on held-out data Use calibration_curvewith the counts per bin, for the model exactly as it's deployed.Calibrate with CalibratedClassifierCV Use sigmoid for small calibration sets, isotonic with more than about 1,000 samples, temperature for multiclass. Evaluate on data the calibrator never saw Report the curve with log loss or Brier score from a separate test split. Recheck after the world changes A new base rate, a new market or retraining with class weights can break calibration silently. Plot the curve on recent data.
Key takeaways
predict_probagives scores; a 0.8 means 80% only if the model is calibrated, and that's something you measure.Calibration is about groups: of all cases scored 0.8, about 80% should be positive. Naive Bayes, max-margin models, deep networks and class weights all distort scores; here "under 5%" meant 13.7%. CalibratedClassifierCVfixed it without changing the ranking: ROC AUC stayed 0.934.
📚 المصادر
Probability calibration — scikit-learn User Guide Probability Calibration curves — scikit-learn examples CalibratedClassifierCV — scikit-learn API Reference Niculescu-Mizil and Caruana, Predicting Good Probabilities With Supervised Learning (ICML 2005) Guo et al., On Calibration of Modern Neural Networks (ICML 2017)
التعليقات (0)