NumPy random.normal() function in Python is used to create an array of specified shape and fills it with random values from a normal (Gaussian) distribution. This distribution is also known as Bell Curve because of its characteristic shape.
This function takes an int or tuple of int, to specify the size of an array, similar to other NumPy functions like the numpy.ones() and numpy.zeros(). In this article, I will explain numpy.random.normal() function syntax and using its parameters how to get random values of the single and multi-dimensional arrays with examples.
1. Quick Examples of NumPy random.normal() function
Following are quick examples of random.normal() function. For more functions and examples of NumPy refer NumPy Tutorial.
# Below are the quick examples
# Example 1: Get the random samples of normal distribution
arr = np.random.normal(size = 5)
# Example 2: Get the random samples of normal distribution
arr = np.random.normal(size = (3,2 ))
# Example 3: Get the mean value of random values
arr = np.random.normal(loc = 2)
# Example 4: Get the standard deviation of random values
arr = np.random.normal(scale = 4.0)
# Example 5: Get the random values
arr = np.random.normal( loc = 2, scale = 4.0, size = 5)
2. Syntax of NumPy random.normal()
Following is the Syntax of np.random.normal().
# Syntax of NumPy random.normal()
numpy.random.normal(loc = 0.0, scale = 1.0, size = None)
2.1 Parameters of random.normal()
Following are the parameters of normal() function.
loc
: It is optional. Like an array of a float value. It specifies the mean of the distribution. By default, it is set to 0.0.scale
: It is optional. Like an array of a float value. It specifies the standard deviation. By default, it is set to 1.0. It must not be a negative value.size
: It is optional and is an integer or a tuple of integers. It specifies the shape of the returned array. If the size is None. By default, it is set to value 1.
2.2 Return Value of random.normal()
It returns an array of random values from a normal distribution along with the specified shape.
3. Usage of NumPy random.normal()
random.normal()
function is the built-in function in the NumPy module package of python. This function generates random samples from a normal distribution/Gaussian distribution.
When we pass the int or tuple of int as a parameter into this function, it will return the array of specified size or multi-dimensional array of random samples of normal distribution. Let’s take the example,
import numpy as np
# Get the random samples of normal distribution
arr = np.random.normal(size = 5)
print(arr)
# Output :
# [-0.53008761 0.50853827 -0.26577348 -0.35692072 0.37717259]
# Get the random samples of normal distribution
arr = np.random.normal(size = (3,2 ))
print(arr)
# Output:
# [[-1.52064919 0.40079434]
# [-0.86957264 1.17437412]
# [-0.41216874 -1.56245058]]
4. Get the NumPy Random Values along Loc
When we pass loc
parameter into this function, it will return the mean of the array’s random values from a normal distribution. When not used, by default it uses a 1.0 value.
# Get the mean of random values
arr = np.random.normal(loc = 2)
print(arr)
# Output :
# 3.294342794253831
5. Get the NumPy Random Values along Scale
When we pass the scale
parameter into this function, it will return the standard deviation of the array’s random values from the normal distribution. When not used, by default it uses a 0.0 value.
# Get the standard deviation of random values
arr = np.random.normal(scale = 4.0)
print(arr)
# Output:
# 5.781979817085629
6. Get the Random Values along Loc, Scale, & Size
By using the size
parameter along with loc
and scale
into this function, it will return the array of the specified size of random samples from a normal distribution. Let’s take the example,
# Get the random values
arr = np.random.normal( loc = 2, scale = 4.0, size = 5)
print(arr)
# Output:
# [ 0.63610699 -1.31665607 0.85681477 -0.08999122 3.95373571]
7. Graphical representation of NumPy random.normal() function
To make a histogram with the help pyplot
library and print the graph of NumPy random.normal() function.
# Import pyplot library
from matplotlib import pyplot as plt
output = np.random.normal( 1, 2.0, size = 5)
print(output)
count, bins, ignored = plt.hist( output, 50)
plt.show()
# Output:
# [ 3.19098457 -1.50999446 2.53421593 4.44277223 4.1947252 ]

8. Conclusion
In this article, I have explained NumPy random.normal()
and using this how to get the random values of 1-D NumPy array and multidimensional array, where the elements are from a normal distribution.
Related Articles
- Python NumPy array copy
- Python NumPy Concatenate() Function
- How to create an array using ones() function?
- Get the shape of an array
- How to create an array using the zeros() function?
- How to Use NumPy random.uniform() in Python?
- How to Use NumPy random.randint() in Python
- How to Use Numpy random.rand() in Python
- How to Use NumPy random.randn() in Python?