Jan*_*ila 7 graphviz pydot scikit-learn
我正在关注scikit文档的决策树教程.我有,pydotplus 2.0.2但它告诉我它没有write方法 - 错误如下.我现在一直苦苦挣扎,有什么想法,好吗?非常感谢!
from sklearn import tree
from sklearn.datasets import load_iris
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
from IPython.display import Image
dot_data = tree.export_graphviz(clf, out_file=None)
import pydotplus
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
Image(graph.create_png())
Run Code Online (Sandbox Code Playgroud)
而我的错误是
/Users/air/anaconda/bin/python /Users/air/PycharmProjects/kiwi/hemr.py
Traceback (most recent call last):
File "/Users/air/PycharmProjects/kiwi/hemr.py", line 10, in <module>
dot_data = tree.export_graphviz(clf, out_file=None)
File "/Users/air/anaconda/lib/python2.7/site-packages/sklearn/tree/export.py", line 375, in export_graphviz
out_file.write('digraph Tree {\n')
AttributeError: 'NoneType' object has no attribute 'write'
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
-----更新-----
使用修复程序out_file,它会引发另一个错误:
Traceback (most recent call last):
File "/Users/air/PycharmProjects/kiwi/hemr.py", line 13, in <module>
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
File "/Users/air/anaconda/lib/python2.7/site-packages/pydotplus/graphviz.py", line 302, in graph_from_dot_data
return parser.parse_dot_data(data)
File "/Users/air/anaconda/lib/python2.7/site-packages/pydotplus/parser.py", line 548, in parse_dot_data
if data.startswith(codecs.BOM_UTF8):
AttributeError: 'NoneType' object has no attribute 'startswith'
Run Code Online (Sandbox Code Playgroud)
---- 更新2 -----
另外,下面我自己的答案解决了另一个问题
问题是你要设置参数out_fileNone NoneNone Nonestring stringstring`没有'write'方法.
因此,请执行以下操作:
dot_data = tree.export_graphviz(clf)
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14042 次 |
| 最近记录: |