How to Use NumPy random.randn() in Python?

Spread the love

NumPy random.randn() function in Python is used to return random values from the normal distribution in a specified shape. This function creates an array of the given shape and it fills with random samples from the normal standard distribution. This function takes a single integer or sequence of integers to specify the size of an array similar to other NumPy functions like the numpy.ones() and numpy.zeros() functions. For more NumPy examples refer to NumPy Tutorial.

In this article, I will explain how to use the NumPy random.randn() function syntax and how it can be created an array based on a given shape with examples.

2.1 Syntax of NumPy random.randn()

Following is the syntax of the numpy.random.randn() function.


# Syntax of NumPy random.randn()
random.rand(d0, d1, ..., dn) 

2.1 Parameters of NumPy random.randn()

Following are the parameters of random.randn() function.

  • d0, d1, …, dn – The dimension of the returned array and it must be int type. If no argument is specified a single Python float is returned.

2.2 Return Value

It returns a random array of specified shapes, filled with random values of float type from the standard normal distribution.

3. Usage of NumPy random.randn()

The random.randn() is a numpy library function that returns an array of random samples from the standard normal distribution. It allows specified dimensions as an argument and returns an array of specified dimensions. If we don’t provide any argument, it will return the float value. The np.random.randn() function returns all the samples in float form, which are from the univariate “normal” (Gaussian) distribution of mean 0 and variance 1.

Note : The dimensions of the returned array must be non-negative. If you provide a negative argument, then it will return an error.

This function returns the random number without passing any parameter. You might get different random numbers when you run the same code multiple times. Let’s take an example,


import numpy as np
# Use random.randn() function
arr = np.random.randn()
print(arr)

# Output
# 0.2990869209563859

4. Get 1-D NumPy Array of Random Values

To pass the integer as a shape of the array into random.randn() function, let’s create a one-dimensional NumPy array of random values. This function will return an array of a given dimension. For example,


# Get 1-dimensional array of random values
arr = np.random.randn(6)
print(arr)

# Output
# [-0.46114837 -0.26834527  1.02120092 -2.18643677  0.75038041  1.40443056]

5. Get 2-D NumPy Array of Random Values

While constructing two-dimensional random arrays, pass the shape(sequence of ints) of the array to this function. It returns the two-dimensional array of the specified shape of random values.


# Get 2-Dimensional array of random values
arr = np.random.randn(2,5)
print(arr)

# Output
# [[ 0.93183545  0.36577443 -0.58049491 -0.5598316  -0.8680673 ]
# [ 0.69076295 -0.56112501  0.69368353  0.22545393  0.49982591]]

6. Get 3-D NumPy Array of Random Values

This function can be generated a three-dimensional random array with a specified shape. Similarly, you can also generate any random samples of arrays of any size using the numpy.random.randn() function.


# Generate 3-dimensional array of random values
arr = np.random.randn(5,2,4)
print(arr)

Yields below output.


[[[-1.59898444e+00 -6.81374812e-01  9.72735988e-01  5.61476894e-01]
  [ 1.16647104e+00 -1.09262603e+00 -1.07282492e+00 -8.59539220e-01]]

 [[-8.71788863e-01  5.88431649e-01 -7.52198987e-01  1.00950913e+00]
  [-1.52871699e-01  8.20791995e-01 -1.36908725e-01  2.47184843e-01]]

 [[ 3.08811252e-01  6.31850653e-01  3.97157461e-01 -1.21179245e-01]
  [ 5.24723452e-01  1.28409089e+00 -8.64642446e-01 -8.75128023e-01]]

 [[-5.74942850e-01 -3.48680480e-01 -1.97713507e+00  4.01081047e-01]
  [-1.80487363e-03 -3.45531534e-01 -5.55700399e-01  8.27350472e-01]]]

7. Specify Negative Integer as Parameter

As I mentioned above, If we provide a negative integer as a parameter to specify the size of an array, it will give the ValueError.


# Use random.randn() function
arr = np.random.randn(-2)
print(arr)

# Output :
# ValueError: negative dimensions are not allowed

8. Conclusion

In this article, I have explained NumPy random.randn() function and using this how to get the random sample or random samples of single and multi-dimensional arrays based on specified dimensions with examples.

Happy Learning!!

References

Leave a Reply

You are currently viewing How to Use NumPy random.randn() in Python?