Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

python - AttributeError:'list'对象没有属性'write_pdf'-在解决了其他类似问题之后,无法解决该问题(AttributeError: 'list' object has no attribute 'write_pdf' - Can't Fix the Issue After Following the Solutions of Other Similar Questions)

I am learning Machine Learning by following Google's Machine Learning Recipe videos on YouTube.

(我正在通过在YouTube上关注Google的机器学习食谱视频来学习机器学习。)

I am using PyCharm and Anaconda.

(我正在使用PyCharm和Anaconda。)

Right now, I am facing an issue while following this video: https://www.youtube.com/watch?v=tNa99PG8hR8&list=PLOU2XLYxmsIIuiBfYad6rFYQU_jL2ryal&index=2

(目前,我在观看此视频时遇到问题: https : //www.youtube.com/watch?v=tNa99PG8hR8&list=PLOU2XLYxmsIIuiBfYad6rFYQU_jL2ryal&index=2)

I am getting this error:

(我收到此错误:)

Traceback (most recent call last):
      File "C:/Users/shawn/PycharmProjects/AnacondaProject1/video 2.py", line 38, in <module>
      graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'

Here are my codes:

(这是我的代码:)

from sklearn.datasets import load_iris
import numpy as np
from sklearn import tree

iris = load_iris()
# print iris.feature_names
# print iris.target_names
# print iris.data[0]
# print iris.target[0]
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(clf.predict(test_data))
print(test_target)

# viz code (these codes didn't run)
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")

print (test_data[0],test_target[0])
print (iris.feature_names, iris.target_names)

I have followed the solutions of other similar questions on Stackoverflow.

(我已经关注了Stackoverflow上其他类似问题的解决方案。)

Here is an example of the similar question with solution: AttributeError: 'list' object has no attribute 'write_pdf'

(这是带有解决方案的类似问题的示例: AttributeError:'list'对象没有属性'write_pdf')

But it doesn't work for my case.

(但这对我的情况不起作用。)

How can I fix the issue?

(我该如何解决该问题?)

  ask by Shawn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...