Graphviz's executables are not found (Python 3.4)

后端 未结 26 1423
故里飘歌
故里飘歌 2020-11-29 05:00

I am running Python3.4 on Windows 7. I am trying to use the Python interface for graphviz. This is a script I intend to run:

from graphviz import Digraph
imp         


        
26条回答
  •  死守一世寂寞
    2020-11-29 05:42

    I tried setting up Environment variable. Didn't work.I am in Windows environment Combining above methods worked for me :

    1. Downloaded graphviz-2.38.zip from https://graphviz.gitlab.io/_pages/Download/Download_windows.html

    2. Copied the extracted folder into C:\Users\\AppData\Local\Continuum\anaconda3\pkgs\Graphviz2.38

    3. I called the Graphviz location in my code

      Build the classifier

      import sklearn.datasets as datasets
      import pandas as pd
      from sklearn.tree import DecisionTreeClassifier
      iris=datasets.load_iris()
      df=pd.DataFrame(iris.data, columns=iris.feature_names)
      y=iris.target
      
      dtree=DecisionTreeClassifier()
      dtree.fit(df,y)
      

      Build the tree

      from sklearn.externals.six import StringIO  
      from IPython.display import Image  
      from sklearn.tree import export_graphviz
      import pydotplus
      import graphviz
      import os
      os.environ\["PATH"\] += os.pathsep + 'C:/Users/tstusr/AppData/Local/Continuum/anaconda3/pkgs/Graphviz2.38/bin'
      
      dot_data = StringIO()
      export_graphviz(dtree, out_file=dot_data,  
                      filled=True, rounded=True,
                      special_characters=True)
      graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
      Image(graph.create_png())
      

      Here is how the tree looks:

提交回复
热议问题