Python 是否可以在 scikit-learn 中打印决策树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25274673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to print the decision tree in scikit-learn?
提问by Hyman Twain
Is there a way to print a trained decision tree in scikit-learn? I want to train a decision tree for my thesis and I want to put the picture of the tree in the thesis. Is that possible?
有没有办法在 scikit-learn 中打印训练有素的决策树?我想为我的论文训练一个决策树,我想把树的图片放在论文中。那可能吗?
采纳答案by EdChum
There is a method to export to graph_viz format: http://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html
有一种导出为graph_viz格式的方法:http://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html
So from the online docs:
所以从在线文档:
>>> from sklearn.datasets import load_iris
>>> from sklearn import tree
>>>
>>> clf = tree.DecisionTreeClassifier()
>>> iris = load_iris()
>>>
>>> clf = clf.fit(iris.data, iris.target)
>>> tree.export_graphviz(clf,
... out_file='tree.dot')
Then you can load this using graph viz, or if you have pydot installed then you can do this more directly: http://scikit-learn.org/stable/modules/tree.html
然后您可以使用图形即加载它,或者如果您安装了 pydot,那么您可以更直接地执行此操作:http://scikit-learn.org/stable/modules/tree.html
>>> from sklearn.externals.six import StringIO
>>> import pydot
>>> dot_data = StringIO()
>>> tree.export_graphviz(clf, out_file=dot_data)
>>> graph = pydot.graph_from_dot_data(dot_data.getvalue())
>>> graph.write_pdf("iris.pdf")
Will produce an svg, can't display it here so you'll have to follow the link: http://scikit-learn.org/stable/_images/iris.svg
将生成一个 svg,无法在此处显示,因此您必须点击链接:http: //scikit-learn.org/stable/_images/iris.svg
Update
更新
It seems that there has been a change in the behaviour since I first answered this question and it now returns a listand hence you get this error:
自从我第一次回答这个问题以来,行为似乎发生了变化,现在它返回 a list,因此您会收到此错误:
AttributeError: 'list' object has no attribute 'write_pdf'
Firstly when you see this it's worth just printing the object and inspecting the object, and most likely what you want is the first object:
首先,当您看到此内容时,只需打印对象并检查对象即可,很可能您想要的是第一个对象:
graph[0].write_pdf("iris.pdf")
Thanks to @NickBraunagel for the comment
感谢@NickBraunagel 的评论
回答by NickBraunagel
Although I'm late to the game, the below comprehensive instructions could be useful for others who want to display decision tree output:
虽然我迟到了,但以下综合说明对于想要显示决策树输出的其他人可能有用:
Install necessary modules:
安装必要的模块:
- install
graphviz. I used conda's install package here(recommended overpip install graphvizaspipinstall doesn't include the actual GraphViz executables) - install
pydotvia pip (pip install pydot) - Add the graphviz folder directory containing the .exe files (e.g. dot.exe) to your environment variable PATH
- run EdChum's above (NOTE:
graphis alistcontaining thepydot.Dotobject):
- 安装
graphviz。我在这里使用了 conda 的安装包 (推荐使用,pip install graphviz因为pip安装不包括实际的 GraphViz可执行文件) pydot通过 pip (pip install pydot)安装- 将包含 .exe 文件(例如 dot.exe)的 graphviz 文件夹目录添加到您的环境变量 PATH
- 运行上面的 EdChum(注意:
graph是一个list包含pydot.Dot对象):
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf") # must access graph's first element
Now you'll find the "iris.pdf" within your environment's default directory
现在您将在环境的默认目录中找到“iris.pdf”

