如何保存 &加载 xgboost 模型? [英] How to save & load xgboost model?
本文介绍了如何保存 &加载 xgboost 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 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
我的问题如下.
save_model
和有什么区别?dump_model
?- 保存
'0001.model'
和'dump.raw.txt','featmap.txt'
有什么区别? - 为什么加载
model.bin
的模型名称与要保存的名称0001.model
不同? - 假设我训练了两个模型:
model_A
和model_B
.我想保存这两个模型以备将来使用.其中save
&我应该使用load
函数吗?你能帮忙展示清楚的流程吗?
- 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?
推荐答案
我在这里找到了方法,因为我正在寻找一种方法来保存和加载我的 xgboost 模型.这是我解决问题的方法:
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
这篇关于如何保存 &加载 xgboost 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文