Skip to content
ConformityBaseConformalPredictor

BaseConformalPredictor

Read more in the User Guide.

Reference

[source]
class conformity.BaseConformalPredictor(
    estimator,
    *,
    auto_calibrate=True,
    tts_kwargs=None,
)

Wrap a cloned estimator with held-out, unweighted split calibration.

Parameters

estimator BaseEstimator

Cloneable sklearn estimator; classifiers also require predict_proba.

auto_calibrate bool, default=True

Reserve held-out calibration data during fit; defaults to True.

tts_kwargs TrainTestSplitKwargs or None, default=None

train_test_split options used for automatic calibration. By default, test_size is 0.2, shuffle is True, and stratify is None. An explicit test_size here overrides the default.

Attributes

estimator_ BaseEstimator

Fitted clone; created by fit and removed by parameter changes.

n_features_in_ int

Number of validated fitting features.

feature_names_in_ ndarray

Input names, present when fitting named string columns.

classes_ ndarray

Classifier labels in probability and membership-column order.

calibration_scores_ ndarray

Immutable held-out scores; created by successful calibration.

sorted_calibration_scores_ ndarray

Immutable sorted scores cached for rank queries.

n_calibration_ int

Number of held-out calibration observations.

alpha_used_ float

Miscoverage probability from the most recent threshold query.

quantile_level_ float

Finite-sample rank divided by calibration count; may exceed one.

q_level_ float

Most recent threshold, including infinity when the rank exceeds n.

is_calibrated_ bool

Whether held-out scores exist. Refitting clears calibration state.

Examples

>>> from sklearn.linear_model import Ridge
>>> from conformity import BaseConformalPredictor, ConformalRegressor
>>> issubclass(ConformalRegressor, BaseConformalPredictor)
True
>>> model = ConformalRegressor(Ridge(), tts_kwargs={"test_size": 0.25})
>>> model.get_params()["tts_kwargs"]
{'test_size': 0.25}
>>> model.is_calibrated_
False

Methods

Method Purpose
calibrate Replace calibration scores using data independent of model fitting.
fit Fit a fresh clone; optionally reserve an independent calibration split.
get_metadata_routing Get metadata routing of this object.
get_params Get parameters for this estimator.
set_fit_request Request metadata passed to the fit method.
set_params Set sklearn parameters and invalidate fitted and calibrated state.
### calibrate
[source]
calibrate(X, y)

Replace calibration scores using data independent of model fitting.

Parameters

X {array-like, sparse matrix, DataFrame} of shape (n_samples, n_features)

Feature matrix of shape (n_samples, n_features); dense, sparse, or DataFrame.

y array-like of shape (n_samples,)

One target per sample; classification labels follow the fitted classes.

Returns

self object

Predictor with replacement calibration scores.

Raises

ValueError

If input shapes, values, weights or configuration are invalid.

NotFittedError

If fit has not established a fitted estimator.

fit

[source]
fit(
    X,
    y,
    auto_calibrate=None,
    tts_kwargs=None,
    *,
    sample_weight=None,
    **fit_params,
)

Fit a fresh clone; optionally reserve an independent calibration split.

Parameters

X {array-like, sparse matrix, DataFrame} of shape (n_samples, n_features)

Feature matrix of shape (n_samples, n_features); dense, sparse, or DataFrame.

y array-like of shape (n_samples,)

One target per sample; classification labels follow the fitted classes.

auto_calibrate bool or None, default=None

Reserve an independent calibration split when fitting.

tts_kwargs TrainTestSplitKwargs or None, default=None

train_test_split options for this fit; requires automatic calibration. If provided, this mapping replaces the constructor mapping. Missing options use defaults: test_size=0.2, train_size=None, random_state=None, shuffle=True and stratify=None. Set random_state here for reproducible automatic splits.

sample_weight array-like of shape (n_samples,) or None, default=None

Optional finite, nonnegative weights; fitting or evaluation only, never calibration.

**fit_params dict

Estimator-specific fit metadata forwarded to the cloned estimator.

Returns

self object

Fitted predictor; calibrated when automatic calibration is enabled.

Raises

TypeError

If the estimator cannot be cloned or lacks the required prediction methods.

ValueError

If input shapes, values, weights or configuration are invalid.

get_metadata_routing

[inherited API]
get_metadata_routing()

Get metadata routing of this object.

Returns

routing MetadataRequest

A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating routing information.

get_params

[inherited API]
get_params(deep=True)

Get parameters for this estimator.

Parameters

deep bool, default=True, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params dict

Parameter names mapped to their values.

set_fit_request

[inherited API]
set_fit_request(
    *,
    auto_calibrate='$UNCHANGED$',
    sample_weight='$UNCHANGED$',
    tts_kwargs='$UNCHANGED$',
)

Request metadata passed to the fit method.

Parameters

auto_calibrate str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'

Metadata routing for auto_calibrate parameter in fit.

sample_weight str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'

Metadata routing for sample_weight parameter in fit.

tts_kwargs str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'

Metadata routing for tts_kwargs parameter in fit.

Returns

self object

The updated object.

set_params

[source]
set_params(**params)

Set sklearn parameters and invalidate fitted and calibrated state.

Parameters

**params dict

Sklearn parameters, including nested estimator parameters.

Returns

self object

Predictor with updated parameters and invalidated fitted state.

Raises

ValueError

If input shapes, values, weights or configuration are invalid.

is_calibrated_

Report whether calibration scores are available.