Python 如何保存和加载 xgboost 模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43691380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save & load xgboost model?
提问by Pengju Zhao
On the link of XGBoost guide:
在XGBoost 指南的链接上:
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
训练完成后,可以保存模型。
bst.save_model('0001.model')
模型及其特征图也可以转储到文本文件中。
# dump model bst.dump_model('dump.raw.txt') # dump model with feature map bst.dump_model('dump.raw.txt', 'featmap.txt')
可以按如下方式加载保存的模型:
bst = xgb.Booster({'nthread': 4}) # init model bst.load_model('model.bin') # load data
My questions are following.
我的问题如下。
- What's the difference between
save_model
&dump_model
? - What's the difference between saving
'0001.model'
and'dump.raw.txt','featmap.txt'
? - Why the model name for loading
model.bin
is different from the name to be saved0001.model
? - Suppose that I trained two models:
model_A
andmodel_B
. I wanted to save both models for future use. Whichsave
&load
function should I use? Could you help show the clear process?
save_model
& 和有dump_model
什么区别?- save
'0001.model'
和 和有'dump.raw.txt','featmap.txt'
什么不一样? - 为什么加载的模型名称
model.bin
与要保存的名称不同0001.model
? - 假设我训练了两个模型:
model_A
和model_B
。我想保存这两个模型以备将来使用。我应该使用哪个save
&load
功能?你能帮忙展示清楚的过程吗?
回答by pplonski
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.
都有保存模型save_model
和dump_model
保存模型的功能,区别在于dump_model
可以保存特征名称和保存树的文本格式。
The load_model
will work with model from save_model
. The model from dump_model
can be used for example with xgbfi.
该load_model
会从模型工作save_model
。dump_model
例如,模型 from可以与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!
在加载模型期间,您需要指定模型的保存路径。在示例中,bst.load_model("model.bin")
模型是从文件加载的model.bin
- 它只是带有模型的文件名。祝你好运!
回答by ChrisDanger
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:
我在这里找到了方法,因为我正在寻找一种方法来保存和加载我的 xgboost 模型。这是我解决问题的方法:
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
回答by Ioannis Nasios
An easy way of saving and loading a xgboost model is with joblib library.
保存和加载 xgboost 模型的一种简单方法是使用 joblib 库。
import joblib
#save model
joblib.dump(xgb, filename)
#load saved model
xgb = joblib.load(filename)