将 xgboost.Booster 类转换为 XGBRegressor 或从 xgboost.Booster 加载 XGBRegressor

2024-03-29

我从 Sagemaker 获得了一个模型,其类型为:

<class 'xgboost.core.Booster'>

我可以在本地对此进行评分,这很棒,但一些谷歌搜索表明,可能无法做类似这样的“标准”事情here https://mljar.com/blog/feature-importance-xgboost/:

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

是否可以将 xgboost.core.Booster 转换为 XGBRegressor?也许可以使用 save_raw 方法查看this https://xgboost.readthedocs.io/en/latest/python/python_api.html#xgboost.Booster?谢谢!

到目前为止我尝试过:

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

但这会导致:

NotFittedError: need to call fit or load_model beforehand

沿着这些思路的东西似乎工作得很好:

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.tar.gz 是来自 sagemaker 的工件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 xgboost.Booster 类转换为 XGBRegressor 或从 xgboost.Booster 加载 XGBRegressor 的相关文章

随机推荐