Xgboost plottree error: Unable to parse node: 0:[petal

Xgboost plottree error: Unable to parse node: 0:[petal

我正在尝试使用 plot_tree 中的 tutorial

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

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”版本

确保您安装了 graphviz。因为 XGboost 的 plot_tree 内部使用 graphviz 进行绘图。

我遇到了同样的问题,但它与 graphviz 安装无关。在我的例子中,问题是 pandas dataFrame 的一些列名有白色 space。 另见关于 github.

的讨论

当我添加

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

我的预处理解决了这个问题。