AppDividend
Latest Code Tutorials

np.random.randn: Understanding Numpy.random.randn()

In Python, numpy.random.randn() function creates an array of specified shapes and fills them with random specified values per standard Gaussian/normal distribution. 

np.random.randn

The np.random.randn() is a numpy library method that returns a sample (or samples) from the “standard normal” distribution. It takes the dimensions of the returned array as an argument and returns either ndarray or, if no argument is provided, returns the float value.

The np.random.randn() function returns all the values in float form and in distribution mean = 0 and variance = 1.

Syntax

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

Parameters

If positive parameters are provided, the randn() function generates the array of shape (d0, d1, …, dn), filled with random floats sampled from the univariate “normal” (Gaussian) distribution of mean 0 and variance 1,

A single float randomly sampled from a distribution is returned if no argument is provided. In addition, the dimensions of the returned array must be non-negative. If you provide a negative argument, then it will return an error. If no argument is given, a single Python float is returned.

Example

# app.py

import numpy as np

data = np.random.randn()
print(data)

Output

➜  pyt python3 app.py
-0.7919353665049774
➜  pyt python3 app.py
0.9218908714949405
➜  pyt python3 app.py
-0.025179948728764872
➜  pyt python3 app.py
0.29764955041572655
➜  pyt python3 app.py
-0.8279168113225552
➜  pyt python3 app.py
-1.5048354875053158

Every time you run the app.py file, you will get different random values.

Creating a 1D array using np random randn()

To create a 1D array in Python, use the np.random.randn() method. The numpy random randn() method takes only one dimension and returns the one-dimensional array.

Let’s create a 1D array with 6 elements in it.

# app.py

import numpy as np

data = np.random.randn(6)
print(data)

Output

python3 app.py
[ 1.08086154  0.70693     0.38091969 -1.64244255  1.13132413 -0.7443323 ]

We have passed 6 as an argument to create 6 random items of the array.

See another example.

# importing numpy
import numpy as np

# Now creating an 1D array of size 10
arr = np.random.randn(10)

