2016-07-04 18 views
33

Mã của tôi tuân theo lớp học máy của google. Hai mã giống nhau.Tôi không biết tại sao nó lại hiển thị lỗi. Có thể là loại biến là error.But mã của google là giống với tôi.Who đã từng có vấn đề này?graph.write_pdf ("iris.pdf") AttributeError: đối tượng 'list' không có thuộc tính 'write_pdf'

Đây là lỗi

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

Đây là mã

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

Trả lời

53

Tôi nghĩ rằng bạn đang sử dụng phiên bản mới hơn của python. Hãy thử với pydotplus.

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

Điều này sẽ thực hiện.

+0

Cảm ơn bạn rất rất rất nhiều !!!! Phương pháp này đã giải quyết được vấn đề của tôi. –

+3

Tôi chỉ có một cái nhìn. Đồ thị là một danh sách và nó chứa một đối tượng pydot.Dot.I sử dụng đối tượng pydot.Dot gọi hàm write_pdf ("iris.pdf") , nó cũng giải quyết được câu hỏi. –

+0

@ 乔守卿 Bạn đã sử dụng cú pháp nào/bạn đã sử dụng pydot.Dot như thế nào? – programmer

16

pydot.graph_from_dot_data() trả về một danh sách, vì vậy hãy thử:

graph = pydot.graph_from_dot_data(dot_data.getvalue()) 
graph[0].write_pdf("iris.pdf") 
+1

Cảm ơn câu trả lời. Giải pháp đơn giản đủ mà không cần phải trải qua những rắc rối của pydotplus – maheeka

+0

Điều đó làm việc cho tôi. Cảm ơn bạn! –

0

tôi cài đặt scikit-học qua conda và tất cả khoảng không làm việc. Trước hết, tôi phải cài đặt libtool

brew install libtool --universal 

Sau đó, tôi làm theo this sklearn guide Sau đó thay đổi file python để mã này

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

Cuối cùng chuyển đổi sang png trong terminal

dot -Tpng tree.dot -o tree.png 
0
import pydotplus 
... 
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
graph.write_pdf("iris.pdf") 

Tôi có Python 3.6.0 | Anaconda 4.3.1 và gặp lỗi:

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

InvocationException: GraphViz's executables not found

+1

Bạn nên cài đặt GraphViz đầu tiên từ đây (http://www.graphviz.org/Download..php) và sau đó thêm thư mục "bin" của nó với các tệp thi hành (c: \ Program Files (x86) \ Graphviz2.38 \ bin) vào PATH của máy tính của bạn. –

1

@ Alex Sokolov, đối với trường hợp của tôi trong cửa sổ, tôi đã tải về và cài đặt/giải nén the following vào một thư mục sau đó thiết lập các PATH in Windows environment variables. chạy lại mã py làm việc cho tôi. hy vọng là hữu ích cho bạn.

3

Tôi đã có chính xác cùng một vấn đề. Hóa ra là tôi chưa cài đặt graphviz. Một khi tôi đã làm điều đó nó bắt đầu làm việc.

Các vấn đề liên quan