Numpy random.randn() Function



The Numpy random.randn() function used to generate a numpy array of specified shape and fills it with random values as per standard normal distribution.

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. A single float randomly sampled from the distribution is returned if no argument is provided.

Syntax

Following is the syntax of the Numpy random.randn() function −

numpy.random.randn(d0, d1, ..., dn)

Parameters

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

  • d0, d1, ..., dn - These are the dimensions of the output array.

Return Values

This function returns a NumPy array filled with random floats following a standard normal distribution.

Example

Following is a basic example to generate a single random float using Numpy random.randn() function −

import numpy as np
random_value = np.random.randn()
print("Random Value -", random_value)
print(type(random_value))

Output

Following is the output of the above code −

Random Value - 0.3946743245523318
<class 'float'>

Example : Numpy array using 'random.randn()'

The numpy.random.randn() function can also be used to generate a 1D NumPy array of random values from a standard normal distribution.

In the following example, a 1D array of 4 elements is generated using numpy.random.randn() function −

import numpy as np
numpy_array = np.random.randn(4)
print("1D Array of Random Values - \n", numpy_array)
print(type(numpy_array))

Output

Following is the output of the above code:

1D Array of Random Values - 
 [ 1.30524442 -0.74706264  0.53150742 -1.29810795]
<class 'numpy.ndarray'>

Example : Multi-dimensional Numpy Array

Using the numpy.random.randn() function, we can generate n-dimensional arrays of random values by specifying the desired dimensions as parameters.

In this example, a 3D array with dimensions 3x4x3 is been generated, filled with random values from the standard normal distribution −

import numpy as np
numpy_3d_array = np.random.randn(3, 4, 3)
print("3D Array of Random Values -\n", numpy_3d_array)

Output

Following is the output of the above code −

3D Array of Random Values -
 [[[ 0.62852014  1.41807874  0.83575845]
  [ 1.00913888 -0.62230585  0.67617611]
  [-0.51083778  0.52039257 -0.84454698]
  [ 1.22446679 -0.13410954 -0.33386194]]

 [[ 0.41440124  1.00212288  0.39328479]
  [ 0.51793246  1.03868843  1.23749478]
  [-2.23742862  0.0430593  -0.60495951]
  [ 0.0221033   0.75218868 -0.02696248]]

 [[ 0.4918343   0.72076167 -1.40222382]
  [ 2.43693037 -0.83250305  0.26874105]
  [-0.54347424  2.25655853  0.45657885]
  [-1.01706963  0.6131971   0.45100215]]]

Example : Passing Negative Arguments

When we pass a negative argument to the numpy.random.randn() function, it raises a ValueError.

In the following example, we have passed a -6(negative) number to the numpy.random.randn() function −

#importing numpy array
import numpy as np
#passing a negative integers
my_Array = np.random.randn(-6)
print("Numpy Array",my_Array)

Output

Following is the output of the above code −

Traceback (most recent call last):
  File "/home/cg/root/52187/main.py", line 4, in <module>
    my_Array = np.random.randn(-6)
  File "mtrand.pyx", line 1270, in numpy.random.mtrand.RandomState.randn
  File "mtrand.pyx", line 1431, in numpy.random.mtrand.RandomState.standard_normal
  File "_common.pyx", line 636, in numpy.random._common.cont
ValueError: negative dimensions are not allowed
numpy_array_creation_routines.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy