sklearn 决策树 plot_tree 节点重叠

时间:2021-03-20 14:34:49

标签: python matplotlib scikit-learn decision-tree

当我使用 sklearn.tree.plot_tree() 绘制我的 sklearn 决策树时,节点在更深层次上重叠,我无法读取节点中的内容。展示你的结果并不好。它看起来像这样:

overlapping nodes, want more space between them

我怎样才能让它们之间有更多的空间,以避免重叠?

我已经找到了 this 问题,并且我已经将 fontsize 增加到 10 并将 figsize 增加到 (20,30),但它导致绘制的树如图所示。我还将 dpi 增加到 150。这是我的代码:

fig = plt.figure(figsize=(20,30))
artists = sklearn.tree.plot_tree(decision_tree=clf, feature_names=feature_names, class_names=class_names, filled=True, rounded=True, fontsize=10, max_depth=4)
fig.savefig(filepath, dpi=150)

1 个答案:

答案 0 :(得分:-1)

您需要控制渲染图像的大小。 尝试像 fig = plt.figure(figsize=(50,30)) 一样改变 figsize。这对我有用。
您可以在 https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html 中找到原始文档。