First look at the great Gaussian distribution (Gaussian distribution) probability density function (probability density function):
F (x) =12π−−√σexp (− (x−μ) 22σ2) f (x) =\frac1{\sqrt{2\pi}\sigma}\exp (-\frac{(X-\MU) ^2}{2\sigma^2})
Corresponds to the NumPy:
Numpy.random.normal (loc=0.0, scale=1.0, Size=none)
The meaning of the parameter is:
Loc:float the
mean value of this probability distribution (corresponding to the central centre of the whole distribution)
scale:float the
standard deviation of this probability distribution (corresponding to the width of the distribution, scale the larger the more Humpty Dumpty, the smaller the scale, the more lanky)
Size:int or tuple of ints
output of shape, default to None, output only one value
The NP.RANDOM.RANDN (size) that we use more often is called the standard normal distribution (Μ=0,σ=1 \mu=0,\,\sigma=1), which corresponds to Np.random.normal (loc=0, scale=1, size). sampling (sampling)
# from a distribution (identified by mean and standard deviation), we get the sample
mu, sigma = 0,. 1
s = np.random.normal (Loc=mu, Scale=sigma, size=1000)
You can also use the relevant APIs in the SCIPY library (the classes and functions here are more in line with the intuition in mathematical statistics):
Import scipy.stats as St
mu, sigma = 0,. 1
s = St.norm (Mu, sigma). RVs (1000)
verify mean and variance:
>>> Abs (Mu < Np.mean (s)) <.
True
>>> ABS (SIGMA-NP.STD (S, ddof=1)) <
true< c6/># Ddof,delta degrees of freedom, representing degrees of freedom
# generally takes 1, representing unbiased estimates,
Fitting
Let's see how to fit a Gaussian distribution using Matplotlib.pyplot's handy and powerful syntax:
Import Matplotlib.pyplot as Plt
count, bins, _ = Plt.hist (S, normed=true)
# normed is the key to fitting
# count counts a The number of times bin appears, and may have a slightly different value when normed is true
plt.plot (bins, 1./(np.sqrt (2*NP.PI) *sigma) *np.exp (-(BINS-MU) **2/(2*sigma* *2), lw=2, c= ' R ')
Plt.show ()
Or:
S_fit = Np.linspace (S.min (), S.max ())
Plt.plot (S_fit, St.norm (Mu, sigma). PDF (S_fit), lw=2, c= ' R ')