Python scikit学习:决策树

Python scikit学习:决策树,python,scikit-learn,pydot,Python,Scikit Learn,Pydot,我正在尝试使用scikit学习的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

我正在尝试使用scikit学习的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\u from\u dot\u data(dot\u data.getvalue())中的TypeError回溯(最近一次调用last)

C:\Users\Priya\Anaconda3\Lib\site packages\pydot.py在图表\u中来自\u dot\u数据(数据)

218“ 219

-->220返回dot_解析器。解析dot_数据(数据)221 222

C:\Users\Priya\Anaconda3\Lib\site packages\dot\u parser.py在parse\u dot\u数据(数据)中

508顶图=列表() 509

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

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

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
TypeError回溯(最近一次调用)
在()
---->1 graph=pydot.graph_from_dot_data(dot_data.getvalue())
C:\Users\Priya\Anaconda3\Lib\site packages\pydot.py在图表\u中来自\u dot\u数据(数据)
218     """
219
-->220返回dot_解析器。解析dot_数据(数据)
221
222
C:\Users\Priya\Anaconda3\Lib\site packages\dot\u parser.py在parse\u dot\u数据(数据)中
508顶图=列表()
509
-->510如果数据.startswith(编解码器.BOM\u UTF8):
511 data=data.decode('utf-8')
512
TypeError:startswith first arg必须是str或str的元组,而不是字节
@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
TypeError回溯(最近一次调用)
在()
---->1 graph=pydot.graph_from_dot_data(dot_data.getvalue())
C:\Users\Priya\Anaconda3\Lib\site packages\pydot.py在图表\u中来自\u dot\u数据(数据)
218     """
219
-->220返回dot_解析器。解析dot_数据(数据)
221
222
C:\Users\Priya\Anaconda3\Lib\site packages\dot\u parser.py在parse\u dot\u数据(数据)中
508顶图=列表()
509
-->510如果数据.startswith(编解码器.BOM\u UTF8):
511 data=data.decode('utf-8')
512
TypeError:startswith first arg必须是str或str的元组,而不是字节

奇怪。你的代码对我有效。哪一行出错?@Arman当我输入这一行时:graph=pydot.graph\u from\u dot\u data(dot\u data.getvalue())你能展示你从错误中得到的完整回溯吗?@ThomasK你有答案吗?奇怪。你的代码对我有效。哪一行抛出了错误?@Arman当我输入这一行时:graph=pydot.graph\u from_dot\u data(dot\u data.getvalue())你能展示你从错误中得到的完整回溯吗?@ThomasK你对此有答案吗?顺便说一句,你应该用额外的细节来编辑问题,而不是将它们作为答案发布。看起来PyPI上的“pydot”包是,但有几个人已经制作了与Python 3兼容的版本:顺便说一句,你应该编辑q提供更多详细信息,而不是将其作为答案发布。看起来PyPI上的“pydot”软件包很有用,但有几个人已经制作了与Python 3兼容的版本: