AttributeError: 'list' object has no attribute 'write_pdf'

from sklearn import datasets
x,y = datasets.make_classification(1000,20,n_informative = 3)from sklearn.tree import DecisionTreeClassifierdt = DecisionTreeClassifier()dt.fit(x,y)
 
  
from StringIO import StringIO
from sklearn import tree
import pydot
str_buffer = StringIO()
tree.export_graphviz(dt,out_file = str_buffer)
graph = pydot.graph_from_dot_data(str_buffer.getvalue())
graph.write("myfile.jpg")


输出错误:“AttributeError: 'list' object has no attribute 'write_pdf'”。

查了一下,是缺少Graphviz。按照下面的方式安装了Graphviz和pygraphviz。

http://blog.csdn.net/shouwangzhelv/article/details/51163535

但是安装Graphviz-dev时失败。Mac系统brew install graphviz-dev找不到“graphviz-dev


Error: No available formula with the name "graphviz-dev" 

==> Searching for a previously deleted formula...

Warning: homebrew/core is shallow clone. To get complete history run:

  git -C "$(brew --repo homebrew/core)" fetch --unshallow


Error: No previously deleted formula found.

==> Searching for similarly named formulae...

Error: No similarly named formulae found.



再查了一下stackoverflow,发现pydot已经升级为pydotplus。

然后 安装上pydotplus :

sudo pip install pydotplus

然后讲上面的的代码修改为:


from sklearn import datasets
x,y = datasets.make_classification(1000,20,n_informative = 3)
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(x,y)
from StringIO import StringIO
from sklearn import tree
import pydotplus
str_buffer = StringIO()
tree.export_graphviz(dt,out_file = str_buffer)
graph = pydotplus.graph_from_dot_data(str_buffer.getvalue())
graph.write_jpg("myfile.jpg")
 
  

你可能感兴趣的:(mac,linux,hadoop,python)