Change language

numpy.random.randn () in Python

| |

If positive arguments are provided, randn generates an array of the form (d0, d1, ..., dn) filled with random floating point numbers selected from the one-dimensional "normal" (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i is float, they are first converted to integers by truncation). A single randomly chosen floating point number from the distribution is returned if no argument is supplied.

Parameters :

  d0, d1, .. ., dn:  [int, optional] 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. 

Code 1: randomly constructing a 1D array

# Python program illustrating
# numpy.random.randn () method

 

import numpy as geek

 
# 1D Array

array = geek.random.randn ( 5 )

print ( "1D Array filled with random values:" , array); 

Output:

 1D Array filled with randnom values: [-0.51733692 0.48813676 -0.88147002 1.12901958 0.68026197] 

Code 2: Randomly build 2D array

# Python program illustrating
# numpy.random.randn () method

 

import numpy as geek

 
# 2D Array

array = geek.random.randn ( 3 , 4 )

print ( "2D Array filled with random values:" , array); 

Output:

 2D Array filled with random values: [[1.33262386 -0.88922967 -0.07056098 0.27340112] [1.00664965 -0.68443807 0.43801295 -0.35874714] [-0.19289416 -0.42746963 -1.80435223 0.02751727>] 
 Code : randomly building a 3-D array  

# Python program illustrating
# numpy .random.randn () method

 

import numpy as geek

 
# 3D Array

array = geek.random. randn ( 2 , 2 , 2 )

print ( "3D Array filled with random values:" , array); 

Output:

 3D Array filled with random values: [[[-0.00416587 -0.66211158] [-0.97254293 -0.68981333]] [[-0.18304476 -0.8371425] [2.18985366 -0.9740637]]]] 


Code 4: Manipulating a randomly created array

# Python program illustrating
# numpy.random.randn () method

 

import numpy as geek

  
# 3D Array

array = geek.random.randn ( 2 , 2 , 2 )

print ( "3D Array filled with random values:" , array); 

 
# Multiply values ​​by 3

print ( "Array * 3:" , array * 3 )

 
# Or we can do it directly,

array = geek.random.randn ( 2 , 2 , 2 ) * 3 + 2

print ( " Array * 3 + 2: " , array); 

Output:

 3D Array filled with random values: [[[1.9609643 -1.89882763] [0.52252173 0.08159455]] [[-0.6060213 -0.86759247] [0.53870235 -0.77388125]]] Array * 3: [[[5.88289288] [1.51965 0.24478366]] [[-1.81806391 -2.6027774] [1.61610704 -2.32164376]]] Array * 3 + 2: [[[-2.73766306 6.80761741] [-1.57909191 -1.64195796]] [[0.51019498 1.30017345] [3.438107863] / pre> 

Links:
randn.html > https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.randn.html

Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work. 
,
This article is provided by Mohit Gupta_OMG

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method