1

我正在尝试使用来自 scikit-learn 的 iris 数据集训练决策树。我尝试运行以下命令:

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()) 

我收到以下错误:

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

有人可以帮我解决这个问题吗。谢谢

回溯我得到的错误

() ----> 1 graph = pydot.graph_from_dot_data(dot_data.getvalue()) 中的 TypeError Traceback (最近一次调用最后一次)

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

第218话 第219话

--> 220 返回 dot_parser.parse_dot_data(data) 221 222

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

第508章 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

4

1 回答 1

0

@托马斯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
于 2016-03-09T06:30:27.477 回答