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

来源:互联网 发布:eclipse python import 编辑:程序博客网 时间:2024/06/05 10:13

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

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

我使用的是python3.4

1
2
3
4
5
6
from sklearn.externals.siximport 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:

1
2
3
4
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:
1
2
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

来源:https://www.cnblogs.com/iamxyq/p/5918611.html

阅读全文
0 0