calpit._sklearn_backend

Cal-PIT with scikit-learn classifiers.

A scikit-learn estimator is fitted once on a fixed data set, so the fresh alpha per item and epoch of the PyTorch backend is replaced by n_alpha stratified draws of alpha per object (calpit.coverage.stratified_alpha), expanded into (alpha, x) rows (calpit.coverage.expand_coverage).

Functions

is_sklearn_classifier(→ bool)

Returns whether model looks like a scikit-learn classifier with predict_proba.

fit_sklearn(→ Any)

Fits a clone of a scikit-learn classifier to the indicators PIT <= alpha.

predict_sklearn(→ numpy.ndarray)

Predicts r(alpha; x) = P(PIT <= alpha | x) with a fitted classifier.

Module Contents

is_sklearn_classifier(model: object) → bool[source]

Returns whether model looks like a scikit-learn classifier with predict_proba.

fit_sklearn(model: Any, x: numpy.ndarray, pit: numpy.ndarray, n_alpha: int, random_state: calpit.coverage.SeedLike) → Any[source]

Fits a clone of a scikit-learn classifier to the indicators PIT <= alpha.

If the classifier has a monotonic_cst parameter left at None, as HistGradientBoostingClassifier does, the clone is constrained to increase in alpha, so its predictions are non-decreasing in alpha without rearrangement.

Parameters:
  • model – The unfitted classifier; it is cloned, not changed.

  • x – The features, shape (n_objects, n_features).

  • pit – The PIT values, shape (n_objects,).

  • n_alpha – The number of stratified alpha per object.

  • random_state – The seed or generator of the alpha draws.

Returns:

The fitted clone.

predict_sklearn(estimator: Any, x: numpy.ndarray, alpha: numpy.typing.ArrayLike, n_knots: int, batch_size: int = 2048) → numpy.ndarray[source]

Predicts r(alpha; x) = P(PIT <= alpha | x) with a fitted classifier.

Classifiers such as tree ensembles are piecewise constant in alpha, and a staircase in alpha would turn into spikes in the recalibrated densities. So r is predicted at n_knots + 1 evenly spaced alpha from 0 to 1 and interpolated linearly between them, which keeps it continuous and, for a classifier constrained to increase in alpha, monotone.

Parameters:
  • estimator – The classifier, fitted on [alpha, x] rows with boolean targets.

  • x – The features, shape (n_objects, n_features).

  • alpha – The coverage levels, shape (n_alpha,) or (n_objects, n_alpha).

  • n_knots – The number of intervals between the alpha knots.

  • batch_size – The number of objects per call to predict_proba.

Returns:

The predicted PIT CDF, shape (n_objects, n_alpha).