Skip to main content

Normal (Gaussian) Distribution with Python


In this tutorial you will learn:

  • What is a Gaussian Distribution?
  • Gaussian Distribution Implementation in python

Gaussian Distribution

Gaussian Distribution also known as normal distribution is a probability distribution that is symmetric about the mean and it depicts that that the frequency of values near the mean is greater as compared to the values away from the mean. Gaussian distributions are symmetrical while all symmetrical distributions are not Gaussian distributions. Gaussian distribution is characterized by the value of mean equal to zero while the value of standard deviation is one. The graphical pattern of a gaussian distribution always appears as a bell curve.

Gaussian Distribution in Python

Gaussian distribution in python is implemented using normal() function. The normal() function is included in the random module. It takes in the “size” of the distribution which we want as an output as a first and mandatory parameter. It takes “loc” as a second parameter, the location determines the point of the peak. It take “scale” as a third parameter, the scale determines the how flat the graph distribution would be (also known as standard deviation).
In this example we will generate a 2D array of normal distribution having size (2,4), in the first line of code we are importing random module from numpy library. In the third line of the code we are calling the normal() function with only mandatory parameter “size”.

  1. from numpy import random
  2. #here we are using normal function to generate gaussian distribution of size 2 x 4
  3. res = random.normal(size=(2,4))
  4. print('2D Gaussian Distribution as output from normal() function:\n')
  5. #printing the result
  6. print(res)

Lets take another example, in this example we will generate a 3D array of normal distribution of the size(2,4,5).
  1. from numpy import random
  2. #here we are using normal function to generate gaussian distribution of size 2 x 3 x 5
  3. res = random.normal(size=(2,3,5))
  4. print('3D Gaussian Distribution as output from normal() function:\n')
  5. #printing the result
  6. print(res)

In this example while calling the normal() function, we will also declare the standard deviation and location of the mean as 3 and 4 respectively. Here you can observe that I have given two additional parameters “size” and “loc” to normal() function.
  1. from numpy import random
  2. #here we are using normal function to generate gaussian distribution of size 3 x 4
  3. res = random.normal(size=(3,4), loc= 3, scale = 4)
  4. print('2D Gaussian Distribution as output from normal() function:\n')
  5. #printing the result
  6. print(res)

This is an important example, in this example we will visualize the array being generated by the normal() function with output array size 1000, standard deviation 4 and loc of mean 3. Here we will use the distplot() as used in last tutorial.
  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using normal function to generate gaussian distribution of size 1000
  6. sb.distplot(random.normal(size=1000, loc = 3, scale = 4), hist=True)
  7. #plotting the graph
  8. mpl.show()

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.