Python: ModuleNotFoundError: No module named 'stats'

Viewed 2349

I'm trying to run a script that requires SciPy to be able to use the stats module. 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 --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.

I tried the answers provided in other answers at Stack Overflow (like force reinstall Numpy and SciPy) unsuccessfully. 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?

1 Answers

scipy.stats.histogram has been deprecated in the latest releases. 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

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

Related