Search code examples
python-2.7pathtreerandom-forest

".dot" not found in path - After adding PATH to Windows Environment variables


I am trying to visualize the random forest using Graphviz2. The code that I am using is given below. However, when I try to read the .dot file it gives the following error:

FileNotFoundError: [WinError 2] "dot" not found in path

I have seen other people encounter this error and have solved it by modifying the Windows Environment Variables. I added the following to the PATH:

C:\Program Files (x86)\Graphviz2.38\bin;
C:\Program Files (x86)\Graphviz2.38\;
C:\Users\Adarsh\Anaconda3\Lib\site-packages\graphviz

None of this works.

import pickle
filename = '11f_rf_roc91.sav'
loaded_model = pickle.load(open(filename, 'rb'))

import os
from sklearn.tree import export_graphviz
import six
import pydot
from sklearn import tree
dotfile = six.StringIO()
i_tree = 0
for tree_in_forest in loaded_model.estimators_:
    export_graphviz(tree_in_forest, out_file='tree.dot', feature_names = list(X), class_names = ["0", "1"], rounded = True, proportion = False, precision = 2, filled = True)
    (graph,) = pydot.graph_from_dot_file('tree.dot')
    name = 'tree' + str(i_tree)
    graph.write_png(name+  '.png')
    os.system('dot -Tpng tree.dot -o tree.png')
    i_tree +=1

Solution

  • The mentioned paths are okay. There was an issue with your dot_data file itself. See inline comments too for clarity.

    # all import statements here according to Python PEP convention.

    import os
    import pydot
    import pickle
    
    from sklearn import tree
    from sklearn.tree import export_graphviz     # this can be even > import export_graphviz <
    from sklearn.externals.six import StringIO   # shortened StringIO instead of six.StringIO
    
    filename = '11f_rf_roc91.sav'
    loaded_model = pickle.load(open(filename, 'rb'))
    
    dot_data = StringIO()          # this is data not filename "dotfile".
    i_tree = 0
    
    for tree_in_forest in loaded_model.estimators_:
        export_graphviz(tree_in_forest, 
                        out_file      = dot_data,    # now linked to correct file data
                        feature_names = list(X),     # X seems not to be defined.
                        class_names   = ["0", "1"], 
                        rounded       = True, 
                        proportion    = False, 
                        precision     = 2, 
                        filled        = True)
    
        graph = pydot.graph_from_dot_file(dot_data)
        name = 'tree' + str(i_tree)
        graph.write_png(name+  '.png')
        os.system('dot -Tpng tree.dot -o tree.png')
        i_tree += 1