scikit-learn-contrib / MAPIE

A scikit-learn-compatible module to estimate prediction intervals and control risks based on conformal predictions.
https://mapie.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
1.27k stars 102 forks source link

Gradient Boosting Regressor Model #411

Closed Karoloso closed 7 months ago

Karoloso commented 8 months ago

Attempts to obtain confidence intervals for the GradientBoostingRegressor model. Below is the program code:

from sklearn.model_selection import train_test_split import numpy as np from sklearn.linear_model import LinearRegression from sklearn.datasets import make_regression from sklearn.ensemble import GradientBoostingRegressor from matplotlib import pyplot as plt from mapie.regression import MapieQuantileRegressor

Model = GradientBoostingRegressor( n_estimators = 500, max_depth = 4, min_samples_split = 5, learning_rate = 0.01, loss = "squared_error", )

X, y = make_regression(n_samples=5000, n_features=1, noise=20, random_state=59) X_train, X_tmp, y_train, y_tmp = train_test_split(X, y, test_size=2000, random_state=42) X_calib, X_test, y_calib, y_test = train_test_split(X_tmp, y_tmp, test_size=1000, random_state=42)

alpha = 0.1 mapie = MapieQuantileRegressor(estimator=Model, cv="split", alpha=alpha) mapie.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib) y_pred, y_pis = mapie.predict(X_test)

When I try to compile the code I get this error:

ValueError Traceback (most recent call last) in <cell line: 24>() 22 alpha = 0.1 23 mapie = MapieQuantileRegressor(estimator=Model, cv="split", alpha=alpha) ---> 24 mapie.fit(X_train, y_train, X_calib=X_calib, y_calib=y_calib) 25 y_pred, y_pis = mapie.predict(X_test)

1 frames /usr/local/lib/python3.10/dist-packages/mapie/regression/quantile_regression.py in _check_estimator(self, estimator) 285 if loss_name in param_estimator: 286 if param_estimator[loss_name] != "quantile": --> 287 raise ValueError( 288 "You need to set the loss/objective argument" 289 + " of your base model to quantile."

ValueError: You need to set the loss/objective argument of your base model to quantile.

LacombeLouis commented 7 months ago

Hey @Karoloso, Thank you for your issue. As suggested in the error raised, when using CQR which is a quantile based method, you need to set your regressor to use a quantile loss. In this context, it means that you need to set

Model = GradientBoostingRegressor(n_estimators = 500, max_depth = 4, min_samples_split = 5, learning_rate = 0.01, loss = "quantile")

Note that if you wish to fit other models that are not preestablished in MAPIE, I refer you back to these issues: #409 #199. I hope this solves your problem!

Karoloso commented 7 months ago

Thanks for help.