BaseConformalPredictor¶
Read more in the User Guide.
Reference¶
class conformity.BaseConformalPredictor(
estimator,
*,
auto_calibrate=True,
tts_kwargs=None,
)Wrap a cloned estimator with held-out, unweighted split calibration.
Parameters¶
estimatorBaseEstimatorCloneable sklearn estimator; classifiers also require predict_proba.
auto_calibratebool, default=TrueReserve held-out calibration data during fit; defaults to True.
tts_kwargsTrainTestSplitKwargs or None, default=Nonetrain_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_BaseEstimatorFitted clone; created by fit and removed by parameter changes.
n_features_in_intNumber of validated fitting features.
feature_names_in_ndarrayInput names, present when fitting named string columns.
classes_ndarrayClassifier labels in probability and membership-column order.
calibration_scores_ndarrayImmutable held-out scores; created by successful calibration.
sorted_calibration_scores_ndarrayImmutable sorted scores cached for rank queries.
n_calibration_intNumber of held-out calibration observations.
alpha_used_floatMiscoverage probability from the most recent threshold query.
quantile_level_floatFinite-sample rank divided by calibration count; may exceed one.
q_level_floatMost recent threshold, including infinity when the rank exceeds n.
is_calibrated_boolWhether 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 |
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.
yarray-like of shape (n_samples,)One target per sample; classification labels follow the fitted classes.
Returns¶
selfobjectPredictor with replacement calibration scores.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
fit¶
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.
yarray-like of shape (n_samples,)One target per sample; classification labels follow the fitted classes.
auto_calibratebool or None, default=NoneReserve an independent calibration split when fitting.
tts_kwargsTrainTestSplitKwargs or None, default=Nonetrain_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_statehere for reproducible automatic splits.sample_weightarray-like of shape (n_samples,) or None, default=NoneOptional finite, nonnegative weights; fitting or evaluation only, never calibration.
**fit_paramsdictEstimator-specific fit metadata forwarded to the cloned estimator.
Returns¶
selfobjectFitted predictor; calibrated when automatic calibration is enabled.
Raises¶
TypeErrorIf the estimator cannot be cloned or lacks the required prediction methods.
ValueErrorIf input shapes, values, weights or configuration are invalid.
get_metadata_routing¶
get_metadata_routing()Get metadata routing of this object.
Returns¶
routingMetadataRequestA :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating routing information.
get_params¶
get_params(deep=True)Get parameters for this estimator.
Parameters¶
deepbool, default=True, default=TrueIf True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns¶
paramsdictParameter names mapped to their values.
set_fit_request¶
set_fit_request(
*,
auto_calibrate='$UNCHANGED$',
sample_weight='$UNCHANGED$',
tts_kwargs='$UNCHANGED$',
)Request metadata passed to the fit method.
Parameters¶
auto_calibratestr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
auto_calibrateparameter infit.sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
sample_weightparameter infit.tts_kwargsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
tts_kwargsparameter infit.
Returns¶
selfobjectThe updated object.
set_params¶
set_params(**params)Set sklearn parameters and invalidate fitted and calibrated state.
Parameters¶
**paramsdictSklearn parameters, including nested estimator parameters.
Returns¶
selfobjectPredictor with updated parameters and invalidated fitted state.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
is_calibrated_¶
Report whether calibration scores are available.