pitcp.models.CQR¶
- class CQR(estimator, *, confidence_level=0.9, gamma=0.5)[source]¶
Fits conformalized quantile intervals with a boosting estimator.
The estimator can be a scikit-learn histogram gradient booster or an optional LightGBM, XGBoost, or CatBoost regressor. Backend-specific quantile models are fitted independently for every target output.
- Quantile estimation settings:
estimator: Unfitted regression prototype cloned into lower- and upper-quantile estimators for every target output. Supported backends are histogram gradient boosting, LightGBM, XGBoost, and CatBoost.confidence_level: Target marginal coverage probability in the open interval from zero to one. Defaults to 0.9.gamma: Fraction of total miscoverage assigned to the lower tail, from zero to one. Defaults to 0.5, which gives equal-tailed intervals.
- Variables:
estimator (BaseEstimator) – Unfitted quantile-regression prototype.
confidence_level (float) – Desired marginal coverage level.
gamma (float) – Miscoverage fraction assigned to the lower tail.
estimators (list[QuantileEstimator]) – Fitted quantile adapter for each target output.
scores (np.ndarray) – Joint calibration scores.
correction (float) – Finite-sample conformal correction.
- Parameters:
estimator (BaseEstimator)
confidence_level (float)
gamma (float)
Examples
>>> from sklearn.ensemble import HistGradientBoostingRegressor >>> from pitcp import CQR >>> model = CQR(HistGradientBoostingRegressor(), confidence_level=0.9) >>> model.fit([[0.0], [1.0], [2.0]], [0.0, 1.0, 2.0]) CQR(...) >>> model.conformalize([[3.0], [4.0]], [3.0, 4.0]) CQR(...) >>> model.predict([[5.0]]).shape (1, 2)
- __init__(estimator, *, confidence_level=0.9, gamma=0.5)[source]¶
Initializes the conformalized quantile estimator.
- Parameters:
estimator (BaseEstimator) – HistGradientBoosting, LightGBM, XGBoost, or CatBoost regressor.
confidence_level (float, optional) – Coverage level. Defaults to 0.9.
gamma (float, optional) – Miscoverage assigned to the lower tail. Defaults to 0.5.
- fit(X, y)[source]¶
Fits cloned quantile estimators.
- Parameters:
X (np.typing.ArrayLike) – Training features with shape
(n_samples, n_features).y (np.typing.ArrayLike) – Targets with shape
(n_samples,)or(n_samples, n_outputs).
- Returns:
The fitted estimator.
- Return type:
Self
- Raises:
TypeError – If the estimator backend is unsupported.
- conformalize(X, y)[source]¶
Calibrates a joint correction on held-out targets.
- Parameters:
X (np.typing.ArrayLike) – Calibration features with shape
(n_samples, n_features).y (np.typing.ArrayLike) – Calibration targets with shape
(n_samples,)or(n_samples, n_outputs).
- Returns:
The calibrated estimator.
- Return type:
Self
- predict(X)[source]¶
Predicts calibrated lower and upper bounds.
- Parameters:
X (np.typing.ArrayLike) – Features with shape
(n_samples, n_features).- Returns:
- Bounds with shape
(n_samples, 2, n_outputs), with a singleton output axis collapsed.
- Bounds with shape
- Return type:
np.ndarray
- contains(X, y)[source]¶
Tests whether targets lie inside every output interval.
- Parameters:
X (np.typing.ArrayLike) – Features with shape
(n_samples, n_features).y (np.typing.ArrayLike) – Targets with shape
(n_samples,)or(n_samples, n_outputs).
- Returns:
One boolean per sample.
- Return type:
np.ndarray
- set_fit_request()¶
No-op.
Calling this method has no effect.
- Returns:
self – The updated object.
- Return type:
object
- Parameters:
self (CQR)
- set_predict_request()¶
No-op.
Calling this method has no effect.
- Returns:
self – The updated object.
- Return type:
object
- Parameters:
self (CQR)
- set_score_request(*, sample_weight='$UNCHANGED$')¶
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.self (CQR)
- Returns:
self – The updated object.
- Return type:
object