Platt Scaling
Platt scaling turns a classifier’s raw scores into calibrated probabilities by fitting a logistic curve to them. With \(f(x)\) the score โ an SVM decision value, a logit, a boosting margin โ the calibrated probability is
\[ P(y = 1 \mid f(x)) = \frac{1}{1 + \exp\big(A f(x) + B\big)} \]
with \(A\) and \(B\) fitted by minimizing negative log-likelihood. It was introduced for support vector machines, whose decision values are not probabilities at all, and is now applied to anything that produces a score with the right ordering and the wrong scale โ boosted trees being the common case, since they tend to be overconfident.
Fitting it
The calibrator must be fitted on data the classifier did not train on. scikit-learn’s documentation is precise about why, and the reason is a directional bias rather than generic overfitting: “using the classifier output of training data to fit the calibrator would thus result in a biased calibrator that maps to probabilities closer to 0 and 1 than it should.” The model looks more separable on its training data than it is, so the fitted sigmoid is too steep and the calibrated probabilities come out more confident than before โ the opposite of the intended effect.
In practice that means a dedicated calibration split, or cross-validated calibration where the calibrator is fitted on out-of-fold predictions. The second wastes no data and is what library implementations do by default.
When the ranking is preserved
This is the property that decides when calibration is worth doing, and it is rarely stated. Fitted with a positive slope on a single scalar score, the logistic map is strictly increasing, so it preserves the order exactly and rank-based metrics โ AUC, average precision, the ordering of a review queue โ are unchanged in exact arithmetic. Numerical saturation or rounding can introduce ties. Two conditions are doing work there. A negative fitted slope reverses the order rather than preserving it, which is what happens when the score runs the wrong way. And the guarantee is about the ordering: a decision taken at a fixed probability cutoff such as 0.5 can select a different set afterward, because the cutoff now sits somewhere else on the score. A cutoff expressed as a quantile is unaffected.
import numpy as np
from sklearn.isotonic import IsotonicRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import brier_score_loss, roc_auc_score
def overconfident_model(n, seed):
"""Scores that rank correctly but are pushed toward the extremes."""
rng = np.random.default_rng(seed)
p_true = rng.beta(2, 5, n)
y = (rng.uniform(size=n) < p_true).astype(int)
scores = np.clip(p_true * 1.6, 1e-6, 1 - 1e-6)
return y, scores
y, scores = overconfident_model(40000, seed=6)
# The calibrator is fitted on held-out data and judged on data neither model has seen.
fit, test = slice(0, 20000), slice(20000, 40000)
logit = np.log(scores / (1 - scores)).reshape(-1, 1)
platt = LogisticRegression().fit(logit[fit], y[fit]).predict_proba(logit[test])[:, 1]
isotonic = IsotonicRegression(out_of_bounds="clip").fit(scores[fit], y[fit]).predict(scores[test])
for name, p in (("raw", scores[test]), ("platt", platt), ("isotonic", isotonic)):
print(f"{name:9} {brier_score_loss(y[test], p):.4f} {roc_auc_score(y[test], p):.6f}")
# raw 0.2176 0.714483
# platt 0.1895 0.714483
# isotonic 0.1808 0.713704
| Scores | Brier score | AUC |
|---|---|---|
| Raw, overconfident | 0.2176 | 0.714483 |
| Platt scaled | 0.1895 | 0.714483 |
| Isotonic | 0.1808 | 0.713704 |
Platt scaling cuts the Brier score by 13 percent and leaves the AUC identical โ not approximately, identically, because a strictly increasing function cannot reorder anything. Isotonic does better on the Brier score here and moves the AUC very slightly, because it is monotone non-decreasing rather than strictly increasing: it maps ranges of scores to the same probability, creating ties where there were none, and ties are what AUC notices.
So the decision rule is simple. If nothing downstream reads the probability values โ a fixed number of cases will be reviewed in score order โ calibration buys nothing at all. If a threshold is set from a probability, if the numbers feed an expected-value calculation, or if a person reads them as risks, it buys the thing that matters and costs nothing that does.
Sigmoid or isotonic
| Platt scaling (sigmoid) | Isotonic regression | |
|---|---|---|
| Form | Two parameters, a fixed shape | Non-parametric, a non-decreasing function fitted as flat steps at the training points |
| What it can fix | Distortions the sigmoid shape can express | “Any monotonic distortion of the un-calibrated model” |
| Data needed | “Most effective for small sample sizes” | “As well as or better than ‘sigmoid’ when there is enough data (greater than ~ 1000 samples) to avoid overfitting” |
| Risk | Underfits a distortion that is not sigmoid-shaped | “More prone to overfitting, especially on small datasets” |
| Output | Smooth | Piecewise linear between fitted points in scikit-learn, with flat stretches that create ties |
The documentation adds one more condition under which the sigmoid is the right choice: when the uncalibrated model “is under-confident and has similar calibration errors for both high and low outputs” โ that is, when the miscalibration is symmetric. Heavy class imbalance can produce asymmetric calibration errors, which a sigmoid may fit poorly. Imbalance alone does not establish that the distortion is asymmetric; inspect the calibration pattern and validate the fit.
Neither method can repair a non-monotonic distortion, because both are monotone by construction. If a model’s scores are non-monotonically related to the true probability, the problem is the model rather than its scale.
Temperature scaling
For neural networks the standard variant drops one of the two parameters: divide the logits by a single learned temperature before the softmax. Guo and colleagues, who found that “modern neural networks, unlike those from a decade ago, are poorly calibrated”, describe it as “a single-parameter variant of Platt Scaling” and report that it is “surprisingly effective at calibrating predictions” on most of the datasets they tried.
Having one parameter rather than two matters beyond simplicity. A single temperature scales all class logits identically, so in the multiclass case it cannot change which class is predicted โ the accuracy of the network is untouched, and only the confidence moves. That is the multiclass analog of the AUC invariance above, and it is why top-1 predictions and their accuracy stay the same when the method is applied to a model already in production. Decisions that read the probabilities, such as a probability threshold, an abstain rule or an expected-cost calculation, can still change and should be validated separately.
Checking it worked
Calibration is fitted, so it can fail like any fit. Evaluate on data used for neither training nor calibration, look at a reliability curve rather than only a summary number, and check a proper scoring rule such as the Brier score alongside expected calibration error โ the latter has a bias that makes small improvements hard to read.
One failure mode is worth naming: calibration is fitted to a distribution, and it decays when the distribution moves. A calibrator trained on last quarter’s traffic can be as stale as the model, and a shift that leaves the ranking intact can still wreck the probabilities. What calibration means and when it matters is under model calibration.
References
- scikit-learn User Guide, Probability calibration
- Chuan Guo, Geoff Pleiss, Yu Sun and Kilian Q. Weinberger, “On Calibration of Modern Neural Networks”, ICML 2017, arXiv:1706.04599
- Alexandru Niculescu-Mizil and Rich Caruana, “Predicting Good Probabilities With Supervised Learning”, ICML 2005
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
