# Python Scipy Stats Norm [14 Amazing Examples]

In this Python tutorial, we will learn about the “Python Scipy Stats Norm” to calculate the different types of normal distribution and how to plot it and cover the following topics.

• What is Norm in Statistics
• Python Scipy Stats Norm Pdf
• Python Scipy Stats Norm Expect
• Python Scipy Stats Norm Plot
• Python Scipy Stats Norm Parameters
• Python Scipy Stats Norm Cdf
• Python Scipy Stats Norm Interval
• Python Scipy Stats Norm Ppf
• Python Scipy Stats Norm Gen
• Python Scipy Stats Norm Logpdf
• Python Scipy Stats Norm Logcdf
• Python Scipy Stats Norm Gennorm
• Python Scipy Stats Norm Rvs
• Python Scipy Stats Norm Fit

## What is Norm in Statistics?

Norms are statistical depictions of a population, such as the CBSE math scores of male sixth-graders or the IELTS reading scores of female Emma ninth-graders.

The test results of an individual are compared with the statistical representation of the population in a norm-referenced score interpretation. In real life, a representative sample or group is tested rather than the entire population. A norm for the group or set of norms is provided by this. Standards describe what a certain group should be able to perform, while norms indicate what that population can do.

Also, check: Python Scipy Mann Whitneyu

## Python Scipy Stats Norm Pdf

The `scipy.stats.norm` represents the random variable that is normally continuous. It has different kinds of functions for normal distribution like CDF, PDF, median, etc.

It has two important parameters `loc` for the mean and `scale` for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

``scipy.stats.norm.method_name(data,loc,size,moments,scale)``

Where parameters are:

• data: It is a set of points or values that represent evenly sampled data in the form of array data.
• loc: It is used to specify the mean, by default it is 0.
• moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
• scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object `scipy.stats.norm()`. The methods are given below.

• scipy.stats.norm.cdf(): It is used for the cumulative distribution function.
• scipy.stats.norm.pdf(): It is used for the probability density function.
• scipy.stats.norm.rvs(): To get the random variates.
• scipy.stats.norm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
• scipy.stats.norm.logpdf(): It is used to get the log related to the probability density function.
• scipy.stats.norm.logcdf(): It is used to find the log related to the cumulative distribution function.
• scipy.stats.norm.sf(): It is used to get the values of the survival function.
• scipy.stats.norm.isf(): It is used to get the values of the inverse survival function.
• scipy.stats.norm.logsf(): It is used to find the log related to the survival function.
• scipy.stats.norm.mean(): It is used to find the mean related to the normal distribution.
• scipy.stats.norm.medain(): It is used to find the median related to the normal distribution.
• scipy.stats.norm.var(): It is used to find the variance related to the distribution.
• scipy.stats.norm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

``````import numpy as np
import matplotlib.pyplot as plt
from scipy import stats``````

Create observation data values and calculate the `probability density function` from these data values with `mean = 0` and `standard deviation = 1`.

``````observatin_x = np.linspace(-4,4,200)
pdf_norm = stats.norm.pdf(observatin_x,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,pdf_norm)
plt.xlabel('x-values')
plt.ylabel('PDF_norm_values')
plt.title("Probability density funciton of normal distribution")
plt.show()``````

This is how to use the method `norm() `of python Scipy to compute the different distributions of the norm.

## Python Scipy Stats Norm Expect

The method `expect()` of Python Scioy that exist in a module `scipy.stats.rv_continous` uses numerical integration, to determine the expected value of a function about the distribution. According to a distribution dist, a function’s expected value, f(x), is defined as follows:

Here in this section. we will determine the expected value of a function about the norm distribution.

The syntax is given below.

``rv_continuous.expect(func=None, args=(), loc=0, scale=1, lb=None, ub=None, conditional=True)``

Where parameters are:

• func(callable): Calculating an integral for a function. only accepts one parameter. The identity mapping f(x) = x is the default.
• args(tuple): Distribution’s shape parameters.
• loc(float): It is the location parameter and by default 0.
• scale(float): It is a scale parameter and by default 1.
• lb,ub(scalar): Integration’s lower and upper bounds.
• conditional(boolean): If true is the case, the integral is rectified using the integration interval’s conditional probability. The function’s expected value, subject to the supplied interval, is the return value. False by default.

The method `expect()` returns `expect` of type float which is the expected value that was calculated.

Let’s understand with an example by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy.stats import norm
norm(1).expect(lambda a: 1, lb=0.0, ub=1.0)``````

The above is close to the following code.

``norm(1).cdf(1.0) - norm(1).cdf(0.0)``

If we specify conditional equal to `True`,

``norm(1).expect(lambda a: 1, lb=0.0, ub=1.0, conditional = True)``

