Python决策树GraphViz [英] Python Decision Tree GraphViz

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

问题描述

我正在尝试使用scikit learning实现决策树,然后使用Graphviz可视化决策树,我理解这是可视化DT的标准选择.我正在使用PyCharm,anaconda,Python 2.7和OS X El Capitan.据我所知,我已经使用PIP安装程序安装了pydot和Graphviz,并且也直接将它们安装在Pycharm中,但是无论如何我都会不断得到没有名为graphviz的模块".

I'm attempting to implement a Decision Tree with scikit learn and then visualise the tree with Graphviz which I understand is the standard choice for visualising DT. I'm using PyCharm, anaconda, Python 2.7 and OS X El Capitan. I've installed pydot and Graphviz with PIP install as far as I can tell and have also installed them directly in Pycharm but whatever I do I continuously get a 'No module named graphviz'.

from sklearn.datasets import load_iris
from sklearn import tree
#import graphviz as gv
# uncommenting the row above produces an error
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
with open('graph.dot', 'w') as file:
    tree.export_graphviz(clf, out_file = file)
file.close()

目前,运行此代码会生成graph.dot,但我无法查看该文件. 1.如何使graphviz存储库正常工作? 2.如何将图形写成PDF/PNG?我看到了一些例子,但是没有用 3.我找到了以下命令:dot -Tps filename.dot -o outfile.ps 我在哪里使用?以及如何验证OS X上是否存在点实用程序?

At the moment running this code produces the graph.dot but I cannot view the file. 1. How do I get the graphviz repository to work? 2. How do I write the graph to PDF/PNG? I saw some examples but non-worked 3. I found this command: dot -Tps filename.dot -o outfile.ps Where do I used it? And how can I verify a dot utility exists on my OS X?

提前谢谢!

推荐答案

您还可以使用以下代码将其导出为pdf.

You can also use following code for exporting to pdf.

首先安装pydot2

First install pydot2

pip install pydot2

然后您可以使用以下代码:

Then you can use following code:

from sklearn.datasets import load_iris
from sklearn import tree
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)

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("graph.pdf") 

这篇关于Python决策树GraphViz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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