Sub topic 9
Platt scaling
This chapter assumes you know logistic regression, the sigmoid function, and how to split data into train and validation sets. Platt scaling is a post-hoc method. It takes a trained classifier's raw scores and maps them to better probabilities.
The idea
Platt scaling fits a one-feature logistic regression on top of the classifier's scores. For a score s, the calibrated probability is:
p = 1 / (1 + exp(A·s + B))
A and B are two numbers learned from labeled data. A controls how steep the curve is. B shifts it left or right. The classifier itself is not changed. Only the mapping from score to probability is learned.
The scores can be decision-function values, such as SVM margins, or log-odds from any model. Platt's original use was with support vector machines, whose margins are not probabilities at all.
Why a sigmoid fits some models
The sigmoid shape corrects a specific distortion. Margin-based models such as SVMs and boosted trees tend to push predictions away from 0 and 1, so the reliability curve looks like an S. A sigmoid can undo that. Platt scaling is therefore a good fit when the distortion is smooth and symmetric.
It is a poor fit when the distortion has some other shape. A sigmoid cannot fix a curve that bends one way in one region and the other way elsewhere. Naive Bayes often produces probabilities that are too extreme, and a sigmoid helps there, but it will not remove every error.
Fitting it
The calibrator must be fitted on data the base model did not train on. If you reuse the training data, the scores are overconfident and the sigmoid learns the wrong correction. This is the leakage problem from the previous chapter.
Here is a manual fit using a held-out calibration set:
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import brier_score_loss
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)
svm = LinearSVC().fit(X_train, y_train)
# Fit A and B on held-out scores
s_cal = svm.decision_function(X_cal).reshape(-1, 1)
platt = LogisticRegression().fit(s_cal, y_cal)
s_test = svm.decision_function(X_test).reshape(-1, 1)
p_test = platt.predict_proba(s_test)[:, 1]
print("Brier score after Platt scaling:", brier_score_loss(y_test, p_test))Here LogisticRegression learns the slope and intercept. Its default L2 penalty is mild with one feature, but you can set C large to approximate an unpenalized fit.
Properties and common errors
- Platt scaling is monotonic when A is negative in the formula above. It does not change the ranking of examples, so AUC is unchanged. Accuracy at the 0.5 threshold can shift slightly if the learned offset moves the 0.5 point.
- It has only two parameters, so it works with a few hundred calibration examples. Isotonic regression needs more data.
- Fitting on training scores is the most common mistake. Always check that the calibration data is separate.
- Platt's paper also smoothed the target labels to avoid overfitting. Scikit-learn does this internally in its sigmoid method.
- If the calibration set is tiny or unrepresentative, the fitted sigmoid can be worse than none.
Exercise
A reliability curve for an SVM looks like a flattened S: predictions near 0.2 are right 10% of the time, and predictions near 0.8 are right 90% of the time. Would Platt scaling be a reasonable first fix, and why must the calibration data be separate from the training data?
Show answer
Yes. A sigmoid with a steeper slope can pull 0.2 down and push 0.8 up, which matches this smooth S-shaped distortion. The calibration data must be separate because the base model's scores on its own training data are more confident than on new data, so a sigmoid fitted to them would learn a correction that does not apply at prediction time.