Because of numerical integration, there is a tiny departure from 1.

This is how to determine the expected value of a function about the norm distribution.

## Python Scipy Stats Norm Plot

The method `norm()` has two parameters `loc` and `scale` that we can use to plot the distribution using the library matplotlib. These parameters are defined in the above subsection “Python Scipy Stats Norm”.

So plot the distribution by following the below steps:

Import the required libraries or methods using the below python code.

``````import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm``````

Generate data and define the loc and scale parameters using the below code.

``````x_data = np.linspace(0, 30, 200)

loc_pr = 12
scale_pr = 1.5``````

Compute the pdf of the norm and plot the distribution using the below code.

``````plt.plot(x_data, norm.pdf(x_data, loc=loc_pr, scale=scale_pr))
plt.show()``````

This is how to plot the normal distribution using the matplotlib library.

## Python Scipy Stats Norm Parameters

The Python Scipy method `norm()` has four main parameters `data`, `loc`, `moments` and `scale` that can be used to control the distribution.

Let’s understand with an example by following the below steps:

Import the required libraries or methods using the below python code.

``````import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm``````

Generate data and define the loc and scale parameters using the below code.

``````x_data = np.linspace(0, 20, 200)

loc_pr = 10
scale_pr = 1
plt.plot(x_data, norm.pdf(x_data, loc=loc_pr, scale=scale_pr))
plt.show()``````

Change the loc parameter to some value and keep constant the value of the scale parameter using the below code.

``````
loc_pr = 5
scale_pr = 1
plt.plot(x_data, norm.pdf(x_data, loc=loc_pr, scale=scale_pr))
plt.show()``````

When we change the log_pr to 5, it shifted the distribution towards the left side as we can see in the output.

Again, change the scale_pr to some value and keep constant the value of loc_pr using the below code.

``````loc_pr = 5
scale_pr = 3
plt.plot(x_data, norm.pdf(x_data, loc=loc_pr, scale=scale_pr))
plt.show()``````

When we change the scale_pr to 3, it changes the distribution shape as we can see in the output.

We have other parameters of the method `norm()` that we can use to get more control over the distribution.

This is how to use the parameters of the method `norm()` of Python Scipy.

## Python Scipy Stats Norm Cdf

The object `norm()` has a method `cdf()` that calculates the cumulative distribution of the norm.

The syntax is given below.

