Fitting from Np.random.normal () to normal distribution

Source: Internet
Author: User
Tags abs

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.