簡體   English   中英

從 Python 中的 H2OXGBoostEstimator 模型中提取原生 xgboost 模型

[英]Extract native xgboost model from H2OXGBoostEstimator model in Python

是否可以從 Python 中的 H2OXGBoostEstimator 模型中提取本機 xgboost 模型泡菜文件並通過原始 XGBoost Python API 讀取? 謝謝!

您可以使用以下內容:

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 存儲庫中找到的數據集,並使用此示例創建模型

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()

您可以嘗試這兩種“h2o-to-xgboost”方法從經過訓練的 H2O 模型中提取 XGBoost 超參數和 DMatrix,這(根據文檔)將為您提供完全相同的 XGBoost 原生 python 模型。

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])

更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM