Sub topic 3
Calibrating a Model with CalibratedClassifierCV
Assume you know binary classification, train/validation/test splits, cross-validation, and basic Python with scikit-learn. Calibration adjusts a model's scores so they behave like probabilities. A predicted probability of 0.8 should correspond to about 80% positive outcomes among similar cases.
Calibrating with `CalibratedClassifierCV`
CalibratedClassifierCV fits a base classifier and a calibrator using cross-validation. Each calibrator learns from predictions made on data that did not train that particular base-model copy.
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=10000, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0
)
model = CalibratedClassifierCV(
estimator=GaussianNB(),
method="sigmoid",
cv=5,
)
model.fit(X_train, y_train)
probability = model.predict_proba(X_test)[:, 1]
print(brier_score_loss(y_test, probability))Use a separate final test set for evaluation. Do not fit the calibrator on test predictions.
Choosing Sigmoid or Isotonic Calibration
Sigmoid calibration, also called Platt scaling, fits a smooth logistic transformation. It usually works well with limited calibration data and avoids many overfitting problems.
Isotonic calibration fits a nondecreasing step function. It can learn more irregular relationships, but it needs more data. With small samples, its steps may fit random noise.
| Method | Strength | Main risk |
|---|---|---|
| Sigmoid | Stable with less data | Too rigid for complex distortion |
| Isotonic | Flexible and monotonic | Overfits small calibration sets |
Temperature Scaling for Neural Networks
Neural networks often produce overconfident softmax probabilities. Temperature scaling divides every logit by one positive value, T, before softmax:
\[ pi = \operatorname{softmax}(zi / T) \]
Fit T by minimizing validation negative log loss. Temperature scaling changes confidence but preserves the top-ranked class.
More Than Two Classes
For multiclass models, calibration must produce a valid probability vector whose values sum to one. Temperature scaling naturally does this. One-vs-rest calibrators can require additional normalization and may calibrate each class unevenly.
Distribution Shift and Monitoring
Calibration describes a data distribution. If customer behavior, sensors, or class frequencies change, old probabilities can become unreliable. Monitor predicted-probability bins, observed outcome rates, log loss, and Brier score over time. Recalibrate using recent, representative labeled data when performance drifts.
Thresholds and Decisions
A calibrated probability supports decisions based on cost. For example, approve an action when the probability of success exceeds a threshold derived from its benefit and failure cost. Calibration does not choose that threshold for you. Validate both calibration and the resulting decision rate on future-like data.
Exercise
Which method is usually safer with a small calibration set: sigmoid or isotonic?
Show answer
Sigmoid calibration is usually safer because isotonic regression can overfit small samples.