我正在玩决策树算法并尝试绘制树。但是 IDE 报告了以下错误:
Couldn't import dot_parser, loading of dot files will not be possible.
<class 'pandas.core.frame.DataFrame'>
Traceback (most recent call last):
File "C:/Users/s152730/Desktop/exe1.py", line 70, in <module>
graph = pydot.graph_from_dot_data(test.getvalue())
File "C:\Python27\lib\site-packages\pydot.py", line 220, in graph_from_dot_data
return dot_parser.parse_dot_data(data)
NameError: global name 'dot_parser' is not defined
我不知道如何处理这个问题,因为我试图卸载并重新安装 pydot 和 pyparsing,这是在其他答案中提出的,但它没有帮助。
这是我的代码:
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
from IPython.display import Image
test = StringIO()
tree.export_graphviz(clf, out_file=test, feature_names = attribute_names)
graph = pydot.graph_from_dot_data(test.getvalue())
graph.writepng('test.png')
image(filename = 'test.png')
我正在使用python2.7并在PyCharm上运行,操作系统是win8.1。谢谢你的帮助。