print("Values of 1D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))
# Creating of size 5
arr2 = np.random.randn(5)
print("Values of the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))

Output

python3 app.py
Values of 1D array is:
 [ 0.456789    0.11413981 -0.06548174  0.54791075 -0.18972466 -0.11922963
  1.37909645 -0.0688107  -0.02731399  0.09351504]
Shape of the array is :  (10,)
Values of the array is:
  [ 0.99990254 -0.07788214 -0.63521035 -1.01484305 -0.2993925 ]
Shape of the array is :  (5,)

Explanation

In this example, we have printed two 1D arrays using random.randn() function. In the first case, we have printed an array of shape 10 and the second array of shape 5. Array values are inserted randomly as per the above-discussed rule.

Creating a 2D array using np random randn()

To create a 2D array in Python, use the np.random.randn() method and pass two parameters like dimensions, and it returns the two-dimensional array.

Syntax

The syntax for creating a two-dimensional array using random.randn() function is the following.

np.random.randn(d1, d2)

Parameters

It takes two parameters.

  1. The d1 parameter shows how many rows we need to create an array.
  2. The d2 parameter shows how many columns we need to create an array.

See the following code.

# app.py

import numpy as np

data = np.random.randn(2, 2)
print(data)

Output

python3 app.py
[[1.38596221 1.59121102]
 [0.11743191 0.89372055]]

Create a 3D array using np random randn()

To create a 3D array in Python, use the np.random.randn() method and pass three parameters as dimensions, and it returns the three-dimensional array.

Example

See the following code.

# app.py

import numpy as np

data = np.random.randn(3, 3, 3)
print(data)

Output

python3 app.py
[[[-1.31932293 -0.55698306 -0.52587777]
  [-1.02907293 -0.87960688  0.48399357]
  [-0.64534737 -0.40360183  0.90921266]]

 [[ 0.94321599  0.67847027  0.70100542]
  [-0.52738798 -0.69975292  0.0960497 ]
  [-0.3399558   1.54436365  0.26914068]]

 [[ 1.98426783  1.27291484 -0.06685548]
  [-0.36821547  1.30168745  1.69065317]
  [ 1.26130492  2.05068361  0.82860505]]]

It will generate 3D arrays with positive and negative random values.

We can not pass the negative dimension to the randn() function; otherwise, it returns ValueError. 

# app.py

import numpy as np

data = np.random.randn(-3)
print(data)

Output

python3 app.py
Traceback (most recent call last):
  File "app.py", line 3, in <module>
    data = np.random.randn(-3)
  File "mtrand.pyx", line 1218, in numpy.random.mtrand.RandomState.randn
  File "mtrand.pyx", line 1375, in numpy.random.mtrand.RandomState.standard_normal
  File "_common.pyx", line 558, in numpy.random._common.cont
ValueError: negative dimensions are not allowed

See another code example.

# importing numpy
import numpy as np

# Now creating an 3D array of size 2x2x3
arr = np.random.randn(2, 2, 3)

print("Values of 3D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))
# Creating 2D array of size 5x5
arr2 = np.random.randn(5, 5)
print("Values of the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))

Output

python3 app.py
Values of 3D array is:
 [[[ 1.17736919  1.26366938 -0.06364264]
  [ 0.57792361 -0.68398595  0.32957414]]

 [[ 0.49199588  0.56612773  0.98434267]
  [ 0.30158417  1.20173148 -0.36974876]]]
Shape of the array is :  (2, 2, 3)
Values of the array is:
  [[ 2.50244931e+00  1.12361971e+00 -2.54657975e-01  2.12150049e-02
  -4.34988456e-01]
 [-4.84566143e-01  2.24132038e-01 -1.42568814e+00 -2.47381915e-04
   3.37966017e-01]
 [-1.56456348e+00 -2.03418573e-01 -7.98728742e-01  8.25852255e-01
  -1.57770187e-01]
 [-8.90851952e-01  9.51316758e-01 -2.90582269e-01 -9.88468496e-01
  -4.66474163e-01]
 [ 1.86058960e-01  3.19397531e-01 -1.59117225e+00  2.16834898e-01
  -4.51887901e-01]]
Shape of the array is :  (5, 5)

Explanation

In this example, we have printed one 3D array using random.randn() function. In this case, we have printed an array of shapes 2x2x3. Also, we have printed one 2D array using the function. Array values are inserted randomly as per the above-discussed rule.

Change a randomly created array.

In this example, first, we will create an array of 2D with np random randn() function and then multiply that array into 2 and then Add 2 in the array.

# app.py

import numpy as np

array = np.random.randn(2, 2)
print("2D Array filled with random values : \n", array)

# Multiplying values with 2
print("\nArray * 2 : \n", array * 2)

# Or we cab directly do so by
array = np.random.randn(2, 2) * 2 + 2
print("\nArray * 2 + 2 : \n", array)

Output

python3 app.py
2D Array filled with random values :
 [[ 0.61033846 -2.17725096]
 [-0.45407816  2.04812173]]

Array * 2 :
 [[ 1.22067691 -4.35450192]
 [-0.90815633  4.09624346]]

Array * 2 + 2 :
 [[0.99010675 2.16741196]
 [1.99530574 3.23772725]]

Reshape the array using np.reshape()

There are some instances where we have to reshape the array. In Python, numpy.reshape() function, change the dimension and return the new array.

See the following code.

# app.py

import numpy as np

array = np.random.randn(3, 4)
print(array)

print("After reshape the array")
print(array.reshape(6, 2))

Output

python3 app.py
[[ 0.86247213 -0.36951031  0.74445018 -1.28952837]
 [-1.10220821  0.15989654 -0.49336996  1.05084014]
 [ 0.84959592  0.7147576   0.83266357 -0.24533966]]
After reshape the array
[[ 0.86247213 -0.36951031]
 [ 0.74445018 -1.28952837]
 [-1.10220821  0.15989654]
 [-0.49336996  1.05084014]
 [ 0.84959592  0.7147576 ]
 [ 0.83266357 -0.24533966]]

Our first array is created from the np random randn() function, and then we have used the numpy reshape() function to change the dimensions of the array. Remember, the value of the array is not changing here, but the dimension is changing.

We can also convert the above 2D array into a 3D array using reshape() function.

See the following code.

# app.py

import numpy as np

array = np.random.randn(3, 4)
print(array)

print("After reshape the array")
print(array.reshape(2, 2, 3))

Output

python3 app.py
[[ 2.28291597 -0.53931961 -0.62792401 -0.81161398]
 [-0.7488756   0.43830336  0.27803475  2.64883588]
 [ 0.4840037  -0.95278401  0.5861628  -1.08436804]]
After reshape the array
[[[ 2.28291597 -0.53931961 -0.62792401]
  [-0.81161398 -0.7488756   0.43830336]]

 [[ 0.27803475  2.64883588  0.4840037 ]
  [-0.95278401  0.5861628  -1.08436804]]]

Conclusion

In this tutorial, we have seen how we can use numpy random.randn() method to create a 1D array, 2D array, 3D array.

Using np.reshape() method, we can change its dimension. That is it for today’s topic.

See also

Generate random permutation in Numpy

Python random number module

How to generate random number in Python

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.