Sub topic 8
Fitting a calibrator without leaking data
This chapter assumes you know train, validation and test splits, cross-validation, and how to call predict_proba in scikit-learn. A calibrator is a small model that maps a classifier's scores to probabilities. Like any model, it can be fitted dishonestly. This chapter covers the split rules that keep it honest.
Why the calibrator needs its own data
A classifier fitted on a training set has seen those labels. Its scores on that data are usually too confident, because the model has partly memorized them. If you fit the calibrator on those same scores, it learns the model's overconfidence on seen data. Then it fails on new data.
The rule is that the calibrator must be fitted on scores for examples the base model did not train on. The final test set must stay untouched by both.
Option 1: a separate calibration set
Split the data three ways: train, calibration and test. Fit the base model on train. Fit the calibrator on the model's scores for the calibration set. Report results on test.
- The scheme is simple and easy to audit.
- It costs data, because the base model never sees the calibration rows.
- With a small calibration set, flexible calibrators such as isotonic regression overfit.
Option 2: cross-validation
Cross-validation reuses all the training data. For each fold, fit a clone of the base model on the other folds and score the held-out fold. Together, the held-out scores cover every training row, and none came from a model that saw that row. The calibrator is fitted on these pooled scores.
CalibratedClassifierCV with an integer cv does exactly this. The next chapters use it in detail. By default (ensemble=True), it keeps one fitted clone and one calibrator per fold, and averages their calibrated predictions at prediction time.
A leak you can see
The code below fits the calibrator on training scores, then on held-out scores. It compares both on the test set. Exact numbers vary by library version, but the held-out version should give a lower Brier score for an overfit model.
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.isotonic import IsotonicRegression
from sklearn.metrics import brier_score_loss
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=6000, n_features=20, n_informative=5, random_state=0)
X_train, X_rest, y_train, y_rest = train_test_split(X, y, test_size=0.5, random_state=0)
X_cal, X_test, y_cal, y_test = train_test_split(X_rest, y_rest, test_size=0.5, random_state=0)
model = RandomForestClassifier(n_estimators=100, random_state=0).fit(X_train, y_train)
def brier_with_calibrator(X_fit, y_fit):
iso = IsotonicRegression(out_of_bounds="clip")
iso.fit(model.predict_proba(X_fit)[:, 1], y_fit)
p = iso.predict(model.predict_proba(X_test)[:, 1])
return brier_score_loss(y_test, p)
print("fitted on training data :", brier_with_calibrator(X_train, y_train))
print("fitted on held-out data :", brier_with_calibrator(X_cal, y_cal))On its own training rows, the forest gives scores near 0 or 1, so the calibrator learns that extreme scores are nearly always right. That does not hold on test data.
Common mistakes
- Calling
predict_probaon training rows and fitting the calibrator on them. - Wrapping an already-fitted model in a calibrator and passing it the same training data.
- Scaling or selecting features on all the data before splitting.
- Reusing the calibration set to evaluate calibration quality. Evaluate on the test set.
Exercise
You train a gradient boosting model on 10,000 rows, then call CalibratedClassifierCV on the fitted model using those same 10,000 rows. Why is this a leak, and what is one fix?
Show answer
The base model already saw those rows, so its scores on them are overconfident, and the calibrator learns that overconfidence. Fix it by fitting the calibrator on rows the base model did not train on: either hold out a separate calibration set, or pass the unfitted estimator to CalibratedClassifierCV with cv=5 so each fold's scores come from a model that did not see that fold.