How to save & load xgboost model?

Pengju Zhao picture Pengju Zhao · Apr 29, 2017 · Viewed 63.8k times · Source

On the link of XGBoost guide:

After training, the model can be saved.

bst.save_model('0001.model')

The model and its feature map can also be dumped to a text file.

# dump model
bst.dump_model('dump.raw.txt')
# dump model with feature map
bst.dump_model('dump.raw.txt', 'featmap.txt')

A saved model can be loaded as follows:

bst = xgb.Booster({'nthread': 4})  # init model
bst.load_model('model.bin')  # load data

My questions are following.

  1. What's the difference between save_model & dump_model?
  2. What's the difference between saving '0001.model' and 'dump.raw.txt','featmap.txt'?
  3. Why the model name for loading model.bin is different from the name to be saved 0001.model?
  4. Suppose that I trained two models: model_A and model_B. I wanted to save both models for future use. Which save & load function should I use? Could you help show the clear process?

Answer

pplonski picture pplonski · May 22, 2017

Both functions save_model and dump_model save the model, the difference is that in dump_model you can save feature name and save tree in text format.

The load_model will work with model from save_model. The model from dump_model can be used for example with xgbfi.

During loading the model, you need to specify the path where your models is saved. In the example bst.load_model("model.bin") model is loaded from file model.bin - it is just a name of file with model. Good luck!