1
votes

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

1
Weird. Your code is working for me. What line is throwing the error? - David Maust
@Arman When I enter this line: graph = pydot.graph_from_dot_data(dot_data.getvalue()) - Priya
Can you show the full traceback you get with that error? - Thomas K
@ThomasK do you have an answer to this? - eikooc

1 Answers

0
votes

@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