Search code examples
pythonxgboostamazon-sagemakerxgbregressor

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


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

Solution

  • 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.