如何保存&加载 xgboost 模型? [英] How to save & load xgboost model?

查看:50
本文介绍了如何保存&加载 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

我的问题如下.

  1. save_model & 有什么区别?dump_model?
  2. 保存'0001.model''dump.raw.txt','featmap.txt'有什么区别?
  3. 为什么加载model.bin的模型名和要保存的0001.model的模型名不同?
  4. 假设我训练了两个模型:model_Amodel_B.我想保存这两个模型以备将来使用.保存 &我应该使用 load 函数吗?您能帮忙展示一下清晰的流程吗?
  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?

推荐答案

这是我解决问题的方法:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