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:
|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.|
|size||This 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:
The following procedure is implemented to obtain a random normal distribution with a dimension of 5×2:
The mean and standard deviation of the normal distribution are 4 and 6, respectively:
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:
Visualization The Normal Distribution
Now that we have generated some random samples from a normal distribution, let’s visualize the distribution using a histogram.