Save LGBMRegressor model from python lightgbm package to disc

Try:

my_model.booster_.save_model('mode.txt')
#load from model:

bst = lgb.Booster(model_file='mode.txt')

Note: the API state that

bst = lgb.train(…)
bst.save_model('model.txt', num_iteration=bst.best_iteration)

Depending on the version, one of the above works. For generic, You can also use pickle or something similar to freeze your model.

import joblib
# save model
joblib.dump(my_model, 'lgb.pkl')
# load model
gbm_pickle = joblib.load('lgb.pkl')

Let me know if that helps


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'