Python 如何绘制决策树?(TypeError:从点数据开始的图形中的StartWith,pydot)

Python 如何绘制决策树?(TypeError:从点数据开始的图形中的StartWith,pydot),python,machine-learning,scikit-learn,decision-tree,pydot,Python,Machine Learning,Scikit Learn,Decision Tree,Pydot,我刚开始使用Scikit学习、机器学习和Python。我试着用一个决策树。在我试图得到决策树图之前,我设法完成了所有的数据清理、分析等工作 我正在使用Python 3.4和pyplot2。我有一个名为decision_tree的函数来创建模型,然后调用一个函数(plot_classifier)giving(clf)来用以下几行代码绘制模型: dot_data = StringIO() export_graphviz(clf, out_file=dot_data) prin

我刚开始使用Scikit学习、机器学习和Python。我试着用一个决策树。在我试图得到决策树图之前,我设法完成了所有的数据清理、分析等工作

我正在使用Python 3.4和
pyplot2
。我有一个名为decision_tree的函数来创建模型,然后调用一个函数(plot_classifier)giving(clf)来用以下几行代码绘制模型:

    dot_data = StringIO()
    export_graphviz(clf, out_file=dot_data)
    print(type(dot_data.getvalue()))
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    Image(graph.create_png())
此代码类似于Scikit精益代码。问题在标记行中。我已经拟合了我的模型并检查了结果。它们还可以,但我不知道怎么画这棵树。我把这个放在控制台上

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-66-900e97a70715> in <module>()
----> 1 decision_tree(feature_train, feature_test, label_train, label_test)

<ipython-input-3-c2e245caf6c8> in decision_tree(feature_train, feature_test,     label_train, label_test)
 17     print("Cantidad de aciertos: " + str(count) + "\n Cantidad de Elementos: " + str(len(pred)))
 18     print(no_match)
---> 19     plot_classifier(clf)
20     return accuracy
21 

<ipython-input-65-78f8ba2dc1ed> in plot_classifier(clf)
  3         export_graphviz(clf, out_file=dot_data)
  4         print(type(dot_data.getvalue()))
----> 5         graph = pydot.graph_from_dot_data(dot_data.getvalue())
  6         Image(graph.create_png())
  7 

C:\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:\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决策树(特征列、特征列、标签列、标签列)
在决策树中(特征列、特征列、标签列、标签列)
17印刷体(“Cantidad de aciertos:+str(计数)+”\n Cantidad de Elementos:+str(len(pred)))
18打印(不匹配)
--->19 plot_分类器(clf)
20返回精度
21
图内分类器(clf)
3导出图形(clf,输出文件=点数据)
4打印(键入(dot_data.getvalue())
---->5 graph=pydot.graph_from_dot_data(dot_data.getvalue())
6图像(graph.create_png())
7.
C:\Anaconda3\lib\site packages\pydot.py在来自\u dot\u数据(数据)的图形\u中
218     """
219
-->220返回dot_解析器。解析dot_数据(数据)
221
222
C:\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的元组,而不是字节
我在互联网上查到错误msj,但答案是关于
startswith
行的(那是在图书馆里,我认为当很多人都这么做的时候不会有问题)。我在其他行上查到问题,也找不到我有问题的那一行


有人能帮我吗?我尝试过转换成
string
tuple
(即使你
getvalues()
已经返回了
string
),但什么都没有。我在Linux上遇到了同样的问题,并解决了安装pydotplus及其依赖项的问题

安装pydotplus及其依赖项后:

import pydotplus

graph = pydotplus.pydotplus.graph_from_dot_data(dot_data.getvalue())