Platt (sigmoid) probability calibration.
Classes
Probability calibration by fitting a sigmoid to held-out scores.
__init__( self, max_iter: int = 100, tol: float = 1e-10, regularize_targets: bool = True) -> None, )
Overview
- Hold out a calibration set that the model was not trained on.
- Replace the hard labels with Platt's regularised targets, which pull the
- Fit A and B by Newton descent on the log-likelihood.
- At transform time, apply the fitted sigmoid to new scores.
Theory
The calibration map is
fitted by minimising the regularised cross-entropy
where the targets follow Platt's correction for N_+ positive and N_- negative calibration samples:
A negative A gives the usual increasing map; the sign is learned, so the calibrator also handles scores oriented the other way round.
Parameters
max_iter
tol
regularize_targets
Attributes
a_
b_
n_iter_
classes_
fit.
fitted_
fit has been called.
Notes
Complexity. O(n) per Newton iteration, O(1) memory beyond the score vector. Transform is O(m).
When to use. Platt scaling is the right default when the calibration set is small (a few hundred samples) or the miscalibration is a smooth monotone squashing — the typical SVM or AdaBoost case. When the calibration set is large or the distortion is not sigmoidal, prefer IsotonicCalibrator.
References
See Also
>>> import numpy as np
>>> from tuiml.uncertainty import PlattCalibrator
>>> rng = np.random.default_rng(0)
>>> scores = np.concatenate([rng.normal(-1, 1, 200), rng.normal(1, 1, 200)])
>>> y = np.concatenate([np.zeros(200), np.ones(200)])
>>> cal = PlattCalibrator().fit(scores, y)
>>> proba = cal.transform(np.array([-2.0, 0.0, 2.0]))
>>> bool(proba[0] < proba[1] < proba[2])
True
Methods
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'PlattCalibrator'
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'PlattCalibrator'
Fit the sigmoid on held-out scores.
Parameters
scores
y
Returns
self