AttributeError: 'list' object has no attribute 'write_pdf'

我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf'

我使用的是python3.4

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()) 
graph.write_pdf("iris.pdf") 

查阅资料后发现,原来我使用的是较新版本的python.可以采取如下两种解决方案:

  • try with pydotplus:

import pydotplus
...
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
  • pydot.graph_from_dot_data() returns a list, so try:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf") 

 

REF.

[1]http://stackoverflow.com/questions/38176472/graph-write-pdfiris-pdf-attributeerror-list-object-has-no-attribute-writ

posted on 2016-09-29 08:38  pinweihelai  阅读(12505)  评论(1编辑  收藏  举报