如何保存&加载 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
.我想保存这两个模型以备将来使用.保存
&我应该使用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?
推荐答案
这是我解决问题的方法:
Here is how I solved the 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屋!
查看全文