Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save LGBMRegressor model from python lightgbm package to disc

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

like image 678
Utpal Datta Avatar asked Mar 17 '19 15:03

Utpal Datta


People also ask

What is LightGBM booster?

LightGBM is a gradient boosting framework that uses tree based learning algorithms. It is designed to be distributed and efficient with the following advantages: Faster training speed and higher efficiency. Lower memory usage.

How do I use LGBM in Python?

Coding an LGBM in Python The LGBM model can be installed by using the Python pip function and the command is “pip install lightbgm” LGBM also has a custom API support in it and using it we can implement both Classifier and regression algorithms where both the models operate in a similar fashion.

How do I use LightGBM to classify?

LightGBM will by default consider model as a regression model. boosting : defines the type of algorithm you want to run, default=gdbt. learning_rate : This determines the impact of each tree on the final outcome. GBM works by starting with an initial estimate which is updated using the output of each tree.


2 Answers

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

like image 145
Prayson W. Daniel Avatar answered Sep 20 '22 06:09

Prayson W. Daniel


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'

like image 34
JFRANCK Avatar answered Sep 19 '22 06:09

JFRANCK