简体   繁体   English

Python:ModuleNotFoundError:没有名为“stats”的模块

[英]Python: ModuleNotFoundError: No module named 'stats'

I'm trying to run a script that requires SciPy to be able to use the stats module.我正在尝试运行一个需要SciPy才能使用统计模块的脚本。 When I try to run this script I got the following:当我尝试运行此脚本时,我得到以下信息:

$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'

I'm really a newbie in Python stuff so I'm just trying to run this sample code I got from a project I cloned, so it should work... I'm in a Windows 10 environment, I have this Python version:我真的是 Python 东西的新手,所以我只是想运行我从克隆的项目中获得的这个示例代码,所以它应该可以工作......我在 Windows 10 环境中,我有这个 ZA7F5F35426B9387271B 版本:

$ python --version
Python 3.8.4

I read somewhere I needed to install Numpy first so I did it using this pip command here and then I installed SciPy following their official instructions and both installed successfully.我在某处读到我需要先安装Numpy所以我在这里使用这个 pip 命令完成了它,然后我按照他们的官方说明安装了SciPy并且都安装成功。

I tried the answers provided in other answers at Stack Overflow (like force reinstall Numpy and SciPy) unsuccessfully.我尝试了 Stack Overflow 上其他答案中提供的答案(例如强制重新安装 Numpy 和 SciPy),但未成功。 I also did the following test:我还做了以下测试:

$ 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'
>>>

What can I do next?接下来我能做什么?

scipy.stats.histogram has been deprecated in the latest releases. scipy.stats.histogram在最新版本中已被弃用。 You can simply import it as:您可以简单地将其导入为:

from scipy import stats
stats.rv_histogram()

or或者

from scipy.stats import rv_histogram

Here is the example from scipy documentation这是 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)

Result:结果:

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

You can find the documentation here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_histogram.html您可以在此处找到文档: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_histogram.ZFC35FDC70D5FC69D269EZ83A822C7A53

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM