NumPy matlib.randn() Function:
The matlib.randn() function of the Numpy module returns a matrix filled with random floats sampled from a univariate normal (Gaussian) distribution with mean 0 and variance 1.
Syntax:
numpy.matlib.randn(*args)
Parameters
*args: This is required. It is the shape of the output. Each integer specifies the size of one dimension If given as N integers. If given as a tuple, this tuple gives the complete/entire shape.
Return Value:
A matrix of random values taken from a standard normal distribution of shape specified by *args is returned.
- Python NumPy random.random() Function
- Python NumPy matlib.rand() Function
- Python NumPy power() Function
NumPy matlib.randn() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword
- Pass the shape of the matrix(as a tuple), as an argument to the matlib.randn() function of numpy module to create a matrix of the given shape with random values from the standard normal distribution, N(0, 1) i.e, with mean 0 and variance 1.
- Store it in a variable.
- Print the above-obtained matrix with random values of the given shape.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Import matlib function of numpy module using the import keyword import numpy.matlib # Pass the shape of the matrix(as a tuple), as an argument to the matlib.randn() # function of numpy module to create a matrix of the given shape with random values # from the standard normal distribution, N(0, 1) i.e, with mean 0 and variance 1. # Store it in a variable. gvn_matrx = np.matlib.randn((2,2)) # Print the above obtained matrix with random values of the given shape print("The above obtained matrix with random values of the given shape:") print(gvn_matrx)
Output:
The above obtained matrix with random values of the given shape: [[ 0.83740804 -1.54515964] [ 3.44807217 -0.49712937]]
Example2: Normal Distribution
The method below can be used to build a matrix containing random values from a normal distribution, N(μ, σ2).
σ * np.matlib.randn() + μ
where μ = mean
σ = variance
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword.
- Apply the above formula by passing mean and variance as random values and shape as argument(as a tuple) to the randn() function of the numpy module(here mean is 3 and variance is 4).
- Print the above matrix with random values of normal distribution for the given shape.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Import matlib function of numpy module using the import keyword import numpy.matlib # Apply the above formula by passing mean and variance as random values and shape as argument(as a tuple) to the randn() function of the numpy module. # here mean is 3 and variance is 4 # Store it in a variable. gvn_matrx = 4*np.matlib.randn((4,4)) + 3 # Print the above matrix with random values of normal distribution for the given shape print("The above matrix with random values of normal distribution for the given shape:") print(gvn_matrx)
Output:
The above matrix with random values of normal distribution for the given shape: [[ 0.90043018 4.70289111 -0.65385717 2.37142165] [-0.01856318 0.37620644 -2.68371203 7.39011586] [-0.64833481 1.48203811 8.30356936 5.16294525] [ 0.96125199 4.84063464 2.54766651 2.06913251]]