TemperatureScaling

class pycalib.calibration_methods.TemperatureScaling(T_init=1, verbose=False)[source]

Bases: pycalib.calibration_methods.CalibrationMethod

Probability calibration using temperature scaling

Temperature scaling 1 is a one parameter multi-class scaling method. Output confidence scores are calibrated, meaning they match empirical frequencies of the associated class prediction. Temperature scaling does not change the class predictions of the underlying model.

Parameters
  • T_init (float) – Initial temperature parameter used for scaling. This parameter is optimized in order to calibrate output probabilities.

  • verbose (bool) – Print information on optimization procedure.

References

1

On calibration of modern neural networks, C. Guo, G. Pleiss, Y. Sun, K. Weinberger, ICML 2017

Methods Summary

fit(X, y)

Fit the calibration method based on the given uncalibrated class probabilities or logits X and ground truth labels y.

get_params([deep])

Get parameters for this estimator.

latent(z)

Evaluate the latent function Tz of temperature scaling.

plot(filename[, xlim])

Plot the calibration map.

plot_latent(z, filename, **kwargs)

Plot the latent function of the calibration method.

predict(X)

Predict the class of new samples after scaling.

predict_proba(X)

Compute calibrated posterior probabilities for a given array of posterior probabilities from an arbitrary classifier.

set_params(**params)

Set the parameters of this estimator.

Methods Documentation

fit(X, y)[source]

Fit the calibration method based on the given uncalibrated class probabilities or logits X and ground truth labels y.

Parameters
  • X (array-like, shape (n_samples, n_classes)) – Training data, i.e. predicted probabilities or logits of the base classifier on the calibration set.

  • y (array-like, shape (n_samples,)) – Target classes.

Returns

self – Returns an instance of self.

Return type

object

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

mapping of string to any

latent(z)[source]

Evaluate the latent function Tz of temperature scaling.

Parameters

z (array-like, shape=(n_evaluations,)) – Input confidence for which to evaluate the latent function.

Returns

f – Values of the latent function at z.

Return type

array-like, shape=(n_evaluations,)

plot(filename, xlim=[0, 1], **kwargs)

Plot the calibration map.

Parameters
  • xlim (array-like) – Range of inputs of the calibration map to be plotted.

  • **kwargs – Additional arguments passed on to matplotlib.plot().

plot_latent(z, filename, **kwargs)[source]

Plot the latent function of the calibration method.

Parameters
  • z (array-like, shape=(n_evaluations,)) – Input confidence to plot latent function for.

  • filename – Filename / -path where to save output.

  • kwargs – Additional arguments passed on to matplotlib.pyplot.subplots.

predict(X)

Predict the class of new samples after scaling. Predictions are identical to the ones from the uncalibrated classifier.

Parameters

X (array-like, shape (n_samples, n_classes)) – The uncalibrated posterior probabilities.

Returns

C – The predicted classes.

Return type

array, shape (n_samples,)

predict_proba(X)[source]

Compute calibrated posterior probabilities for a given array of posterior probabilities from an arbitrary classifier.

Parameters

X (array-like, shape (n_samples, n_classes)) – The uncalibrated posterior probabilities.

Returns

P – The predicted probabilities.

Return type

array, shape (n_samples, n_classes)

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

object