win64 python pydot安装配置

本文主要是win10,下面anaconda安装pydot以及 .dot文件转化为png。


1. 安装pydot

pip install pydot

win64 python pydot安装配置_第1张图片


2. 安装GraphViz

链接如下:
http://www.graphviz.org/Download_windows.php

win64 python pydot安装配置_第2张图片

下载完成点击安装,选择好安装目录之后即可。最终将安装路径添加到,环境变量中。

win64 python pydot安装配置_第3张图片

win64 python pydot安装配置_第4张图片

3. 重启pycharm

win64 python pydot安装配置_第5张图片

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

win64 python pydot安装配置_第6张图片


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)

这个可视化,添加了属性,看起来更直观

win64 python pydot安装配置_第7张图片

转载注明出处,并在下面留言!!!

你可能感兴趣的:(Python)