The average of a distribution can be calculated by using the mean function within the numpy package. The way to extract the mean of the initialized distribution is as follows:
- Import the relevant packages:
import numpy as np
- Calculate the average of the earlier initialized distribution:
np.mean(x,axis=0)
In the preceding line of code, we have calculated the mean of all the values within x. The output of the preceding line of code is as follows:
3.0372443687293549
The output is close to what we expected, as we have initialized the distribution with a mean of three.
The output does not match three exactly, as we have taken a smaller sample size. Let us calculate the mean of a normal distribution in a similar way, but this time with a larger sample size:
from scipy import stats
x = stats.norm.rvs(loc=3, scale=3, size=(1000000))
np.mean(x,axis=0)
The output is 2.9963541802866853, which is a value that is very close to three—the expected mean value.
- Calculate the standard deviation of the earlier initialized distribution using the std function:
np.std(x)
The output is 3.002600053097471, which is very close to the standard deviation of the distribution that we expected.