Answer a question

Hi I am unable to find an way to save and reuse an LGBM model to a file. I used python package lightgbm and LGBMRegressor model. Could you please help? Documentations doesn't seem to have useful information. I am using python 3.5 on Spyder

Answers

For Python 3.7 and lightgbm==2.3.1, I found that the previous answers were insufficient to correctly save and load a model. The following worked:

lgbr = lightgbm.LGBMRegressor(num_estimators = 200, max_depth=5)
lgbr.fit(train[num_columns], train["prep_time_seconds"])
preds = lgbr.predict(predict[num_columns])
lgbr.booster_.save_model('lgbr_base.txt')

Finally, we can validated that this worked via:

model = lightgbm.Booster(model_file='lgbr_base.txt')
model.predict(predict[num_columns])

Without the above, I was getting the error: AttributeError: 'LGBMRegressor' object has no attribute 'save_model'

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