本文主要是win10,下面anaconda安装pydot以及 .dot文件转化为png。
pip install pydot
链接如下:
http://www.graphviz.org/Download_windows.php
下载完成点击安装,选择好安装目录之后即可。最终将安装路径添加到,环境变量中。
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier, export_graphviz
import subprocess
import pydot
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf, out_file='tree.dot')
(graph,) = pydot.graph_from_dot_file('tree.dot')
graph.write_png('somefile.png')
生成下面的tree
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier, export_graphviz
import subprocess
import pydot
def visualize_tree(tree):
"""Create tree png using graphviz.
Args
----
tree -- scikit-learn DecsisionTree.
feature_names -- list of feature names.
"""
with open("tree.dot", 'w') as f:
export_graphviz(tree, out_file=f,
feature_names=iris.feature_names)
command = ["dot", "-Tpng", "tree.dot", "-o", "dt.png"]
try:
subprocess.check_call(command)
except:
exit("Could not run dot, ie graphviz, to "
"produce visualization")
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf, out_file='tree.dot')
visualize_tree(clf)
这个可视化,添加了属性,看起来更直观
转载注明出处,并在下面留言!!!