【发布时间】:2017-07-14 23:38:50
【问题描述】:
我编写了代码来可视化决策树模型。最初,我遇到了 graphviz's executables not found 之类的错误,但我将其路径添加到环境变量中,甚至重新安装了 graphviz 模块。它现在似乎工作正常。但是现在出现以下错误:
Traceback (most recent call last):
File "C:/Ankur/Python36/Python Files/Decision_Tree.py", line 57, in
<module>
Image(graph.create_png())
TypeError: 'module' object is not callable
代码如下。
from sklearn import tree
from io import StringIO
from PIL import Image
from graphviz import Graph
import pydotplus as py
# Code for creating the model and fitting the data.
#...........................
dot_data=StringIO()
tree.export_graphviz(clf,out_file=dot_data)
graph=py.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
【问题讨论】:
-
尝试将“from graphviz import Graph”中的 Graph 重命名为其他名称。那里的“图形”可能会干扰变量“图形”,因为当您调用它时,“图形”最终会成为一个模块,而不是来自 graph_from_dot_data() 的结果。也许你在一个不区分大小写的平台上,由于 python 中的一些边缘情况,Graph 可以以某种方式给图取别名。
-
@Hannes Landeholm 我正在使用 PyCharm,它区分大小写。我确实从 GRAPHVIZ 导入了 GRAPH,但我没有使用它。所以,我相信错误在其他地方。我检查了'graph'的类,它是'pydotplus.graphviz.Dot'。
标签: python python-imaging-library