Python, PyDot and DecisionTree

Polly picture Polly · Jul 3, 2015 · Viewed 17.9k times · Source

I'm trying to visualize my DecisionTree, but getting the error The code is:

X = [i[1:] for i in dataset]#attribute
y = [i[0] for i in dataset]
clf = tree.DecisionTreeClassifier()

dot_data = StringIO()
tree.export_graphviz(clf.fit(train_X, train_y), out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("tree.pdf")

And the error is

Traceback (most recent call last):
if data.startswith(codecs.BOM_UTF8):
TypeError: startswith first arg must be str or a tuple of str, not bytes

Can anyone explain me whats the problem? Thank you a lot!

Answer

Juliano ENS picture Juliano ENS · Apr 6, 2016

In case of using Python 3, just use pydotplus instead of pydot. It will also have a soft installation process by pip.

import pydotplus

<your code>

dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")