Sub topic 16
Calibrating predicted probabilities
This chapter assumes you know the softmax and sigmoid outputs, argmax and threshold decision rules, and the metrics from the earlier chapters. It also assumes you can use scikit-learn estimators.
What calibration means
A model is calibrated if its probabilities match observed frequencies. Among all examples where a class gets a predicted probability of 0.8, about 80% should truly belong to that class.
Accuracy and F1 only check the final decision. Calibration checks the numbers behind it. Poor calibration matters when you:
- tune per-label thresholds, since a threshold of 0.3 only has a clear meaning if the scores behave like probabilities;
- rank or combine labels by confidence;
- pass probabilities to a downstream cost calculation.
Softmax and sigmoid outputs are not automatically calibrated. Naive Bayes tends to be overconfident. Random forests tend to be underconfident near 0 and 1. Deep networks trained with cross-entropy are often overconfident.
Measuring it
Log loss and the Brier score penalize bad probabilities, not just wrong decisions. A reliability curve bins the predictions and plots the mean predicted probability against the observed fraction of positives per bin. For multiclass models, check each class one-vs-rest. For multilabel models, check each label separately.
Fixing it
You fit a small mapping from raw scores to probabilities on data the base model did not train on.
| Method | Shape | Best when |
|---|---|---|
| `sigmoid` (Platt scaling) | Two-parameter logistic curve | Little calibration data, smooth distortion |
| `isotonic` | Monotonic step function | Thousands of examples, irregular distortion |
Isotonic regression overfits on small data. The mapping is monotonic, so it rarely changes the ranking, but it can change argmax decisions and the effect of thresholds.
Example
CalibratedClassifierCV fits the calibrator with cross-validation, so no example is both trained on and calibrated on. For multiclass targets it calibrates each class one-vs-rest and then renormalizes so the probabilities sum to 1.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import log_loss
X, y = make_classification(
n_samples=3000, n_features=20, n_informative=8,
n_redundant=6, n_classes=3, random_state=0,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
raw = GaussianNB().fit(X_train, y_train)
calibrated = CalibratedClassifierCV(
GaussianNB(), method="sigmoid", cv=5
).fit(X_train, y_train)
print("raw log loss: ", round(log_loss(y_test, raw.predict_proba(X_test)), 3))
print("calibrated log loss:", round(log_loss(y_test, calibrated.predict_proba(X_test)), 3))Lower log loss after calibration means the probabilities are more trustworthy. Accuracy may barely move.
Multilabel models
CalibratedClassifierCV does not accept a multi-hot target directly. Wrap it so that each label gets its own calibrated binary model:
from sklearn.multiclass import OneVsRestClassifier
model = OneVsRestClassifier(
CalibratedClassifierCV(GaussianNB(), method="sigmoid", cv=5)
)Common mistakes
- Calibrating on the training set. The base model is already too confident there, so the calibrator learns nothing useful.
- Reusing the same held-out data for calibration, threshold tuning and final scoring. Keep a separate test set.
- Calibrating rare labels with isotonic regression. A label with a few dozen positives gives an unstable step function, so prefer
sigmoid.
Exercise
You tuned a per-label threshold of 0.25 on a random forest's raw scores, then switched to calibrated scores without changing the threshold. Why might the F1 change, and what should you do?
Show answer
Calibration changes the scale of the scores, so 0.25 no longer marks the same operating point. Tune the thresholds again on held-out calibrated probabilities.