'How To resolve "NameError: name 'export_graphviz' is not defined"

export_graphviz is not defined error!!

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') `

Got the following error when calling the function 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 'export_graphviz' is not defined



Solution 1:[1]

To use the export_graphviz exporter you need to import it from sklearn.tree.

Try

from sklearn import tree

(see example at the bottom of this page)

or

from sklearn.tree import DecisionTreeClassifier, export_graphviz

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A. Beal