ContinuumIO / anaconda-issues

Anaconda issue tracking
646 stars 220 forks source link

Windows10->Anaconda->Jupyter->graphviz: name 'graphviz' is not defined #9534

Open MHDeveloping opened 6 years ago

MHDeveloping commented 6 years ago

### 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

did not get any info

##### conda list --show-channel-urls

did not get any of it
MHDeveloping commented 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

MHDeveloping commented 6 years ago

#Solution

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

heator commented 5 years ago

Please use pydotplus instead of pydot

  1. Find:C:\Users\zhangqianyuan\AppData\Local\Programs\Python\Python36\Lib\site-packages\pydotplus

  2. Open graphviz.py

  3. Find line 1925 - line 1972, find the function:

    def create(self, prog=None, format='ps'):
  4. 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])
        )
  5. Between the two block add this(Your Graphviz's executable path):

      self.progs[prog] = "C:/Program Files (x86)/Graphviz2.38/bin/gvedit.exe"`
  6. 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])
        )
  7. save the changed file then you can run it successfully.

  8. you'd better save it as bmp file because png file will not work. pucture is here