[英]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.