4

I am trying to run the following code:

from sklearn.datasets import load_iris
from sklearn import tree
import pydot
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
from sklearn.externals.six import StringIO
from pydot import *
dotfile = StringIO()
tree.export_graphviz(clf, out_file = dotfile)
pydot.graph_from_dot_data(dot_data.getvalue()).write_png("dtree2.png")

and i get the following error: AttributeError: module 'pydot' has no attribute 'graph_from_dot_data'

I have tried hard to find the solution but could not be able to do so. Please someone help me in this regard.

2 Answers2

5

1) Use pydotplus if you are using python 3+

2) Change the last line to pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png("dtree2.png") as your variable name is 'dotfile' and not 'dot_data'

P.S - reinstall graphviz after you install pydotplus

Hope this helps!

3

pydot.graph_from_dot_data() returns a list, so try:

graphs = pydot.graph_from_dot_data(dot_data.getvalue())
graphs[0].write_png("dtree2.png")
Iliyan Bobev
  • 3,070
  • 2
  • 20
  • 24