【问题标题】:Cannot use scipy.stats无法使用 scipy.stats
【发布时间】:2014-04-02 05:08:15
【问题描述】:

使用 scipy.stats 时出现错误。在导入 scipy 后的脚本中。

AttributeError: 'module' object has no attribute 'stats'

在脚本编辑器中,我可以在输入 scipy 后单击统计信息。从下拉菜单中, 在 python 控制台中,我无法从下拉菜单中选择 python.stats,它不存在。 我正在使用熊猫 2.7 和 SciPy 0.13.0 这是为什么? 有任何已知问题吗?

【问题讨论】:

  • 您是否导入了统计信息,即from scipy import statsimport scipy.stats。 stats 子包不使用import scipy 导入,但您的编辑器可能会这样做以完成制表符。
  • “脚本编辑器”? “下拉菜单”?你用的是什么编辑器?你是怎么安装的?你是怎么安装python的?在终端中,python -c "import scipy.stats" 是否给出错误?另外,显示生成错误的代码,以及导入 scipy.stats 的代码。
  • 谢谢,使用 import scipy.stats 有效。

标签: python-2.7 scipy


【解决方案1】:

扩展我的评论(列出答案)。

Scipy 与许多其他大型软件包一样,不会自动导入所有模块。如果要使用 scipy 的子包,则需要直接导入。

但是,一些 scipy 子包会加载其他 scipy 子包,因此例如导入 scipy.stats 也会导入大量其他包。但我从不依赖这个来让子包在命名空间中可用。

在许多使用 scipy 的包中,首选模式是导入子包,以便通过它们的名称使用它们,例如:

>>> from scipy import stats, optimize, interpolate


>>> import scipy
>>> scipy.stats
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'stats'
>>> scipy.optimize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'optimize'

>>> import scipy.stats
>>> scipy.optimize
<module 'scipy.optimize' from 'C:\Python26\lib\site-packages\scipy\optimize\__init__.pyc'>

【讨论】:

  • 也许会提到从 scipy 导入的明确指南:docs.scipy.org/doc/scipy/reference/api.html(这经常出现,在 SO 上有一个“规范”的答案确实很好。)
  • 谢谢,这解释了观察到的行为。使用 import.scipy.stats 解决了。
【解决方案2】:

这是意料之中的。当您只导入 scipy 时,大多数子包都不会导入。其中有很多,有很多繁重的扩展模块需要时间来加载。您应该始终显式导入要使用的子包。

https://github.com/scipy/scipy/issues/13618

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2013-04-07
    • 2023-02-01
    • 2013-05-22
    • 2018-07-07
    • 2019-01-25
    相关资源
    最近更新 更多