Xgboost plottree错误:无法解析节点:0:[花瓣

时间:2019-08-09 02:47:08

标签: python machine-learning scikit-learn xgboost

我正在尝试使用tutorial

中的plot_tree

我正在使用虹膜数据集来训练模型,这是我拥有的代码:

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



X,y = shap.datasets.iris()
y = pd.Series([v if v==1 else 0 for v in y]) 

model = XGBClassifier()
model.fit(X, y)
# plot single tree
plot_tree(model)
plt.show()

从那里,我收到此错误:

ValueError: Unable to parse node: 0:[petal

我不知道该在哪里寻找,因为该模型可以毫无问题地进行训练和预测。

我正在使用sklearn的'0.20.3'版本

enter image description here

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,它与graphviz的安装无关。在我的情况下,问题是我的pandas dataFrame的某些列名称带有空格。 另请参见有关github的讨论。

添加时

df.rename(columns = lambda x: x.replace(' ', '_'), inplace=True)

在我的预处理下,它解决了问题。

答案 1 :(得分:0)

确保已安装graphviz。因为XGboost的plot_tree内部使用graphviz进行绘制。