在路径中找不到“ .dot”-将PATH添加到Windows环境变量后

时间:2019-04-07 19:10:39

标签: python-2.7 path tree random-forest

我正在尝试使用Graphviz2可视化随机森林。我在下面使用的代码。但是,当我尝试读取.dot文件时,出现以下错误:

  

FileNotFoundError:[WinError 2]在路径中找不到“点”

我看到其他人遇到此错误,并通过修改Windows环境变量解决了该错误。我在PATH中添加了以下内容:

  

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

这些都不起作用。

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

1 个答案:

答案 0 :(得分:0)

提到的路径是可以的。您的dot_data文件本身存在问题。另请参阅内联注释以获取清晰性。

#根据Python PEP约定在此处导入所有语句。

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