graphviz,dot,及dot图可视化

graphviz简介

graphviz简介

注意

不同于一般的Python包,graphviz需要额外下载可执行文件,并配置环境变量,如果想在python代码中使用,需要参考下面的步骤:
Install GraphViz if you haven't already (I used the MSI download)
Get the path for gvedit.exe (for me it was "C:\Program Files (x86)\Graphviz2.34\bin")
Add this path to the computer's PATH
One way to get to environment settings to set your path is to click on each of these button/menu options: start->computer->system properties->advanced settings->environment variables
Click Edit User path
Add this string to the end of your Variable value list (including semicolon): ;C:\Program Files (x86)\Graphviz2.34\bin
Click OK
Restart your Python IDE
stackoverflow原文地址
否则运行的时候回报出下面的错误:

'GraphViz\'s executables not found'

使用

场景介绍:

最近在学决策树,在学习使用scikitlearn的决策树方法时候,会产生一个dot格式的文件来表示最终的结果。我想把这个dot树可视化。
pydot可以实现这个功能,不过最后发现pydot还是用不了,好在pydotplus可以解决这个问题。

代码:
from sklearn.datasets import load_iris
from sklearn.externals.six import StringIO
from sklearn import tree
import os
import pydotplus
from IPython.display import Image
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
dot_data = StringIO()
tree.export_graphviz(clf)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
                         feature_names=iris.feature_names,
                         class_names=iris.target_names,
                         filled=True, rounded=True,
                         special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf('iris.pdf')
效果:

最终会形成一个pdf文件,内容是这样的:

graphviz,dot,及dot图可视化_第1张图片
iris决策树

应用于西瓜数据集的尝试

放在scikit-learn decision tree 部分中

你可能感兴趣的:(graphviz,dot,及dot图可视化)