【问题标题】:Xgboost plot_tree Error: ValueError: booster must be Booster instanceXgboost plot_tree Error: ValueError: booster must be Booster instance
【发布时间】:2018-02-05 18:47:24
【问题描述】:

我是 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,运行这段代码会得到 ​​p>

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 实例?提前致谢。

【问题讨论】:

    标签: python matplotlib graphviz xgboost


    【解决方案1】:

    我找到答案了。

    只是改变

    plot_tree(model)
    

    进入:

    plot_tree(model._Booster)
    

    它会起作用的。

    【讨论】:

    • 为自己感到骄傲。
    猜你喜欢
    • 2021-06-04
    • 2020-09-17
    • 2017-11-16
    • 2017-01-27
    • 1970-01-01
    • 2022-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多