Numpy Random Normal Quick Guide

The NumPy random normal function is a commonly used function that generates samples based on a normal (Gaussian) distribution.

In this article, we will explore the numpy.random.normal() function in detail, its various parameters and how to manipulate those distributions with examples.



What is a Normal Distribution?

Before we dive into the numpy.random.normal() function, let’s briefly review what a normal distribution is.

After the German mathematician Carl Friedrich Gauss, it is also known as the Gaussian Distribution.

A normal distribution is a continuous probability distribution that is symmetric and bell-shaped.

It is defined by two parameters: the mean (μ) and the standard deviation (σ).

The mean determines the center of the distribution, while the standard deviation determines the spread of the distribution.


Create Random Sample Through Normal Distribution

Now that we know what a normal distribution is, let’s explore how to generate random samples from it using the numpy.random.normal() function.

Several probability distributions can be represented with this model, e.g. IQ Scores, Heartbeat etc.

To get a Normal Data Distribution, call the function random.normal().

Here is the basic syntax of the function:

numpy.random.normal(loc=0.0, scale=1.0, size=None)

There are three parameters that make up this function:

ParametersOverview
loc(Mean) the exact location of the highest point in the bell.
scale(the Standard Deviation of the graph distribution) the degree to which the graph distribution tends to be uniform throughout.
sizeThis parameter specifies the shape of the array that will be generated.

Let’s create a random normal distribution of dimension 4×4 by the following method:

Numpy random normal Example: 

from numpy import random mrx = random.normal(size=(4, 4)) print(mrx)

The following procedure is implemented to obtain a random normal distribution with a dimension of 5×2:

Example: 

from numpy import random mrx = random.normal(size=(5, 2)) print(mrx)

The mean and standard deviation of the normal distribution are 4 and 6, respectively:

Example: 

from numpy import random mrx = random.normal(size=(4, 4)) print(mrx)

Example Explanation

In above example first we have import the numpy.random module, which is used to generate random numbers in Python.

Then, it creates a 4×4 matrix mrx of random numbers drawn from a standard normal distribution (mean = 0, standard deviation = 1) using the numpy.random.normal() function.

The size parameter is set to (4,4), which means that the resulting matrix will have 4 rows and 4 columns.

The values in mrx are random, but they follow a bell-shaped curve centered around 0 with a standard deviation of 1, which is a characteristic of a standard normal distribution.

Finally, the print() function is used to display the matrix mrx in the output console.

We can customize the mean and standard deviation of the normal distribution by setting the loc and scale parameters, respectively.

let’s generate some random normal distribution with a mean of 2 and a standard deviation of 3:

Example: 

from numpy import random mrx = random.normal(loc=2, scale=3, size=(5, 2)) print(mrx)

Visualization The Normal Distribution

Now that we have generated some random samples from a normal distribution, let’s visualize the distribution using a histogram.

We can use the matplotlib and seaborn library to create a histogram of the data:

Example: 

from numpy import random import matplotlib.pyplot as pt import seaborn as sbn sbn.distplot(random.normal(size=500), hist=False) pt.show()

Example: 

from numpy import random import matplotlib.pyplot as pt import seaborn as sbn sbn.distplot(random.normal(size=850), hist=True) pt.show()
Reminder: Due to Numpy random normal’s bell-shaped curve, the graph of a Normal Distribution is also referred to as the Bell Curve.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *