简体   繁体   English

AttributeError:“列表”对象没有属性“ write_pdf”

[英]AttributeError: 'list' object has no attribute 'write_pdf'

I started studying machine learning. 我开始学习机器学习。 I am following a google tutorial, but I face this error and the answers that I have found haven't work in my code. 我正在关注google教程,但遇到此错误,我找到的答案在我的代码中不起作用。 I'm not sure but it seems that the Python version has changed and doesn't use some library anymore. 我不确定,但似乎Python版本已更改,不再使用某些库。

This is the error: 这是错误:

[0 1 2]
[0 1 2]

Warning (from warnings module):
  File "C:\Users\Moi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\externals\six.py", line 31
    "(https://pypi.org/project/six/).", DeprecationWarning)
DeprecationWarning: The module is deprecated in version 0.21 and will be removed in version 0.23 since we've dropped support for Python 2.7. Please rely on the official version of six (https://pypi.org/project/six/).
Traceback (most recent call last):
  File "C:\Users\Moi\Desktop\python\ML\decision tree.py", line 30, in <module>
    graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'

This is the code: 这是代码:

import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris ()
test_idx = [0,50,100]

#training data
train_target = np.delete(iris.target ,test_idx)
train_data = np.delete(iris.data, test_idx, axis= 0)

#testing data
test_target = iris.target [test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier ()
clf.fit (train_data, train_target)
print (test_target )
print (clf.predict (test_data))
# viz code
from sklearn.externals.six import StringIO
import pydot
dot_data =StringIO()
tree.export_graphviz(clf,
        out_file=dot_data,
        feature_names=iris.feature_names,
        class_names=iris.target_names,
        filled= True, rounded=True,
        impurity=False)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

graph_from_dot_data returns a tuple, you have to explode it to get to the graph. graph_from_dot_data返回一个元组,您必须将其graph_from_dot_data才能到达图。

Change: 更改:

graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

to: 至:

(graph,) = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")

Credit: https://www.programcreek.com/python/example/84621/pydot.graph_from_dot_data 信用: https : //www.programcreek.com/python/example/84621/pydot.graph_from_dot_data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 graph.write_pdf(“iris.pdf”) AttributeError: &#39;list&#39; 对象没有属性 &#39;write_pdf&#39; - graph.write_pdf(“iris.pdf”) AttributeError: 'list' object has no attribute 'write_pdf' Weasyprint 在调用 write_pdf 时获得未定义的属性:“AttributeError: &#39;PosixPath&#39; 对象没有属性 &#39;read_text&#39;” - Weasyprint get undefined property at invoking write_pdf: "AttributeError: 'PosixPath' object has no attribute 'read_text'" 试图做一个联系人列表 AttributeError: 'list' object has no attribute 'write' - trying to do a contact list AttributeError: 'list' object has no attribute 'write' AttributeError: 'list' object 没有属性 - AttributeError: 'list' object has no attribute AttributeError:'Sheet'对象没有属性'write' - AttributeError: 'Sheet' object has no attribute 'write' AttributeError:“工作簿”对象没有属性“写” - AttributeError: 'Workbook' object has no attribute 'write' AttributeError:&#39;tuple&#39;对象没有属性&#39;write&#39; - AttributeError: 'tuple' object has no attribute 'write' AttributeError: &#39;DataFrame&#39; 对象没有属性 &#39;write&#39; - AttributeError: 'DataFrame' object has no attribute 'write' AttributeError:&#39;str&#39;对象没有属性&#39;write&#39; - AttributeError: 'str' object has no attribute 'write' AttributeError: 'tuple' object has no attribute 'write' 错误 - AttributeError: 'tuple' object has no attribute 'write' Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM