Xgboost plot_tree Error: ValueError: booster must be Booster instance

Xgboost plot_tree Error: ValueError: booster must be Booster instance

我是 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 但没有找到答案,谁能告诉我如何将我的模型转换为 Booster 实例?提前致谢。

我找到了答案。

改变一下

plot_tree(model)

进入:

plot_tree(model._Booster)

它会起作用。