2017-08-28 190 views
2

我是xgboost的新手,我想要將我的xgboost模型可視化。Xgboost plot_tree錯誤:ValueError:助推器必須是助推器實例

這裏是我的代碼,代碼來自一個教程,可能沒有錯誤。

from numpy import loadtxt 
from xgboost import XGBClassifier 
from xgboost import plot_tree 
import matplotlib.pyplot as plt 

dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",") 
X = dataset[:,0:8] 
y = dataset[:,8] 
model = XGBClassifier() 
model.fit(X, y) 
plot_tree(model) 
plt.show() 

我使用Ubuntu和我已經安裝了Graphviz的,運行這段代碼將得到

Traceback (most recent call last): 
File "a.py", line 15, in <module> 
    plot_tree(model) 
    File "/home/statham/anaconda2/lib/python2.7/site-packages/xgboost/plotting.py", line 214, in plot_tree 
    g = to_graphviz(booster, num_trees=num_trees, rankdir=rankdir, **kwargs) 
    File "/home/statham/anaconda2/lib/python2.7/site-packages/xgboost/plotting.py", line 160, in to_graphviz 
    raise ValueError('booster must be Booster instance') 
ValueError: booster must be Booster instance 

我知道,關鍵的一點是我的模型是不是一個助推器例如,我已搜查谷歌和我沒有」噸找到一個asnwer,誰能告訴我如何將我的模型轉換爲Booster實例嗎?提前致謝。

回答

1

我找到了答案。

只要改變

plot_tree(model) 

到:

plot_tree(model._Booster) 

,它會工作。

+0

爲自己感到驕傲。 – Statham