Venugopal Bukkala
Venugopal Bukkala

Reputation: 179

AttributeError: module 'pydot' has no attribute 'graph_from_dot_data' in spyder

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.

Upvotes: 4

Views: 5179

Answers (2)

Iliyan Bobev
Iliyan Bobev

Reputation: 3108

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")

Upvotes: 3

Jinesh Shah
Jinesh Shah

Reputation: 55

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!

Upvotes: 5

Related Questions