Sklearn plot_tree图太小 [英] Sklearn plot_tree plot is too small

查看:364
本文介绍了Sklearn plot_tree图太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)

tree.plot_tree(clf.fit(X, y))
plt.show()

我得到的结果是这张图:

And the result I get is this graph:

如何使该图清晰可见?我正在使用PyCharm Professional 2019.3作为我的IDE.

How do I make this graph legible? I'm using PyCharm Professional 2019.3 as my IDE.

推荐答案

我认为您要查找的设置是 fontsize .您必须将其与 max_depth figsize 平衡,以获取可读的图.这是一个例子

I think the setting you are looking for is fontsize. You have to balance it with max_depth and figsize to get a readable plot. Here is an example

from sklearn import tree
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

# load data
X, y = load_iris(return_X_y=True)

# create and train model
clf = tree.DecisionTreeClassifier(max_depth=4)  # set hyperparameter
clf.fit(X, y)

# plot tree
plt.figure(figsize=(12,12))  # set plot size (denoted in inches)
tree.plot_tree(clf, fontsize=10)
plt.show()

如果要捕获整个树的结构,我想以小字体和高dpi保存图是解决方案.然后,您可以打开图片并放大到特定的节点以进行检查.

If you want to capture structure of the whole tree I guess saving the plot with small font and high dpi is the solution. Then you can open a picture and zoom to the specific nodes to inspect them.

# create and train model
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)

# save plot
plt.figure(figsize=(12,12))
tree.plot_tree(clf, fontsize=6)
plt.savefig('tree_high_dpi', dpi=100)

这是在大树上的样子的一个例子.

Here is an example of how it looks like on the bigger tree.

这篇关于Sklearn plot_tree图太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