scikit-learn:决策树

时间:2016-03-06 15:35:37

标签: python scikit-learn pydot

我正在尝试使用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

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

我收到错误的回溯

在()---->中的TypeError Traceback(最近的最后一次调用) 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返回dot_parser.parse_dot_data(data)221 222

parse_dot_data(data)中的

C:\ Users \ Priya \ Anaconda3 \ Lib \ site-packages \ dot_parser.py

508 top_graphs = list() 509

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

TypeError:startswith first arg必须是str或str的元组,而不是字节

1 个答案:

答案 0 :(得分: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