ConformalRegressor¶
Read more in the User Guide.
Reference¶
class conformity.ConformalRegressor(
estimator,
*,
auto_calibrate=True,
tts_kwargs=None,
interval_method='symmetric',
)Split-conformal prediction intervals for single-output regression.
Parameters¶
estimatorBaseEstimatorCloneable sklearn regressor or preprocessing-and-regression pipeline.
auto_calibratebool, default=TrueReserve held-out calibration data during fit; defaults to True.
tts_kwargsTrainTestSplitKwargs or None, default=Nonetrain_test_split options for automatic calibration. By default, test_size=0.2, shuffle=True and stratify=None.
interval_method{"symmetric", "asymmetric"}, default='symmetric'Symmetric absolute-residual calibration (default), or separate signed residual quantiles with alpha / 2 allocated to each tail.
Attributes¶
lower_q_level_floatMost recent lower correction for asymmetric intervals.
upper_q_level_floatMost recent upper correction for asymmetric intervals.
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.datasets import make_regression
>>> from sklearn.linear_model import Ridge
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.pipeline import Pipeline
>>> from sklearn.preprocessing import StandardScaler
>>> from conformity import ConformalRegressor
>>> X, y = make_regression(
... n_samples=120, n_features=3, noise=4, random_state=0
... )
>>> X_dev, X_test, y_dev, y_test = train_test_split(
... X, y, test_size=0.25, random_state=1
... )
>>> X_train, X_calib, y_train, y_calib = train_test_split(
... X_dev, y_dev, test_size=1 / 3, random_state=2
... )
>>> model = ConformalRegressor(
... Pipeline([("scaler", StandardScaler()), ("ridge", Ridge())]),
... auto_calibrate=False,
... )
>>> _ = model.fit(X_train, y_train)
>>> _ = model.calibrate(X_calib, y_calib)
>>> points, intervals = model.predict(X_test, alpha=0.1, return_interval=True)
>>> points.shape, intervals.shape
((30,), (30, 2))
>>> model.n_calibration_
30
Methods¶
| Method | Purpose |
|---|---|
| calibrate | Replace held-out scores and return this predictor for method chaining. |
| evaluate | Return point error, coverage, tail misses, width and interval score. |
| 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. |
| predict | Return points or (points, intervals). |
| predict_interval | Return [lower, upper] bounds with shape (n_samples, 2). |
| predict_point | Return point predictions without requiring calibration. |
| score | Return point-prediction R², using point predictions. |
| set_fit_request | Request metadata passed to the fit method. |
| set_params | Set sklearn parameters and invalidate fitted and calibrated state. |
| set_predict_request | Request metadata passed to the predict method. |
| set_score_request | Request metadata passed to the score method. |
| ### calibrate |
calibrate(X, y)Replace held-out scores and return this predictor for method chaining.
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.
evaluate¶
evaluate(
X,
y,
alpha=0.05,
*,
sample_weight=None,
)Return point error, coverage, tail misses, width and interval score.
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.
alphafloat, default=0.05Miscoverage probability strictly between zero and one.
sample_weightarray-like of shape (n_samples,) or None, default=NoneOptional finite, nonnegative weights; fitting or evaluation only, never calibration.
Returns¶
reportdict of str to floatKeys are r2, mae, mse, coverage, lower_miss_rate, upper_miss_rate, mean_width, normalized_mean_width and interval_score. Normalized width divides mean width by the unweighted test-target IQR and is infinite when that IQR is zero. Tail miss rates are empirical diagnostics; for asymmetric intervals compare each with alpha / 2, allowing for finite test-sample variation.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
RuntimeErrorIf conformal output is requested before calibration.
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.
predict¶
predict(
X,
alpha=0.05,
*,
return_interval=False,
)Return points or (points, intervals).
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.
alphafloat, default=0.05Miscoverage probability strictly between zero and one.
return_intervalbool, default=FalseRequest conformal output alongside ordinary predictions.
Returns¶
predictionsndarray or tuple of ndarrayPoint predictions, or (points, intervals) when return_interval=True.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
RuntimeErrorIf conformal output is requested before calibration.
predict_interval¶
predict_interval(X, alpha=0.05)Return [lower, upper] bounds with shape (n_samples, 2).
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.
alphafloat, default=0.05Miscoverage probability strictly between zero and one.
Returns¶
intervalsndarray of shape (n_samples, 2)Closed lower and upper bounds; may be outward infinite.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
RuntimeErrorIf conformal output is requested before calibration.
predict_point¶
predict_point(X)Return point predictions without requiring calibration.
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.
Returns¶
predictionsndarray of shape (n_samples,)Point predictions from the fitted estimator.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
score¶
score(
X,
y,
sample_weight=None,
)Return point-prediction R², using point predictions.
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.
sample_weightarray-like of shape (n_samples,) or None, default=NoneOptional finite, nonnegative weights; fitting or evaluation only, never calibration.
Returns¶
scorefloatPoint-prediction R² for regression or accuracy for classification.
Raises¶
ValueErrorIf input shapes, values, weights or configuration are invalid.
NotFittedErrorIf fit has not established a fitted estimator.
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.
set_predict_request¶
set_predict_request(
*,
alpha='$UNCHANGED$',
return_interval='$UNCHANGED$',
)Request metadata passed to the predict method.
Parameters¶
alphastr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
alphaparameter inpredict.return_intervalstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
return_intervalparameter inpredict.
Returns¶
selfobjectThe updated object.
set_score_request¶
set_score_request(*, sample_weight='$UNCHANGED$')Request metadata passed to the score method.
Parameters¶
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED, default='$UNCHANGED$'Metadata routing for
sample_weightparameter inscore.
Returns¶
selfobjectThe updated object.
is_calibrated_¶
Report whether calibration scores are available.