sklearn.tree.plot_tree 显示 returns 文本块而不是可视化树

sklearn.tree.plot_tree show returns chunk of text instead of visualised tree

我正在尝试使用 plot_tree 显示树可视化,但它显示的是一大段文本:

from sklearn.tree import plot_tree
plot_tree(t)

(其中 t 是 DecisionTreeClassifier 的实例) 这是输出:

[Text(464.99999999999994, 831.6, 'X[3] <= 0.8\nentropy = 1.581\nsamples = 120\nvalue = [39, 37, 44]'),
 Text(393.46153846153845, 646.8, 'entropy = 0.0\nsamples = 39\nvalue = [39, 0, 0]'),

等等等等。我如何让它显示可视树? 我正在使用 Jupyter 6.4.1,并且我已经在代码的前面导入了 matplotlib。谢谢!

在我的例子中,它使用简单的“显示”:

plot_tree(t)
plt.show()

您可以绘制树并使用 plt.figure

指定树的绘图大小
width = 10
height = 7
plt.figure(figsize=(width, height))

tree_plot_max_depth = 6
plot_tree(t, max_depth=tree_plot_max_depth)

## the key to the problem of not showing tree is the command below
plt.show()

正如下面提到的文档,您可以为树指定更多参数以获得更多信息的图像。

https://scikit-learn.org/stable/modules/generated/sklearn.tree.plot_tree.html