How to calculate the mean and standard deviation of a sample in python ?


From a sample of data stored in an array, a solution to calculate the mean and standrad deviation in python is to use numpy with the functions numpy.mean and numpy.std respectively. For testing, let generate random numbers from a normal distribution with a true mean (mu = 10) and standard deviation (sigma = 2.0:)

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> mu = 10.0
>>> sigma = 2.0
>>> x = np.random.randn(10000) * sigma + mu

if we now use np.mean(x) and np.std(x) to estimate the mean and standard deviation:

>>> print('mean: 'np.mean(x))
>>> print('standard deviation', np.std(x))

returns for example:

10.003818651607594
1.9969664232497317

Create random numbers from a standard normal distribution with numpy in python
Create random numbers from a standard normal distribution with numpy in python

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100000)

hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")

plt.ylim(0.0,max(hx)+0.05)
plt.title('Generate random numbers \n from a standard normal distribution with python')
plt.grid()

plt.savefig("numpy_random_numbers_stantard_normal_distribution.png", bbox_inches='tight')
plt.show()

References

Links Site
numpy.mean docs.scipy.org
numpy.std docs.scipy.org
Image

of