Convert decision tree directly to png
<p>I am trying to generate a decision tree which I want to visualize using dot. The resulting dotfile shall be converted to png.</p>

<p>While I can do the last conversion step in dos using something like</p>

<pre><code>export_graphviz(dectree, out_file="graph.dot")
</code></pre>

<p>followed by a DOS command</p>

<pre><code>dot -Tps graph.dot -o outfile.ps
</code></pre>

<p>doing all this directly in python dows not work and generates an error </p>

<pre><code>AttributeError: 'list' object has no attribute 'write_png'
</code></pre>

<p>This is the program code I have tried:</p>

<pre><code>from sklearn import tree  
import pydot
import StringIO

# Define training and target set for the classifier
train = [[1,2,3],[2,5,1],[2,1,7]]
target = [10,20,30]

# Initialize Classifier. Random values are initialized with always the same random seed of value 0 
# (allows reproducible results)
dectree = tree.DecisionTreeClassifier(random_state=0)
dectree.fit(train, target)

# Test classifier with other, unknown feature vector
test = [2,2,3]
predicted = dectree.predict(test)

dotfile = StringIO.StringIO()
tree.export_graphviz(dectree, out_file=dotfile)
graph=pydot.graph_from_dot_data(dotfile.getvalue())
graph.write_png("dtree.png")
</code></pre>

<p>What am I missing?</p>