如何指定决策树的graphviz表示的大小? [英] How can I specify the figsize of a graphviz representation of a decision tree?

查看:532
本文介绍了如何指定决策树的graphviz表示的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在虹膜数据集上训练的决策树的GraphViz表示形式.

I have a GraphViz representation of a decision tree trained on the iris dataset.

import graphviz 

dot_data = tree.export_graphviz(clf, out_file=None, 
                     feature_names=iris.feature_names,  
                     class_names=iris.target_names,  
                     filled=True, rounded=True,  
                     special_characters=True)

graph = graphviz.Source(dot_data)
graph

我正在使用上面的代码生成GraphViz图,但是它创建了一个大图.

I am using the above code to generate the GraphViz figure, but it creates a large graph.

我想手动控制此图的figzise.我该怎么办?

I want to manually control the figzise of this graph. How can I do this?

推荐答案

在最初编写自己的函数以修改DOT源代码字符串以添加size属性后,我在

After initially writing my own function to modify the DOT source code string to add in a size attribute, I stumbled upon this section in the pydotplus.graphviz.Graph documentation:

在Graphviz点语言中定义的所有属性都应 被支持.

All the attributes defined in the Graphviz dot language should be supported.

可以通过动态生成的方法设置属性:

Attributes can be set through the dynamically generated methods:

 set_[attribute name], i.e. set_size, set_fontname

您可以在下面看到使用此示例.请注意调用函数时的语法,因为DOT源代码需要在宽度和高度两边加上双引号.感叹号表示它将强制调整图像的大小,直到其中一个尺寸与指定的尺寸之一匹配为止,这仅在指定的尺寸大于图形的原始尺寸时才有意义.

You can see an example of using this below. Note the syntax when calling the function, as the DOT source code requires double quotes around the width and height. The exclamation mark means it will force the image to resize until one of the dimensions matches one of the specified dimensions, which only seems to matter if the dimensions specified are larger than the original size of the graph.

import pydotplus
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier, export_graphviz

# Load in the dataset from sklearn
dataset = load_breast_cancer()
X = dataset.data
y = dataset.target
col_names = dataset.feature_names

# Create and fit the decision tree
clf_dt = DecisionTreeClassifier(criterion = 'gini', max_depth = 3)
clf_dt.fit(X_train, y_train)

# Export resulting tree to DOT source code string
dot_data = export_graphviz(clf_dt,
                                feature_names=col_names,
                                out_file=None,
                                filled=True,
                                rounded=True)

pydot_graph = pydotplus.graph_from_dot_data(dot_data)
pydot_graph.write_png('original_tree.png')
pydot_graph.set_size('"5,5!"')
pydot_graph.write_png('resized_tree.png')

单击图片以了解尺寸,因为它似乎无法在浏览器中正确显示.

Click on the pictures to get a sense for size since it doesn't seem to show up properly in the browser.

original_tree.png:

resized_tree.png :

还请注意,pydotplus.graphviz.Graph对象具有to_string()方法,该方法返回树的DOT源代码字符串,也可以与问题中的graphviz.Source对象一起使用:

Note also that pydotplus.graphviz.Graph objects have a to_string() method which returns the DOT source code string of the tree, which can also be used with the graphviz.Source object in your question:

import graphviz
gvz_graph = graphviz.Source(pydot_graph.to_string())
gvz_graph

这篇关于如何指定决策树的graphviz表示的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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