1

I am trying to train a decision tree with iris dataset from scikit-learn. I tried running the following command:

from sklearn.datasets import load_iris
from sklearn import tree
clf = tree.DecisionTreeClassifier()
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf,out_file='tree.dot')  

from sklearn.externals.six import StringIO  
import pydot 
dot_data = StringIO() 
tree.export_graphviz(clf, out_file=dot_data) 
graph = pydot.graph_from_dot_data(dot_data.getvalue()) 

I got the following error:

TypeError: startswith first arg must be str or a tuple of str, not bytes

Can someone help me to sort this issue.Thank you

Traceback that I get with the error

TypeError Traceback (most recent call last) in () ----> 1 graph = pydot.graph_from_dot_data(dot_data.getvalue())

C:\Users\Priya\Anaconda3\Lib\site-packages\pydot.py in graph_from_dot_data(data)

218 """ 219

--> 220 return dot_parser.parse_dot_data(data) 221 222

C:\Users\Priya\Anaconda3\Lib\site-packages\dot_parser.py in parse_dot_data(data)

508 top_graphs = list() 509

--> 510 if data.startswith(codecs.BOM_UTF8): 511 data = data.decode( 'utf-8' ) 512

TypeError: startswith first arg must be str or a tuple of str, not bytes

Priya
  • 65
  • 7

1 Answers1

0

@Thomas K

TypeError                                 Traceback (most recent call last)
<ipython-input-12-5394fc84af26> in <module>()
----> 1 graph = pydot.graph_from_dot_data(dot_data.getvalue())

C:\Users\Priya\Anaconda3\Lib\site-packages\pydot.py in graph_from_dot_data(data)

    218     """
    219
--> 220     return dot_parser.parse_dot_data(data)
    221
    222

C:\Users\Priya\Anaconda3\Lib\site-packages\dot_parser.py in parse_dot_data(data)

    508     top_graphs = list()
    509
--> 510     if data.startswith(codecs.BOM_UTF8):
    511         data = data.decode( 'utf-8' )
    512

TypeError: startswith first arg must be str or a tuple of str, not bytes
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • 1
    BTW, on SO, you're supposed to edit the question with extra details rather than posting them as an answer. It looks like the 'pydot' package on PyPI is [not Python 3 compatible](https://github.com/erocarrera/pydot/issues/76), but several people have made Python 3 compatible versions: https://github.com/nlhepler/pydot/issues/12 – Thomas K Apr 21 '16 at 10:56