我正在做练习,在 jupyter 笔记本中使用 graphviz 创建决策树。然而,决策树过于宽泛。这是代码:
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file="tree.dot", class_names=["malignant", "benign"],
feature_names=cancer.feature_names, impurity=False, filled=True)
with open("tree.dot") as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))
and I get this:
我必须滚动才能看到决策树的左侧。我可以把宽度变小吗?如何?
如果节点树分布广泛,可以尝试
- 为长标签添加换行符(
node1 [label="line\nbreak"]
)
- 全局减少节点宽度和边距(
node [width=0.1 margin=0]
)
- 减少图行中节点之间的距离(graph [nodesep=0.1] https://graphviz.org/docs/attrs/nodesep/)
- 减小图形大小(graph [size="3,3"] https://graphviz.org/docs/attrs/size/)
Or you can put all the nodes in a column with rankdir=LR; edge[constraint=false]
, as in example below.
Image:
Script:
digraph {
graph [rankdir=LR ranksep=1]
node[shape=box width=3]
edge[constraint=false]
A -> {B C}
B -> {D E}
C -> F
D -> {G H}
E -> I
F -> {J T}
G -> {K L}
H -> {M N}
J -> {O P}
}
相关问题:是否可以生成一个小的 GraphViz 图表? https://stackoverflow.com/questions/68321537/is-it-possible-to-generate-a-small-graphviz-chart
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)