python中GraphViz's executables not found的解决方法以及决策树可视化

用python做决策树可视化时,安装了Graphviz,并将其添加到path的环境变量,并且已经安装了pydotplus且import之后,仍然会出现GraphViz’s executables not found问题,可用以下代码解决:

import os     
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'

其中C:/Program Files (x86)/Graphviz2.38/bin/是安装Graphviz的bin所在的路径。




也可以直接在python上安装graphviz
一个例子如下(来自http://scikit-learn.org/stable/modules/tree.html)

from sklearn.datasets import load_iris
from sklearn import tree
import graphviz

iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
dot_data = tree.export_graphviz(clf, out_file=None) 
graph = graphviz.Source(dot_data) 
graph.render("iris") 

dot_data = tree.export_graphviz(clf, out_file=None, 
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)  

graph = graphviz.Source(dot_data)
graph

你可能感兴趣的:(python中GraphViz's executables not found的解决方法以及决策树可视化)