【问题标题】:Python: ModuleNotFoundError: No module named 'stats'Python:ModuleNotFoundError:没有名为“stats”的模块
【发布时间】:2020-07-16 08:04:52
【问题描述】:

我正在尝试运行需要 SciPy 才能使用 stats 模块的脚本。当我尝试运行此脚本时,我得到以下信息:

$python ./myScript.py 100 someFile.json
Traceback (most recent call last):
  File "./myScript.py", line 12, in <module>
    from sampler import *
  File "C:\myProject\python\lib\anotherScript.py", line 28, in <module>
    from stats import Histogram
ModuleNotFoundError: No module named 'stats'

我真的是 Python 方面的新手,所以我只是想运行这个从我克隆的项目中获得的示例代码,所以它应该可以工作...... 我在 Windows 10 环境中,我有这个 Python 版本:

$ python --version
Python 3.8.4

我在某处看到我需要先安装 Numpy,所以我使用 this pip command here 安装了它,然后我安装了 SciPy following their official instructions,并且都安装成功了。

我尝试了 Stack Overflow 上其他答案中提供的答案(例如强制重新安装 Numpy 和 SciPy),但未成功。我还做了以下测试:

$ python
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>> scipy.version.full_version
'1.5.1'
>>>
>>> from scipy import stats
>>> from stats import Histogram
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'stats'
>>>
>>> from stats import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'stats'
>>>

接下来我能做什么?

【问题讨论】:

  • from scipy import stats 已经加载了统计模块。您需要做的就是参考stats.Histogram

标签: python python-3.x windows numpy scipy


【解决方案1】:

scipy.stats.histogram 在最新版本中已被弃用。 您可以简单地将其导入为:

from scipy import stats
stats.rv_histogram()

from scipy.stats import rv_histogram

这是 scipy 文档中的示例

from scipy import stats
import numpy as np
data = stats.norm.rvs(size=100000, loc=0, scale=1.5, random_state=123)
hist = np.histogram(data, bins=100)
hist_dist = stats.rv_histogram(hist)

结果:

hist_dist.pdf(1.0)
0.20538577847618705
hist_dist.cdf(2.0)
0.90818568543056499

您可以在此处找到文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_histogram.html

【讨论】:

  • 这就是我所需要的。谢谢你的帮助。问候。
猜你喜欢
  • 2020-12-28
  • 2021-11-07
  • 2022-06-14
  • 2018-09-06
  • 1970-01-01
  • 2021-11-26
  • 2017-07-16
  • 2018-04-23
  • 1970-01-01
相关资源
最近更新 更多