58.Random Numbers

Random numbers are widely used in science and engineering computations.They can be used to simulate noisy data, or to model physical phenomena like the distribution of velocities of molecules in a gas, or to act like the roll of dice in a game. There are even methods for numerically evaluating multi-dimensional integrals using random numbers.
The basic idea of a random number generator is that it should be able to produce a sequence of numbers that are distributed according to some predetermined distribution function. NumPy provides a number of such random number generators in its library numpy.random. Here we focus on three: rand, randn, and randint.

Random means something that can not be predicted logically.Computers work on programs, and programs are definitive set of instructions. So it means there must be some algorithm to generate a random number as well.

If there is a program to generate random number it can be predicted, thus it is not truly random.
Random numbers generated through a generation algorithm are called pseudo random.

Can we make truly random numbers?
Yes. In order to generate a truly random number on our computers we need to get the random data from some outside source. This outside source is generally our keystrokes, mouse movements, data on network etc.Pseudo random number generation can be done with numpy random module
The random module's randint(n) method returns a random number from 0 to n-1.
Example
import numpy as np
x = np.random.randint(100)
print(x)
Output
64

The randint() method takes a size parameter where you can specify the shape of an array.The following commands will generate 5 random numbers from 0 to 100-1.

import numpy as np
x = np.random.randint(100,size=5)
print(x)
[25 62 24 81 39]

The following will Generate a 2-D array with 3 rows, each row containing 5 random integers from 0 to 100-1:
import numpy as np
x = np.random.randint(100,size=(3,5))
print(x)
[[ 2 96 40 43 85]
 [81 81  4 48 29]
 [80 31  6 10 24]]

Generate a 1-D array of 10 random integers. Each integer should be a number between 30 and 40 (inclusive) ( university question)

import numpy as np
np.random.randint(30,41,size=10)

OR

import random
list=random.sample(range(30,41),10)
print(list)

[33, 35, 36, 30, 34, 40, 32, 31, 38, 39]

The random module's rand() method returns a random float between 0 and 1.
import numpy as np
x = np.random.rand()
print(x)
0.2733166576024767

This will generate 10 random numbers
x = np.random.rand(10)
print(x)
[0.82536563 0.46789636 0.28863107 0.83941914 0.24424812
0.25816291 0.72567413 0.80770073 0.32845661 0.34451507]
Generate an array with size (3,5)
x = np.random.rand(3,5)
print(x)

[[0.16220086 0.80935717 0.97331357 0.60975199 0.48542906] [0.68311884 0.27623475 0.73447814 0.29257476 0.27329666] [0.62625815 0.0069779 0.21403868 0.49191027 0.4116709 ]]

The numpy.random.randn() function creates an 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)

A single float randomly sampled from the distribution is returned if no argument is provided.
Syntax :
numpy.random.randn(d0, d1, ..., dn)

Example:
import numpy as np
import matplotlib.pyplot as plt
y=np.random.randn(10)
print(y)
[-1.02942418 -0.43414373 -1.94499517 0.03722918 -0.24486256 0.26037007
0.69586681 -0.28966497 0.29951506 -0.8832814 ]

x=np.random.randn(2,3)
print(x)
[[ 1.27386593 -0.91964348 0.9923548 ]
[-0.30152177 -0.16279287 1.01772984]]

The choice() method allows to get a random value from an array of values.
import numpy as np
x = np.random.choice([3,5,6,7,9,2])
print(x)
3

import numpy as np
x = np.random.choice([3,5,6,7,9,2],size=(3,5))
print(x)
[[3 2 5 2 6]
 [5 9 3 6 9]
 [5 6 9 3 3]]

Random Data Distribution
Data Distribution is a list of all possible values, and how often each value occurs.Such lists are important when working with statistics and data science.
The random module offer methods that returns randomly generated data distributions.
A random distribution is a set of random numbers that follow a certain probability density function.

Probability Density Function: A function that describes a continuous probability. i.e. probability of all values in an array.
We can generate random numbers based on defined probabilities using the choice() method of the random module.
The choice() method allows us to specify the probability for each value.

The probability is set by a number between 0 and 1, where 0 means that the value will never occur and 1 means that the value will always occur.

Example

Generate a 1-D array containing 10 values, where each value has to be 3, 5, 7 or 9.
The probability for the value to be 3 is set to be 0.1
The probability for the value to be 5 is set to be 0.3
The probability for the value to be 7 is set to be 0.6
The probability for the value to be 9 is set to be 0

import numpy as np
x = np.random.choice([3,5,7,9],p=[0.1,0.3,0.6,0.0],size=10)
print(x)
[5 7 7 7 5 7 7 3 7 5]

Random Permutations
A permutation refers to an arrangement of elements. e.g. [3, 2, 1] is a permutation of [1, 2, 3] and vice-versa.
The NumPy Random module provides two methods for this: shuffle() and  permutation().

Shuffling Arrays

Shuffle means changing arrangement of elements in-place. i.e. in the array itself.
import numpy as np
x=np.array([1,2,3,4,5])
np.random.shuffle(x)
print(x)
[4 1 3 5 2]

Generating Permutation of Arrays
The permutation() method returns a re-arranged array (and leaves the original array un-changed).
import numpy as np
x=np.array([1,2,3,4,5])
y=np.random.permutation(x)
print(y)
[3 1 5 2 4]

Normal (Gaussian) Distribution

The Normal Distribution is one of the most important distributions.
It is also called the Gaussian Distribution after the German mathematician Carl Friedrich Gauss.
It fits the probability distribution of many events, eg. IQ Scores, Heartbeat etc.
Use the random.normal() method to get a Normal Data Distribution.
It has three parameters:

loc - (Mean) where the peak of the bell exists.
scale - (Standard Deviation) how flat the graph distribution should be.
size - The shape of the returned array.

The numpy.random module supplements the built-in Python random with functions for efficiently generating whole arrays of sample values from many kinds of probability distributions. For example, you can get a 4 by 4 array of samples from the standard normal distribution using normal:

import numpy as np
print(np.random.normal(size=(4,4)))
[[ 0.18577774 -1.07506339 1.0338707 1.32696306]
[ 0.41939598 -1.15732977 -0.19081001 0.10567808]
[ 0.7482679 -0.39357911 0.08297663 -0.60563642]
[ 0.23671784 -1.3504756 0.24030689 0.4240251 ]]

x = np.random.normal(loc=1, scale=2, size=(2, 3))
print(x)
[[ 4.6162552 2.90317721 1.75121165]
[-0.03026904 3.54906062 1.25067476]]

Visualization of Normal Distribution

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.normal(size=1000), hist=False)
plt.show()

Binomial Distribution

Binomial Distribution is a Discrete Distribution.
It describes the outcome of binary scenarios, e.g. toss of a coin, it will either be head or tails.

It has three parameters:
n - number of trials.
p - probability of occurrence of each trial (e.g. for toss of a coin 0.5 each).
size - The shape of the returned array.

Discrete Distribution:The distribution is defined at separate set of events, e.g. a coin toss's result is discrete as it can be only head or tails whereas height of people is continuous as it can be 170, 170.1, 170.11 and so on.

Example
Given 10 trials for coin toss generate 10 data points:
import numpy as np
x =np. random.binomial(n=10, p=0.5, size=10)
print(x)
Output
[6 5 4 3 5 5 2 6 3 7]

Visualization of Binomial Distribution
from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(random.binomial(n=10, p=0.5, size=1000), hist=True, kde=False)
plt.show()



Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

46.Classes and Objects in Python- Accessors and mutators

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020