ClassifierCalibrator#

class mlquantify.calibration.ClassifierCalibrator(method='bcts', input_type='proba')[source]#

Post-hoc calibration of classifier posteriors by logit scaling.

Parameters:
method{‘ts’, ‘bcts’, ‘vs’, ‘nbvs’}, default=’bcts’

Calibration map to fit:

  • 'ts' – Temperature Scaling (one temperature).

  • 'bcts' – Bias-Corrected Temperature Scaling (temperature + biases).

  • 'vs' – Vector Scaling (per-class weights + biases).

  • 'nbvs' – No-Bias Vector Scaling (per-class weights).

input_type{‘proba’, ‘logits’}, default=’proba’

Whether y_pred holds probabilities (mapped to centred logits before scaling) or raw logits.

Attributes:
weights_ndarray of shape (n_classes,)

Fitted multiplicative factors applied to the logits.

biases_ndarray of shape (n_classes,)

Fitted additive biases (zeros for 'ts' and 'nbvs').

classes_ndarray

Distinct labels seen in fit (only set for 1-D y_true).

n_features_in_int

Number of classes (logit columns) seen in fit.

Notes

Calibration must be fit on predictions held out from classifier training (e.g. a validation split or cross-validated predictions); fitting it on the classifier’s own training predictions under-estimates the miscalibration.

Examples

>>> import numpy as np
>>> from mlquantify.calibration import ClassifierCalibrator
>>> proba = np.array([[0.6, 0.4], [0.3, 0.7], [0.8, 0.2]])
>>> y = np.array([0, 1, 0])
>>> cal = ClassifierCalibrator(method="ts").fit(y, proba)
>>> calibrated = cal.predict(proba)
>>> np.allclose(calibrated.sum(axis=1), 1.0)
True
fit(y_true, y_pred)[source]#

Fit the scaling map on held-out labels y_true and outputs y_pred.

fit_predict(y_true, y_pred)[source]#

Convenience: fit(y_true, y_pred) then predict(y_pred).

predict(y_pred)[source]#

Return calibrated probabilities for y_pred.