我尝试使用教程中的plot_tree函数
我使用的是鸢尾花数据集来训练模型,以下是我的代码:
from numpy import loadtxtfrom xgboost import XGBClassifierfrom xgboost import plot_treeimport matplotlib.pyplot as pltimport shapX,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 treeplot_tree(model)plt.show()
然后,我得到了这个错误:
ValueError: Unable to parse node: 0:[petal
我不知道该从哪里找原因,因为模型在训练和预测时都没有问题。
我使用的是sklearn的’0.20.3’版本
回答:
我遇到了同样的问题,这与graphviz的安装无关。在我的情况下,问题出在我的pandas数据框的一些列名中包含了空格。参见github上的讨论。
当我添加了
df.rename(columns = lambda x: x.replace(' ', '_'), inplace=True)
到我的预处理中时,问题就解决了。