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

我尝试使用教程中的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’版本

enter image description here


回答:

我遇到了同样的问题,这与graphviz的安装无关。在我的情况下,问题出在我的pandas数据框的一些列名中包含了空格。参见github上的讨论。

当我添加了

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

到我的预处理中时,问题就解决了。

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注