Using graphviz to plot decision tree in python

4.7k views Asked by At

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?

1

There are 1 answers

2
Anil_M On BEST ANSWER

Looks like data that you collect in graph is of type list.

graph = pydot.graph_from_dot_data(dot_data.getvalue())
type(graph)
<type 'list'>

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_data value in graph to

(graph, ) = pydot.graph_from_dot_data(dot_data.getvalue())

2) Or collect entire list in graph but just use first element to be sent to pdf

graph[0].write_pdf("iris.pdf")

Here is what I get as output of iris.pdf

enter image description here Update

To get around path error,

Exception: "dot.exe" not found in path.

Install graphviz from here

Then use either following in your code.

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'

Or simply add following to your windows path in control panel.

C:\Program Files (x86)\Graphviz2.38\bin

As per graphviz documentation, it does not get added to windows path during installation.