A NumPy random normal() function is a built-in function in NumPy package of python. The NumPy random normal() function generate random samples from a normal distribution or Gaussian distribution, the normal distribution describes a common occurring distribution of samples influenced by a large of tiny, random distributions or which occurs often in nature.
The NumPy functions are designed to be very fast and memory efficient which makes them perfect for working with huge datasets.
Table of Contents
Syntax
NumPy random normal() function is used to get the NumPy arrays from a NumPy np.random.normal(mean, std, size) object which mean that it will return an array of samples with values taken from a standard normally distributed (Gaussian) distribution given by the parameters:
– ‘mean’ – The NumPy array or scalar representing the arithmetic mean of the distribution;
– ‘std’ – The NumPy array or scalar representing the standard deviation of each element in x; and
– ‘size’: A tuple specifying how many NumPys elements compose one sample.
Basic Syntax
numpy.random.normal(loc = 0.0, scale = 1.0, size = None)
Parameters
The NumPy module provides a function for generating random samples from a normal distribution. The parameters of the function are loc, scale and size. Loc specifies the mean (peak) of distribution, scale specifies the standard deviation or how flat the distribution graph should be, and size specifies the shape of resultant array.
- loc – This is an optional parameter with default value of 0.0.
- scale – This is an optional parameter with default value of 1.0.
- size – This is an optional parameter with default value of 1.
Return value – The return value of this function is the NumPy array of random samples from a normal distribution.
How the NumPy Random Normal() Function Works?
The NumPy random normal() function is one of the most popular and widely used functions in Python. It can be described as a mathematical tool that generates a single sample number or an array of dimension specified in size, loc, and scale from the normal distribution.
The NumPy random normal() function accepts three parameters (loc, scale, size) and all three parameters are not mandatory.
If we pass nothing to the normal() function it will return a single sample number; if we pass specific values for loc, scale and size then the NumPy random normal()function generates a random sample of numbers with specified dimensional specification from the normaldistribution and returns as an array of dimension specified in size.
Let’s have a look at an example.
Example Code
from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
x = random.normal(loc=1, scale=2, size=(2, 3))
print(x)
sns.distplot(x, hist=True)
plt.show()
Output


Conclusion
The NumPy random normal() function is a built-in function in the NumPy package, which is used to gets the random samples of a specified dimensional array from the normal distribution.
This means that if you have an nxm dimensional matrix A and want to get some observations drawn randomly without replacement, then x = np.random.normal(A) will give you those values with probability density proportional to exp(-1/2n).
It has many other uses as well so be sure to check out this blog post for more information on how it can help your business!