graph.write_pdf(“iris.pdf”) AttributeError: 'list' object has no attribute 'write_pdf'

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

My code is follow the class of machine learning of google.The two code are same.I don't know why it show error.May be the type of variable is error.But google's code is same to me.Who has ever had this problem?

This is error

[0 1 2] [0 1 2] Traceback (most recent call last):   File "/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py", line 34, in <module>     graph.write_pdf("iris.pdf") AttributeError: 'list' object has no attribute 'write_pdf' [Finished in 0.4s with exit code 1] [shell_cmd: python -u "/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py"] [dir: /media/joyce/oreo/python/machine_learn] [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games] 

This is 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") 

回答1:

I think you are using newer version of python. Please try with pydotplus.

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

This should do it.



回答2:

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")  


回答3:

I had exactly the same issue. Turned out that I hadn't installed graphviz. Once i did that it started to work.



回答4:

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

I have Python 3.6.0 |Anaconda 4.3.1 and get error:

File "C:\Anaconda\lib\site-packages\pydotplus\graphviz.py", line 1960, in create 'GraphViz\'s executables not found')

InvocationException: GraphViz's executables not found



回答5:

@Alex Sokolov, for my case in window, i downloaded and install / unzip the following to a folder then setup the PATH in Windows environment variables. re-run the py code works for me. hope is helpful to you.



回答6:

I install scikit-learn via conda and all of about not work. Firstly, I have to install libtool

brew install libtool --universal 

Then I follow this sklearn guide Then change the python file to this code

clf = clf.fit(train_data, train_target) tree.export_graphviz(clf,out_file='tree.dot')  

Finally convert to png in terminal

dot -Tpng tree.dot -o tree.png 


回答7:

I tried the previous answers and still got a error when running the script Therefore, I just used pydotplus

import pydotplus 

and install the "graphviz" by using:

sudo apt-get install graphviz 

Then it worked for me, and I added

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

Thanks to the previous contributors.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!