29

Hi I am unable to find a way to save a lightgbm.LGBMRegressor model to a file for later re-use.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Utpal Datta
  • 367
  • 1
  • 3
  • 8

4 Answers4

36

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

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
  • 1
    Could you please let me know if there is any way to get the feature list in the model after loading using lgb.Booster (the bst model in your example) – Utpal Datta Mar 23 '19 at 00:35
  • I have not worked much with lgb. Can you write another question here in SO? I believe that is a good question that I am interested to know too. – Prayson W. Daniel Mar 23 '19 at 06:01
  • @UtpalDatta I would recommend you to use pickle. That way you would be loading the model just as you saved it (class `LGBMRegressor`, not Booster) and you can use the [feature_name_](https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMClassifier.html#lightgbm.LGBMClassifier.feature_name_) attribute. – Farid Jul 15 '20 at 09:25
  • 1
    How can one look into the hyperparameters of a saved model after loading? – Slouei Mar 29 '21 at 21:46
  • Just wondering what is the best approach. In the first example, you work with two different objects (the first one is of `LGBMRegressor` type but the second of type `Booster`) which may introduce some incosistency (like you cannot find something in Booster e.g. the comment from @UtpalDatta). The second one seems more consistent, but pickle or joblib does not seem as a good practice to me (it may behave differently between versions and so on). So which one is the preferred way to do it? – Nerxis Apr 06 '21 at 08:15
  • Good practice goes in model versioning. So if you tried a model on a certain environment, it is best to pin the Python version, pickle or joblib version and all necessary packages to restore the frozen version. In protected environment pickle, dill or joblib are awesome tools. I hope this helps. – Prayson W. Daniel Apr 06 '21 at 17:17
21

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'

JFRANCK
  • 314
  • 3
  • 5
13

With the lastest version of lightGBM using import lightgbm as lgb, here is how to do it:

model.save_model('lgb_classifier.txt', num_iteration=model.best_iteration) 

and then you can read the model as follow :

model = lgb.Booster(model_file='lgb_classifier.txt')
smerllo
  • 3,117
  • 1
  • 22
  • 37
  • 5
    Could you make clear how did you create your `model` and which version of `lightgbm` you used? The question mentions `LGBMRegressor` but as other pointed out there is not `save_model` method AFAIK. – Nerxis Apr 06 '21 at 07:34
  • 1
    Good, upvoted! But note that specifying `num_iteration=model.best_iteration` is redundant, as it is just re-terating the default (`save_model()` would save the best iteration by default), see: https://github.com/Microsoft/LightGBM/issues/1683#issuecomment-425038466) – mirekphd Mar 09 '23 at 11:10
0
clf.save_model('lgbm_model.mdl')
clf = lgb.Booster(model_file='lgbm_model.mdl')
amalik2205
  • 3,962
  • 1
  • 15
  • 21