ValueError: gbrt has to be an instance of BaseGradientBoosting

So I am trying to make partial dependence plot using xgboost in spyder. But it is giving ValueError: gbrt has to be an instance of BaseGradientBoosting. I have predefine values of train_X, train_y, val_X, val_y. Here is the code:

from xgboost import XGBRegressor

model=XGBRegressor(n_estimator=1000, learning_rate=0.05)
model.fit(train_X, train_y, early_stopping_rounds=5, eval_set=[(val_X, val_y)], verbose=False)

pred_xgb=model.predict(val_X)

print(mean_absolute_error(pred_xgb, val_y),'is the mae \n')

from sklearn.ensemble.partial_dependence import plot_partial_dependence
from sklearn.ensemble.partial_dependence import partial_dependence

plot=plot_partial_dependence(model,train_X, features=[1,3], feature_names=['mssubclass','mszoning','salestype','salescondition'], grid_resolution=20)

Thank you.

Upvotes: 0

Views: 1109

Answers (1)

Bar
Bar

Reputation: 2826

This is caused by an incompatibility between sklearn and xgboost.

plot_partial_dependence expects a model that inherits from BaseGradientBoosting that is a sklearn-specific class that XGBoostRegressor does not inherit from AFAIK.

That means that if you want to use that you would need to convert between the XGBoost model and an sklearn GBRT model. It might be possible to do that through treelite.

Upvotes: 1

Related Questions