简体   繁体   English

scikit-learn:决策树

[英]scikit-learn: Decision tree

I am trying to train a decision tree with iris dataset from scikit-learn. 我正在尝试使用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()) ()中的TypeError追溯(最近一次调用最近)----> 1个图= pydot.graph_from_dot_data(dot_data.getvalue())

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

218 """ 219 218“”“ 219

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

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

508 top_graphs = list() 509 508 top_graphs = list()509

--> 510 if data.startswith(codecs.BOM_UTF8): 511 data = data.decode( 'utf-8' ) 512 -> 510如果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 TypeError:从第一个arg开始必须是str或str的元组,而不是字节

@Thomas K @托马斯·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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM