如何绘制决策?(类型错误:从点数据开始图形中的开关,pydot)



我是Scikit Learn、机器学习和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:Anaconda3libsite-packagespydot.py in graph_from_dot_data(data)
218     """
219 
--> 220     return dot_parser.parse_dot_data(data)
221 
222 
C:Anaconda3libsite-packagesdot_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

我在网上查了错误msj,但答案是关于startswith行的(那是在图书馆里,我认为当很多人都在工作时不会有问题)。我检查了其他线路上的问题,也找不到我有问题的那个。

有人能帮我吗?我尝试过转换为stringtuple(即使是getvalues()也已经返回了string),但什么都没有。

我在Linux上遇到了同样的问题,并解决了pydotplus及其依赖项的安装问题。

https://pypi.python.org/pypi/pydotplus

安装pydotplus及其依赖项后:

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

最新更新