cast xgboost.Booster class to XGBRegressor or load XGBRegressor from xgboost.Booster

895 Views Asked by At

I get a model from Sagemaker of type:

<class 'xgboost.core.Booster'>

I can score this locally which is great but some google searches have shown that it may not be possible to do "standard" things like this taken from here:

plt.barh(boston.feature_names, xgb.feature_importances_)

Is it possible to tranform xgboost.core.Booster to XGBRegressor? Maybe one could use the save_raw method looking at this? Thanks!

So far I tried:

xgb_reg = xgb.XGBRegressor() 
xgb_reg._Boster = model
xgb_reg.feature_importances_

but this reults in:

NotFittedError: need to call fit or load_model beforehand
2

There are 2 best solutions below

0
On BEST ANSWER

Something along those lines appears to work fine:

local_model_path = "model.tar.gz"
with tarfile.open(local_model_path) as tar:
    tar.extractall()

model = xgb.XGBRegressor() 
model.load_model(model_file_name)     

model can then be used as usual - model.tar.gz is an artifcat coming from sagemaker.

0
On

For me, the following worked, using your tip of using save_raw:

import xgboost as xgb
import numpy as np

n = 1000

params = {"objective": "reg:squarederror", "tree_method": "hist"}

# some example independent parameters and a
# dependent parameter
x_train = np.zeros((4,2))
x_train[:, 0] = np.arange(4)
x_train[:, 1] = np.arange(4)
y_train = np.array((2,4,6,8))

x_test = np.array((1.5,2.5,3.5)).reshape(3,1)
y_test = np.array((3.,5.,7)).reshape(3,1)

dtrain_reg = xgb.DMatrix(x_train, y_train)
dtest_reg = xgb.DMatrix(x_test, y_test)

evals = [(dtrain_reg, "train"), (dtest_reg, "validation")]

xgboost_core_booster_model = xgb.train(
        params=params,
        dtrain=dtrain_reg,
        num_boost_round=n,
        evals=evals,
        verbose_eval=0,
        early_stopping_rounds=50)

model_saved_raw = xgboost_core_booster_model.save_raw()
xgb_reg = xgb.XGBRegressor()
# load what was originally a native 
# XGBoost Booster model as now a 
# Scikit-Learn model (specifically 
# XGBRegressor)
xgb_reg.load_model(model_saved_raw)