``scipy.stats.norm.cdf(x,loc,size,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

The above parameters are the standard parameter of all the methods in the object `scipy.stats.norm()`. The methods are given below.

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

``````import numpy as np
import matplotlib.pyplot as plt
from scipy import stats``````

Create observation data values and calculate the `cumulative distribution` from these data values with `mean = 0` and `standard deviation = 1`.

``````observatin_x = np.linspace(-2,2,200)
cdf_norm = stats.norm.cdf(observatin_x,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,cdf_norm)
plt.xlabel('x-values')
plt.ylabel('cdf_norm_values')
plt.title("Probability density funciton of normal distribution")
plt.show()``````

This is how to calculate the cumulative distribution of norm using the method `norm.cdf()` of Python Scipy.

## Python Scipy Stats Norm Interval

The method `norm.interval()` of Python Scipy computes the endpoints of the distribution’s fractional alpha range, between 0 and 1.

The syntax is given below.

``scipy.stats.interval(alpha, loc=0, scale=1)``

Where parameters are:

• alpha(float): It is the alpha value.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

Let’s take an example by following the below steps:

Import the required libraries or methods using the python code.

``from scipy.stats import norm``

Define the alpha value and compute the endpoints of the distribution using the below code.

``````alpha = 0.1
norm.interval(alpha)``````

This is how to compute the endpoints of the distribution’s fractional alpha range, between 0 and 1 using the method `nomr.interval()` of Python Scipy,

## Python Scipy Stats Norm Ppf

The object `norm()` has a method `ppf()` that calculate the Percent point function of the norm. In other words, The method `norm. ppf()` accepts a percentage and returns a standard deviation multiplier for the value that percentage occurs at.

The syntax is given below.

``scipy.stats.norm.ppf(q,loc,size,scale)``

Where parameters are:

• q: It is a percentage.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

Let’s understand with an example by following the below code.

``````from scipy.stats import norm
norm.ppf(0.99, loc=0, scale=1)``````

The above code gives a one-tail test result with a 99% confidence interval for a normal distribution.

This is how to compute a standard deviation multiplier for the value using the method `norm.ppf()` of Python Scipy.

## Python Scipy Stats Norm Logpdf

The object `norm()` has a method `logpdf()` that calculates the log probability of the norm.

The syntax is given below.

``scipy.stats.norm.logpdf(x,loc,size,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

The above parameters are the standard parameter of all the methods in the object `scipy.stats.norm()`. The methods are given below.

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

``````import numpy as np
import matplotlib.pyplot as plt
from scipy import stats``````

Create observation data values and calculate the log probability from these data values with `mean = 0` and `standard deviation = 1`.

``````observatin_x = np.linspace(-2,2,200)
logpdf_norm = stats.norm.logpdf(observatin_x,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,logpdf_norm)
plt.xlabel('x-values')
plt.ylabel('logpdf_norm_values')
plt.title("Log probability of normal distribution")
plt.show()``````

This is how to compute the log pdf of norm using the method `norm.logpdf()` of Python Scipy.

## Python Scipy Stats Norm Logcdf

The object `norm()` has a method `logcdf()` that calculates the log cumulative distribution of norm.

The syntax is given below.

``scipy.stats.norm.logcdf(x,loc,size,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• loc: It is used to specify the mean, by default it is 0.
• scale: It is used to determine the standard deviation, by default it is 1.

The above parameters are the standard parameter of all the methods in the object `scipy.stats.norm()`. The methods are given below.

Import the required libraries using the below code.

``````import numpy as np
import matplotlib.pyplot as plt
from scipy import stats``````

Create observation data values and calculate the log cumulative from these data values with `mean = 0` and `standard deviation = 1`.

``````observatin_x = np.linspace(-5,5,200)
logcdf_norm = stats.norm.logcdf(observatin_x,loc=0,scale=1)``````

Plot the created distribution using the below code.

``````plt.plot(observatin_x,logcdf_norm)
plt.xlabel('x-values')
plt.ylabel('logcdf_norm_values')
plt.title("Log cumulative distribution of normal distribution")
plt.show()``````

This is how to compute the log cdf of the norm using the method `norm.logcdf()` of Python Scipy.

## Python Scipy Stats Norm Gen

The `scipy.stats.genpareto` represents the generalized Pareto random variable that is continuous. It has different kinds of functions of normal distribution like CDF, PDF, median, etc.

The generalized Pareto distribution (GPD) is a class of continuous probability distributions used in statistics. It is frequently used to model another distribution’s tails.

It has two important parameters `loc` for the mean and `scale` for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

``scipy.stats.genpareto.method_name(x,c,loc,size,moments,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• c: It is used to specify the shape.
• loc: It is used to specify the mean, by default it is 0.
• moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
• scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object `scipy.stats.genpareto()`. The methods are given below.

• scipy.stats.genpareto.cdf(): It is used for the cumulative distribution function.
• scipy.stats.genpareto.pdf(): It is used for the probability density function.
• scipy.stats.genpareto.rvs(): To get the random variates.
• scipy.stats.genpareto.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
• scipy.stats.genpareto.logpdf(): It is used to get the log related to the probability density function.
• scipy.stats.genpareto.logcdf(): It is used to find the log related to the cumulative distribution function.
• scipy.stats.genpareto.sf(): It is used to get the values of the survival function.
• scipy.stats.genpareto.isf(): It is used to get the values of the inverse survival function.
• scipy.stats.genpareto.logsf(): It is used to find the log related to the survival function.
• scipy.stats.genpareto.mean(): It is used to find the mean related to the normal distribution.
• scipy.stats.genpareto.medain(): It is used to find the median related to the normal distribution.
• scipy.stats.genpareto.var(): It is used to find the variance related to the distribution.
• scipy.stats.genpareto.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

``````from scipy.stats import genpareto
import matplotlib.pyplot as plt
import numpy as np``````

Code creates a variable for the shape parameters and assigns some values.

``c = 0.2``

Create an array of data using the method `ppf()` of an object `genpareto` using the below code.

``````array_data = np.linspace(genpareto.ppf(0.01, c),
genpareto.ppf(0.90, c), 90)
array_data``````

Now plot the probability density function by accessing the method `pdf()` of an object `genpareto` of the module `scipy.stats` using the below code.

``````fig, ax = plt.subplots(1, 1)
ax.plot(array_data, genpareto.pdf(array_data, c),
'r-', lw=4, alpha=0.5, label='genpareto PDF')``````

This is how to use `genpareto()` of Python Scipy to model the distribution tails.

## Python Scipy Stats Norm Gennorm

The `scipy.stats.gennorm` represents the random variable that is generalized normal continuous. It has different kinds of functions of normal distribution like CDF, PDF, median, etc.

It has two important parameters `loc` for the mean and `scale` for standard deviation, as we know we control the shape and location of distribution using these parameters.

The syntax is given below.

``scipy.stats.gennorm.method_name(x,beta,loc,size,moments,scale)``

Where parameters are:

• x: It is a set of points or values that represent evenly sampled data in the form of array data.
• beta: It is used to specify the shape.
• loc: It is used to specify the mean, by default it is 0.
• moments: It is used to calculate statistics like standard deviation, kurtosis, and mean.
• scale: It is used to specify the standard deviation, by default it is 1.

The above parameters are the common parameter of all the methods in the object `scipy.stats.gennorm()`. The methods are given below.

• scipy.stats.gennorm.CDF(): It is used for the cumulative distribution function.
• scipy.stats.gennorm.PDF(): It is used for the probability density function.
• scipy.stats.gennorm.rvs(): To get the random variates.
• scipy.stats.gennorm.stats(): It is used to get the standard deviation, mean, kurtosis, and skew.
• scipy.stats.gennorm.logPDF(): It is used to get the log related to the probability density function.
• scipy.stats.gennorm.logCDF(): It is used to find the log related to the cumulative distribution function.
• scipy.stats.gennorm.sf(): It is used to get the values of the survival function.
• scipy.stats.gennorm.isf(): It is used to get the values of the inverse survival function.
• scipy.stats.gennorm.logsf(): It is used to find the log related to the survival function.
• scipy.stats.gennorm.mean(): It is used to find the mean related to the normal distribution.
• scipy.stats.gennorm.medain(): It is used to find the median related to the normal distribution.
• scipy.stats.gennorm.var(): It is used to find the variance related to the distribution.
• scipy.stats.gennorm.std(): It is used to find the standard deviation related to the distribution

Let’s take an example by using one of the methods mentioned above to know how to use the methods with parameters.

Import the required libraries using the below code.

``````from scipy.stats import gennorm
import matplotlib.pyplot as plt
import numpy as np``````

Code creates a variable for the shape parameters and assigns some values.

``beta = 1.4``

Create an array of data using the method `ppf()` of an object `gennorm` using the below code.

``````array_data = np.linspace(gennorm.ppf(0.01, a),
gennorm.ppf(0.90, a,b), 90)
array_data``````

Now plot the probability density function by accessing the method `PDF()` of an object `gennorm` of the module `scipy.stats` using the below code.

``````fig, ax = plt.subplots(1, 1)
ax.plot(array_data, gennorm.pdf(array_data, beta),
'r-', lw=4, alpha=0.5, label='gennorm PDF')``````

This is how to use the method `norm.gennorm()` of Python Scipy.

## Python Scipy Stats Norm Rvs

The method `rvs()` of Python Scipy of object `norm` is random variates that generate random numbers.

The syntax is given below

``scipy.stats.norm.rvs(loc=0, scale=1, size=1, random_state=None)``

Where parameters are:

• loc: It is a mean.
• scale: The distribution’s matrix of covariance.
• size(int): It is the sample size.
• random_state(int): If the seed is None, the NumPy.random method is utilized (or np.random). It uses a single instance of RandomState. If the seed is an integer, a new RandomState object is made using the seed. If the seed already has a Generator or RandomState instance, that instance is used.

Let’s draw a random sample from a multivariate normal distribution by following the below steps:

Import the required libraries using the below python code.

``from scipy import stats``

Create a multivariate normal distribution using the below code.

``norm_dist = stats.norm()``

Generate random numbers using normal distribution using the below code.

``````samp_size = 100000
norm_dist.rvs(samp_size)``````

This is how to generate the random numbers using the method `norm.rvs()` of python Scipy.

## Python Scipy Stats Norm Fit

The method `fit()` of Python Scipy of object `norm` that provides approximations for scale and location.

The syntax is given below.

``scipy.stats.norm.fit(data)``

Where parameter data is the data for which we need the location and scale.

Let’s understand with an example by following steps:

Import the required libraries or methods using the below code.

``from scipy.stats import norm``

Generate random numbers using the method `norm.rvs()`.

``x_data = norm.rvs(1., 2., size=500, random_state=123)``

Now fit the above data using the below code.

``loc_, scale_ = norm.fit(x_data)``

Check the estimated parameter values using the below code.

``````print("loc is ",loc_)
print("Scale is ",scale_)``````

Also, take a look at some more Python SciPy tutorials.

So, in this tutorial, we have learned about the “Python Scipy Stats Norm” and covered the following topics.

• What is Norm in Statistics
• Python Scipy Stats Norm Pdf
• Python Scipy Stats Norm Expect
• Python Scipy Stats Norm Plot
• Python Scipy Stats Norm Parameters
• Python Scipy Stats Norm Cdf
• Python Scipy Stats Norm Interval
• Python Scipy Stats Norm Ppf
• Python Scipy Stats Norm Gen
• Python Scipy Stats Norm Logpdf
• Python Scipy Stats Norm Logcdf
• Python Scipy Stats Norm Gennorm
• Python Scipy Stats Norm Rvs
• Python Scipy Stats Norm Fit