The numpy.random.randn() function in Python is a way to create random arrays that are shaped as specified and have values that are uniformly distributed according to the standard normal distribution. This blog post will show you how this process works with examples.
numpy.random.randn() in Python The numpy.random.randint(low, high) function creates an array of specified shape and fills it with random values as per standard normal distribution if we use the keyword argument ‘normalize=True’.
The numpy.random.randn() function is a handy tool for generating random arrays in Python. If positive arguments are provided, randn generates an array of shape (d0, d1, …, dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.
Table of Contents
numpy.random.randn(d0, d1, …, dn)
– d0, d1, …, dn: [int] Dimension of the returned array we require
– If no argument is given a single Python float is returned
Return: Array of defined shape filled with random floating point samples from the standard normal distribution.
Let’s create arrays of different dimensions with random values.
Example 01: Creating One-Dimensional Array
In Python, the numpy.random.randn() method is used to create a 1D array from a single dimension. The randn() function takes only one parameter and will return the resulting one-dimensional array.
import numpy as np array = np.random.randn(3) print("1D Array: ", array)
Example 02: Creating Two-Dimensional Array
In Python, a two-dimensional array (2D) can be created using the np.random.randn() method and passing in two parameters as dimensions. This method returns the 2D array with those dimensions filled with random numbers generated by rand().
import numpy as np array = np.random.randn(3,3) print("2D Array: ", array)
Example 03: Creating Three-Dimensional Array
In Python, a three-dimensional array (3D) can be created using the np.random.randn() method and passing in three parameters as dimensions. This method returns the 3D array with those dimensions filled with random numbers generated by rand().
import numpy as np array = np.random.randn(3,3,3) print("2D Array: ", array)
In this tutorial, we have seen how we can use numpy random.randn() method to create a 1D array, 2D array, 3D array. The randn() function is very useful for generating data with uniform distribution in all dimensions of an input matrix.