I am following the answer presented to a previous post: Is it possible to print the decision tree in scikit-learn?
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf, out_file='tree.dot')
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
Unfortunately, I cannot figure out the following error:
'list' object has no attribute 'write_pdf'
Does anyone know a way around this as the structure of the generated tree.dot file is a list?
Update
I have attempted using the web application http://webgraphviz.com/. This works, however, the decision tree conditions, together with the classes are not displayed. Is there any way to include these in the tree.dot file?
Looks like data that you collect in
graphis of typelist.We are only interested in first element of the list. So you can do this one of following of two ways,
1) Change line where you collect
dot_datavalue ingraphto2) Or collect entire list in
graphbut just use first element to be sent topdfHere is what I get as output of iris.pdf
To get around path error,
Install
graphvizfrom hereThen use either following in your code.
Or simply add following to your windows path in control panel.
As per
graphvizdocumentation, it does not get added to windows path during installation.