【问题标题】:Python: NameError: global name 'dot_parser' is not definedPython:NameError:未定义全局名称“dot_parser”
【发布时间】:2015-07-19 13:37:16
【问题描述】:

Python 对我来说相当新。

我正在尝试运行“Machine Learning in Python with Scikit”一书中的巨大机器学习示例。使用决策树的分类工作正常(clf 定义正确),但如果我想可视化决策树(参见下面的代码 sn-p),我收到以下错误消息(从 IPython 复制)。

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-15b1b4a5d909> in <module>()
      3 dot_data = StringIO.StringIO()
      4 tree.export_graphviz(clf, out_file = dot_data, feature_names = ['PClass', 'AgeFill', 'Gender'])
----> 5 graph = pydot.graph_from_dot_data(dot_data.getvalue())
      6 graph.write_png('titanic.png')

C:\Users\885299\AppData\Local\Continuum\Anaconda32\lib\site-packages\pydot.pyc in graph_from_dot_data(data)

    218     """
    219 
--> 220     return dot_parser.parse_dot_data(data)
    221 
    222 
NameError: global name 'dot_parser' is not defined

有人可以帮助我吗?

我使用的代码 sn-p(类似于本书)是:

import pydot, StringIO

dot_data = StringIO.StringIO()
tree.export_graphviz(clf, out_file = dot_data, feature_names = ['Class', 'Age', 'Gender'])
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_png('titanic.png')

from IPython.core.display import Image
Image(filename = 'titanic.png')

【问题讨论】:

  • 欢迎来到 Stack Exchange!我想说谢谢你把一个格式良好的问题放在一起。我建议您省略“有人可以帮助我吗?”来自未来的帖子,因为这对 SO 社区的许多成员来说可能是一个危险信号。否则,这看起来是一个可靠的问题。
  • dot_parser 在什么包中?你导入了吗(就像你导入了pydotStringIO一样)?

标签: python


【解决方案1】:

github上有一个related issue。建议是确保您“已安装 pyparser 库并对其进行更新”。但是,我很确定它们是指 pyparsing 库。

你可以通过运行pip install pyparsing来安装pyparsing

你可以通过运行pip install -U pyparsing来更新pyparsing

另外,related stackoverflow question 建议卸载 pyparsing,然后重新安装 pyparsing 和 pydot。

【讨论】:

  • 只是一个小问题:在 Windows PC 上,我应该只在命令行上键入 pip install pyparsing 吗?如果有,在哪个目录下?
  • pip 在 python 版本 3.4 之前没有随 python 一起安装。这是一个很棒的SO Post,关于如何在 Windows 上安装pip
【解决方案2】:

如果您使用的是 Python 3,那么使用 pydotplus 而不是 pydot 对我来说效果很好。

Here is the github repo

【讨论】:

  • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
【解决方案3】:

我也强烈推荐使用 pydotplus 而不是 pydot(好像 pydot 和 Python3 有问题)。

这篇博文帮助了我: http://www.sas-programming.com/2015/04/sklearn-decisiontree-plot-example-needs.html

【讨论】:

    【解决方案4】:

    我使用了以下内容并使其与 Python3 一起使用。 Pyparser 2.2.0 兼容 pydot。

    pip install pyparsing==2.2.0
    pip install pydot
    

    这安装了 pydot 1.2.3。

    如果您之前安装了任何 pydot 软件包,请先使用 pip uninstall pydot 卸载它们,然后按照上述步骤进行全新安装。

    【讨论】:

      【解决方案5】:

      很好的解决方案,谢谢。我在 Ubuntu 14.04 上遇到了同样的问题。只需几句话:当我尝试卸载 pyparsing 和 pydot 时出现错误:

      不卸载 /usr/lib/python2.7/dist-packages 中的 pydot,归操作系统所有

      我解决了升级 pip 的问题 sudo pip install --upgrade pip

      然后执行以下命令: sudo -H pip uninstall pydot sudo -H pip uninstall pyparsing 并重新安装: sudo -H pip install pyparsing sudo -H pip install pydot

      【讨论】:

        猜你喜欢
        • 2016-06-21
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        • 2011-05-03
        • 1970-01-01
        • 2018-08-09
        • 1970-01-01
        相关资源
        最近更新 更多