2016-02-09 26 views
3

我試圖運行下面的代碼沒有屬性「graph_from_dot_data」:AttributeError的:模塊「pydot」在Spyder的

from sklearn.datasets import load_iris 
from sklearn import tree 
import pydot 
clf = tree.DecisionTreeClassifier() 
iris = load_iris() 
clf = clf.fit(iris.data, iris.target) 
from sklearn.externals.six import StringIO 
from pydot import * 
dotfile = StringIO() 
tree.export_graphviz(clf, out_file = dotfile) 
pydot.graph_from_dot_data(dot_data.getvalue()).write_png("dtree2.png") 

,我也得到了以下錯誤: AttributeError的:模塊「pydot」有沒有屬性'graph_from_dot_data'

我已經盡力找到解決方案,但無法做到這一點。在這方面請有人幫助我。

+1

嘗試更新您的pydot包 – Arseniy

+0

阿爾謝尼,我試圖installe更新軟件包,如pydotplus和pydot2但無濟於事。你能告訴我這個嗎? –

+0

獲取模塊路徑: 在'pydot.graph_from_dot_data'之前加上'print pydot .__ file__'並檢查路徑是否正確 – Arseniy

回答

4

1)使用pydotplus如果你正在使用python 3+

2)的最後一行更改爲pydotplus.graph_from_dot_data(dotfile.getvalue())。write_png( 「dtree2.png」)爲您的變量名字是 'dotfile' 而不是 'dot_data'

PS - 重新安裝graphviz的安裝後pydotplus

希望這有助於!

+0

親愛的Jinesh,我會試試看!非常感謝你。 –

2

pydot.graph_from_dot_data()返回一個列表,所以嘗試:

graphs = pydot.graph_from_dot_data(dot_data.getvalue()) 
graphs[0].write_png("dtree2.png") 
相關問題