Sub topic 11
Calibrating a Model with CalibratedClassifierCV
This chapter assumes you know train/validation/test splits, cross-validation, and the sigmoid function. It also assumes you have read the earlier chapters on reliability diagrams, Brier score, Platt scaling, and isotonic regression. Here you apply both methods with scikit-learn's CalibratedClassifierCV.
What the wrapper does
CalibratedClassifierCV takes a base classifier and fits a calibrator on its scores. With method="sigmoid" it fits Platt scaling. With method="isotonic" it fits isotonic regression.
The key design point is data splitting. If you fit the calibrator on the same rows the base model trained on, the scores are overconfident and the calibrator learns the wrong mapping. With cv=5, the wrapper does this instead:
- Split the training data into 5 folds.
- For each fold, train a clone of the base model on the other 4 folds.
- Score the held-out fold and keep those out-of-fold scores.
- Fit one calibrator per fold on that fold's scores.
At prediction time, the wrapper averages the calibrated probabilities from all 5 model and calibrator pairs.
Comparing raw, sigmoid, and isotonic
Naive Bayes is a good test case. Its independence assumption often pushes probabilities toward 0 and 1. This code compares Brier scores on held-out test data.
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 brier_score_loss
X, y = make_classification(
n_samples=20000, n_features=20, n_informative=5, n_redundant=10, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.5, random_state=0
)
raw = GaussianNB().fit(X_train, y_train)
print("raw :", brier_score_loss(y_test, raw.predict_proba(X_test)[:, 1]))
for method in ("sigmoid", "isotonic"):
calibrated = CalibratedClassifierCV(GaussianNB(), method=method, cv=5)
calibrated.fit(X_train, y_train)
p = calibrated.predict_proba(X_test)[:, 1]
print(f"{method:<10}:", brier_score_loss(y_test, p))Both calibrated versions should score a lower (better) Brier score than the raw model. Exact numbers depend on your scikit-learn version, so compare them in your own run. The test set is never touched during fitting, so the comparison is fair.
Parameters that matter
| Parameter | Effect |
|---|---|
| `estimator` | The base classifier. It is cloned, not reused. |
| `method` | `"sigmoid"` or `"isotonic"`. |
| `cv` | Number of folds, or a splitter object. Default is 5. |
| `ensemble` | If `True` (default), keep one model per fold and average them. If `False`, fit a single calibrator on pooled out-of-fold scores and refit one base model on all data. |
Common errors
- Calibrating on test data. Always keep a separate test set for evaluation, as the code above does.
- Passing an already fitted model. The wrapper refits clones of the estimator. To calibrate a model you trained yourself, use
FrozenEstimatorfromsklearn.frozen(scikit-learn 1.6 and later) withcv="prefit"removed, and give it a separate calibration set. Older versions usedcv="prefit"instead. - Expecting accuracy to change. Calibration is a monotonic mapping per class, so AUC is nearly unchanged. In the multiclass case, accuracy can shift slightly.
- Isotonic on small data. Isotonic regression is flexible and overfits when calibration data is small. Prefer sigmoid below roughly a thousand samples.
Exercise
You set cv=5 and method="isotonic" on 400 training rows. The test Brier score gets worse than the raw model. What is the most likely cause, and what would you try?
Show answer
Isotonic regression is overfitting because each fold's calibrator sees only about 80 scores. Switch to method="sigmoid", which has two parameters, or collect more calibration data.