2016-11-14 78 views
0

我從Visualizing a Decision Tree - Machine Learning 進口numpy的下面的代碼爲NP 從sklearn.datasets從sklearn進口樹導入load_iris 創建PDF使用pydot

iris = load_iris() 
test_idx = [0, 50 , 100] 

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

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

我想在我的Python 3.5運行它,但我得到說圖表是一個列表的錯誤。

Traceback (most recent call last): 
    File "Iris.py", line 31, in <module> 
    graph.write_pdf("iris.pdf") 
AttributeError: 'list' object has no attribute 'write_pdf' 
Press any key to continue . . . 

爲什麼這裏是一個列表?

回答