Chandan Rajpurohit

An Artist With Technical Skills

In an earlier post we have seen What is NumPy?

We have also seen Features  of NumPy,Need for NumPy? We have worked with Array Functionalities using NumPy.

Let us look into some more Functionalities provided by NumPy.

Random Sampling in NumPy

In addition with built-in functions, NumPy also comes with a random sub-module which provides functions to generate data randomly and draw samples from various distributions.

Let see some of these functions.

  1. rand([d0,d1,….,dn])

It is used to create an array of given shape and populate it with random samples from a uniform distribution over [0,1].If no arguments are provided it will return a single float value. Only positive arguments are taken by rand.

#generating single random number

a = np.random.rand()

$o/p : 0.88767776

#generating 1D array with random values

a = np.random.rand(5)

$o/p : array([0.12345,0.23456,0.2131233,0.1241324,0.2133344])

# generating 2D array

a = np.random.rand(2,2)

$o/p : array([[0.99876,0.97657],[0.8766544,0.567433]]) 

  1. randn([d0,d1,….,dn]):

It is used to create an array of given shape and populate it with random samples from a standard normal distributions.It generates an array of shape(d0,d1,….,dn) with random floats sampled from univariate normal distribution of mean 0 and variance 1. It takes only positive arguments. If no argument is passed, a single float value is returned.

#generating single random number

a = np.random.randn()

$o/p : 0.88767776

# generating 2D array

a = np.random.randn(2,2)

$o/p : array([[0.99876,-0.97657],[-0.8766544,0.567433]]) 

  1. randint(low,high=None,size=None):

It returns a random integer from a discrete uniform distribution with limits of low(inclusive) and high(exclusive). If high is not passed i.e None(default) then results are from 0 to low. If size is specified it returns an array of specified size.

#generating a random integer between 0 and 5

a = np.random.randint(5)

$o/p : 3

#generating a random integer between 2 and 7

a = np.random.randint(2,7)

$o/p : 4

#generating a 1D array 

a = np.random.randint(2,5,size=2)

$o/p : array([3,4])

#generating a 2D array

a = np.random.randint(3,9,size=(2,3))

$o/p : array([[7,4,6],[5,8,6]])

  1. random(size=None):

It returns a random float value between 0 and 1 from continuous uniform distribution.

#generating single random number

a = np.random.random()

$o/p : 0.88767776

#generating 1D array with random values

a = np.random.random(5)

$o/p : array([0.12345,0.23456,0.2131233,0.1241324,0.2133344])

# generating 2D array

a = np.random.random(2,2)

$o/p : array([[0.99876,0.97657],[0.8766544,0.567433]]) 

  1. normal(mu=0.0,sigma=1.0,size=None):

It returns random samples from a normal (Gaussian) distribution. If no arguments are passed, a sample will be drawn from N(0,1).

#generating samples from 1D array

a = np.random.normal(0,0.1,5)

$o/p : array([0.12345,-0.23456,0.2131233,-0.1241324,0.2133344])

  1. uniform(low=0.0,high=1.0,size=None):

It returns samples from a uniform distribution over interval 0(inclusive) and 1(exclusive) if arguments are not provided.

#creating 1D array under [-1,0]

a = np.random.uniform(-1,0,5)

$o/p : array([-0.12345,-0.23456,-0.2131233,-0.1241324,-0.2133344])

  1. binomial(n,p,size=None):

It returns samples drawn from binomial distribution with n trials and p probability of success where n is greater than 0 and p under [0,1]

#coin flip for 10 times

Samples = np.random.binomial(1,0.5,10)

$o/p : array([1,1,0,1,0,0,1,0,1,1,0])

More on NumPy in upcoming posts.

Happy Learning!

%d bloggers like this: