【问题标题】:How To resolve "NameError: name 'export_graphviz' is not defined"如何解决“NameError:名称'export_graphviz'未定义”
【发布时间】:2018-07-14 21:33:34
【问题描述】:

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,features,'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:名称“export_graphviz”未定义

【问题讨论】:

  • 错误表示当前闭包中没有函数export_graphviz的定义

标签: jupyter-notebook graphviz


【解决方案1】:

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

试试

from sklearn import tree

(see example at the bottom of this page)

from sklearn.tree import DecisionTreeClassifier, export_graphviz

【讨论】:

    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2021-04-15
    • 2019-01-26
    相关资源
    最近更新 更多