Open MHDeveloping opened 6 years ago
someone has had the same question 4 month ago, placed it on stackover: "https://stackoverflow.com/questions/48615609/how-to-resolve-nameerror-name-export-graphviz-is-not-defined"
I installed graphviz via pip, even I thought its useless, as it was with scipy, and it was usls
pretty trival, cause here its: "import graphviz as g" so here at this line it needs to be graph = g.Source(dot_data.getvalue())
If the community decides its a useless thread ... be done with it
Please use pydotplus instead of pydot
Find:C:\Users\zhangqianyuan\AppData\Local\Programs\Python\Python36\Lib\site-packages\pydotplus
Open graphviz.py
Find line 1925 - line 1972, find the function:
def create(self, prog=None, format='ps'):
In the function find:
if prog not in self.progs:
raise InvocationException(
'GraphViz\'s executable "%s" not found' % prog)
if not os.path.exists(self.progs[prog]) or \
not os.path.isfile(self.progs[prog]):
raise InvocationException(
'GraphViz\'s executable "{}" is not'
' a file or doesn\'t exist'.format(self.progs[prog])
)
Between the two block add this(Your Graphviz's executable path):
self.progs[prog] = "C:/Program Files (x86)/Graphviz2.38/bin/gvedit.exe"`
After adding the result is:
if prog not in self.progs:
raise InvocationException(
'GraphViz\'s executable "%s" not found' % prog)
self.progs[prog] = "C:/Program Files (x86)/Graphviz2.38/bin/gvedit.exe"
if not os.path.exists(self.progs[prog]) or \
not os.path.isfile(self.progs[prog]):
raise InvocationException(
'GraphViz\'s executable "{}" is not'
' a file or doesn\'t exist'.format(self.progs[prog])
)
save the changed file then you can run it successfully.
you'd better save it as bmp file because png file will not work. pucture is here
### Actual Behavior
"NameError: name 'graphviz' is not defined" occured after : #Visualize the tree from sklearn.externals.six import StringIO import graphviz as g 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, impurity=False) graph = graphviz.Source(dot_data.getvalue()) graph.render("iris.pdf", view=True)
### Expected Behavior
Well, the pdf iris
should create the pdf iris.pdf
### Steps to Reproduce
#testin data test_target = iris.target[test_idx] test_data = iris.data[test_idx]
clf = tree.DecisionTreeClassifier() clf.fit(train_data, train_target)
print (test_target)
print(clf.predict(test_data))
#Visualize the tree from sklearn.externals.six import StringIO import graphviz as g 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, impurity=False) graph = graphviz.Source(dot_data.getvalue()) graph.render("iris.pdf", view=True)
##### Anaconda or Miniconda version:
Anaconda 1.8.7 for Win10 64bit
##### Operating System:
Windows 10 64bit
#####
conda info
#####
conda list --show-channel-urls