【问题标题】:AttributeError: module 'pydot' has no attribute 'graph_from_dot_data' in spyderAttributeError:模块'pydot'在spyder中没有属性'graph_from_dot_data'
【发布时间】:2016-05-19 00:43:52
【问题描述】:

我正在尝试运行以下代码:

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'

我已经努力寻找解决方案,但无法做到。请有人在这方面帮助我。

【问题讨论】:

  • 尝试更新你的 pydot 包
  • Arseniy,我尝试安装更新的软件包,例如 pydotplus 和 pydot2,但无济于事。你能告诉我这个吗?
  • 获取模块路径:在pydot.graph_from_dot_data 之前添加print pydot.__file__ 并检查路径是否正确

标签: python pydot


【解决方案1】:

1) 如果您使用的是 python 3+,请使用 pydotplus

2) 将最后一行更改为 pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png("dtree2.png") 因为您的变量名称是 'dotfile' 而不是 'dot_data'

P.S - 安装 pydotplus 后重新安装 graphviz

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    pydot.graph_from_dot_data() 返回一个列表,所以试试:

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

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 2020-03-16
      • 2021-02-17
      • 2020-05-24
      • 2018-04-14
      • 2019-02-18
      • 1970-01-01
      • 2020-01-01
      • 2019-07-20
      相关资源
      最近更新 更多