close

How to save & load xgboost model?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to save & load xgboost model in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to save & load xgboost model?

  1. How to save & load xgboost model?

    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.

  2. save & load xgboost model

    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.

Method 1

I found my way here because I was looking for a way to save and load my xgboost model. Here is how I solved my problem:

import pickle
file_name = "xgb_reg.pkl"

# save
pickle.dump(xgb_model, open(file_name, "wb"))

# load
xgb_model_loaded = pickle.load(open(file_name, "rb"))

# test
ind = 1
test = X_val[ind]
xgb_model_loaded.predict(test)[0] == xgb_model.predict(test)[0]

Out[1]: True

Method 2

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!

EDIT: From Xgboost documentation (for version 1.3.3), the dump_model() should be used for saving the model for further interpretation. For saving and loading the model the save_model() and load_model() should be used. Please check the docs for more details.

There is also a difference between Learning API and Scikit-Learn API of Xgboost. The latter saves the best_ntree_limit variable which is set during the training with early stopping. You can read details in my article How to save and load Xgboost in Python?

The save_model() method recognize the format of the file name, if *.json is specified, then model is saved in JSON, otherwise it is text file.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read