#358Platt ScalingMediumLogistic RegressionEvaluation MetricsAsked atMeta · Google · Microsoft
Platt Scaling
Background
Post-hoc calibration via Platt scaling fits a 2-parameter logistic on top of raw model scores so the resulting probabilities match observed frequencies. It is the cheapest, most-cited fix for the canonical "AUC is great but the probabilities are wrong" failure mode: held-out scores in, calibrated probabilities out, no retrain.
Problem statement
Implement platt_fit(scores, labels, n_iter=500, lr=0.5) that fits parameters minimising binary cross-entropy:
via gradient descent. Return (a, b) as two Python floats. Initialise .
Input
scores- array-like of floats, raw model scores (any real range).labels- array-like of , same length asscores.n_iter-int, gradient steps (default ).lr-float, learning rate (default ).
Output
(a, b)- tuple of plain Python floats.
Examples
Example 1 - perfectly separable labels -> large |a|, b shifts to threshold
Input: scores = [-3, -2, -1, 1, 2, 3], labels = [0, 0, 0, 1, 1, 1]
Output: (a, b) such that sigmoid(a*s + b) ~= labels
Constraints
scoresandlabelsmust be the same length, length .- Use the numerically stable sigmoid for negative arguments:
1 - sigmoid(-z)for , or equivalentlynp.where(z >= 0, 1/(1+exp(-z)), exp(z)/(1+exp(z))). Tests use long-tailed scores that overflow naiveexp. - Tests check that after fitting, the mean of matches the mean of to within and that the predictions correctly order the classes on the separable example.
Notes
- Why init. Starting at the identity () means the first iteration's gradient is dominated by the score-label mismatch, not by an arbitrary scale.
- Newton vs gradient descent. Production Platt fits use a Newton step (the Hessian is 2x2 and trivially invertible) - convergence in 5-10 iterations vs hundreds for plain GD. The contract here uses GD for clarity; the variants section names Newton as the upgrade.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: separable data fit parameters
- •Reference: mixed labels fit parameters
- •Sample: five-point fit parameters