Xgboost plot_tree 错误:值错误:助推器必须是助推器实例



我是xgboost的新手,我想可视化我的xgboost模型。

这是我的代码,代码来自教程,可能没有错误。

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

我知道关键点是我的模型不是 Booster 实例,我已经搜索了 Google 但没有找到 asnwer,谁能告诉我如何将我的模型转换为 Booster 实例?提前谢谢。

我找到了答案。

只需更改

plot_tree(model)

到:

plot_tree(model._Booster)

它会起作用。