在Python中从H2OXGBoostEstimator模型中提取本机xgboost模型

时间:2018-09-27 02:40:09

标签: python machine-learning h2o xgboost

是否可以在Python中从H2OXGBoostEstimator模型中提取本机xgboost模型pickle文件并通过原始XGBoost Python API读取?谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

import matplotlib.pyplot as plt
from xgboost import plot_tree

# model is your xgboost model, choose which tree in num_trees and layout direction default if top to bottom, LR is left to right
plot_tree(model, num_trees=0, rankdir='LR') 
plt.show()

这是一个完全可复制的示例,但请注意,它使用的是xgboost存储库中的dataset,并使用此example创建模型

import numpy as np
import scipy.sparse
import pickle
import xgboost as xgb
import matplotlib.pyplot as plt
from xgboost import plot_tree

### simple example
# load file from text file, also binary buffer generated by xgboost
dtrain = xgb.DMatrix('../data/agaricus.txt.train')
dtest = xgb.DMatrix('../data/agaricus.txt.test')

# specify parameters via map, definition are same as c++ version
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}

# specify validations set to watch performance
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_round = 2
bst = xgb.train(param, dtrain, num_round, watchlist)

plot_tree(bst, num_trees=0, rankdir='LR') # bst is your xgboost model, choose which tree in num_trees and layout direction default if top to bottom, LR is left to right
plt.show()

答案 1 :(得分:0)

您可以尝试这两种“h2o-to-xgboost”方法从经过训练的 H2O 模型中提取 XGBoost 超参数和 DMatrix,这(根据 the docs)将为您提供完全相同的 XGBoost 原生 Python 模型。< /p>

nativeXGBoostParam = h2oModelD.convert_H2OXGBoostParams_2_XGBoostParams()
nativeXGBoostInput = data.convert_H2OFrame_2_DMatrix(myX, y, h2oModelD)
        
nativeModel = xgb.train(dtrain=nativeXGBoostInput,
                        params=nativeXGBoostParam[0],                        
                        num_boost_round=nativeXGBoostParam[1])

更多信息: