如何解决" NameError:name' export_graphviz'没有定义"

时间:2018-02-05 03:46:30

标签: jupyter-notebook graphviz

export_graphviz未定义错误!!

Used the following codes in Jupyrer notebook Python 3.x

`def show_tree (tree, features, path):
        f = io.StringIO()
        export_graphviz(tree, out_file = f, feature_names=features )
        pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
        img = misc.imread(path)
        plt.rcParams['figure.figsize']=[20,20]
        plt.imshow(img)

show_tree(DT,特征' dc_tree.png')     `

调用show_tree函数

时出现以下错误
        `NameError Traceback (most recent call last)
    <ipython-input-21-96002279767f> in <module>()
    ----> 1 show_tree(dt,features,'dc_tree.png')

    `<ipython-input-20-f73dae020a9a> in show_tree(tree, features, path)
          4 def show_tree (tree, features, path):
          5     f = io.StringIO()
    ----> 6     export_graphviz(tree, out_file = f, feature_names=features )
          7     pydotplus.graph_from_dot_data(f.getvalue()).write_png(path)
          8     img = misc.imread(path)`

NameError:name&#39; export_graphviz&#39;未定义

1 个答案:

答案 0 :(得分:0)

要使用export_graphviz导出器,您需要从sklearn.tree导入它。

尝试

from sklearn import tree

see example at the bottom of this page

from sklearn.tree import DecisionTreeClassifier, export_graphviz