Diving into Python’s Numpy Random Poisson

Numpy is a library in python that helps work with multi-dimensional arrays and matrices in python. Numpy stands for Numerical Python. Using numpy enables us to process arrays in python at a faster rate. It is an open-source library for performing scientific computations and logical and mathematical operations on python arrays. Out of the many available functions in python, let us dive into one such function – Numpy Random Poisson.

What is Poisson Distribution?

Before moving ahead with the function, we need to know first what exactly is poisson distribution. Poisson Distribution is a concept that is derived from probability and statistics. The Poisson Distribution tells us about the frequency with which an event occurs in a given interval.

We need to know here how often an event occurs in a specific interval. Basically, it is used to predict the probability of certain events happening if we know how often the event has occurred.

Probability Function for poisson distribution

The formula for the poisson distribution is given below. Here, the event is happening with ‘k’ occurrences.

Numpy Random Poisson

Here:

k: The number of occurrences of an event for which poisson distribution has to be found

λ: The expected number of event occurrences in the given interval

e: It is the Euler’s constant whose approximate value is 2.72.

Let us understand by calculating a example. Lets us assume that a particular event occurs for 2 times on an average. We want to find the likelihood of the same event occurring for 4 times in the same time interval. Then we can use poisson distribution to calculate that probability.

Here, λ will be equal to 2 and k will be equal to 4.

P(4) = ( (2^4) * (2.72 ^ (-2)) ) / 4!

The probability will be approximately equal to 0.09. That means there is only 9% chance that the same event will occur 4 times instead of 2.

Numpy Random Poisson

The Random Poisson function in numpy is used to calculate the poisson distribution for a given sample. This method draws random samples from a poisson distribution. With this function, we can determine the average rate at which a given event occurs.

Syntax of Numpy Random Poisson Function

The syntax of the random poisson function is:

numpy.random.poisson(lam=1.0,size=None)

Parameters:

lam: It is a float value or an array of float values. The lam corresponds to the λ value in the poisson distribution formula. It denotes the expected number of event occurrences for a given time interval. Its value should be greater than or equal to zero since no event can occur a negative amount of times.

size: It is an optional parameter whose default value is ‘None’. For None, it will return a single value as a sample. If you pass any (x,y) value, then x * y samples will be drawn from the poisson distribution. If any other value n is passed, then n samples would be drawn from it.

Return Value:

out: It returns an n-dimensional array or a scalar value as the output. The output consists of the drawn samples from the poisson distribution.

Numpy Random Poisson using Python

Let us look at some examples where we will apply numpy’s random poisson function. First, we shall import the numpy library in python.

import numpy as np

Now at first, we shall pass the lam value as 5 into the np.random.poisson() function. We shall not pass the size parameter and hence, the size will be ‘None’, Then we shall save the drawn sample into a variable named ‘a’. It will simply calculate that if an even occurred for ‘lam’ number of times, then what will be the frequency that the event will occur in the given interval ‘size’.

a = np.random.poisson(5)

print(a)

The output is:

3

Everytime you try to print the variable ‘a’, it will generate a different output. We can also plot a histogram for the same. For that, we will have to import the matlplotlib library.

import matplotlib.pyplot as plt

plt.hist(a)

plt.show()

The printed histogram is:

Numpy Random Poisson

The Entire Piece of Code is:

import numpy as np

import matplotlib.pyplot as plt

a = np.random.poisson(5)

print(a)

plt.hist(a)

plt.show()

Now, we shall generate multiple samples from the distribution. We will pass the size as 20. As a result, 20 samples will be drawn.

import numpy as np

import matplotlib.pyplot as plt

a = np.random.poisson(5,20)

print(a)

plt.hist(a)

plt.show()

The output is:

[ 2  3  7  5  4  7  5  2  6  6 11  2  4  8  9  5  2  4  3  4]

The histogram when plotted for the above values would look something like this:

Numpy Random Poisson

Numpy Random Poisson for genertaing multi dimensional points

We can also draw multi dimensional samples from a given distribution. For that, we have to pass the size in the (x,y) form for creating two dimensional samples. Here we will be generating a 4 by 4 sample distribution for points.

import numpy as np
import matplotlib.pyplot as plt
a = np.random.poisson(5,(4,4))
print(a)
plt.hist(a)
plt.show()

The 16 samples generated are:

[[8 6 7 2]
 [3 4 2 9]
 [8 5 5 6]
 [8 4 3 6]]

The histogram plotted for the 16 samples is:

Numpy Random Poisson

Finding mean of random poisson

Let us generate 100 samples of a poisson distribution with the mean as 50.

import numpy as np

import matplotlib.pyplot as plt

a = np.random.poisson(50,100)

print(a)

The 100 samples generated are:

[59 53 42 50 52 46 48 63 53 40 38 52 46 45 40 42 44 58 45 61 57 47 43 57
 41 57 50 65 48 50 53 52 54 51 53 57 45 54 44 54 53 47 45 46 41 54 46 50
 47 48 45 64 60 37 47 56 45 63 59 39 57 44 54 43 47 59 43 43 42 48 46 47
 62 56 40 47 63 73 58 41 63 42 45 48 58 45 49 46 59 51 40 55 57 50 58 55
 51 62 42 51]

To plot the histogram, we shall be using the hist() function and the show() function present in the matplotlib library.

plt.hist(a)

plt.show()

The histogram is:

Numpy Random Poisson

To find the average of the poisson distribution, we shall be using the mean() function from the numpy library.

print(np.mean(a))

The output is:

50.41

As we can see here, 50.41 is the mean of all the 100 samples generated.

The entire code is:

import numpy as np

import matplotlib.pyplot as plt

a = np.random.poisson(50,100)

print(a)

plt.hist(a)

plt.show()

print(np.mean(a))

Drawing samples for different lam values

We can also draw an equal amount of samples for two different values of lam, simultaneously. In this example, we shall be drawing 20 different sample values for two values of lam: λ = 10 and λ = 100. We will be passing lam argument in the form of (10,100).

a = np.random.poisson((10, 100), (20,2))

print(a)

The generated samples are in two dimensional form:

[[  3 113]
 [  4 104]
 [  7  97]
 [ 14  89]
 [ 11  94]
 [ 11  96]
 [ 13 121]
 [ 14 115]
 [  8 114]
 [ 10  99]
 [  9 101]
 [ 13  94]
 [ 10  91]
 [ 10  96]
 [  7 101]
 [  7  97]
 [  9 121]
 [  7 101]
 [ 10 111]
 [  2 117]]

The histogram plotted for the total 40 samples generated is:

Numpy Random Poisson

The entire code is:

import numpy as np

import matplotlib.pyplot as plt

a = np.random.poisson((10, 100), (20,2))

print(a)

plt.hist(a)

plt.show()

Adding Poisson noise to an image

We can also use the random poisson function to add noise to a given image. Here we will use a cat image and feeding that to the imread() function. Then we obtain the noise_mask by using np.random.poisson() function and then add it to the original image. Then, we will use numpy clip() function and convert it into an image.

The cat image used is:

cat image numpy poisson
Photo by Cédric VT on Unsplash

The code for adding noise to image is:

import numpy as np
import imageio

img = 'cat.jpg'

img = (imageio.imread(img)).astype(float)

noise_mask = np.random.poisson(img)

noise_img = img + noise_mask

new = np.clip(noise_img, 0, 255).astype(np.uint8)

imageio.imsave('new.jpg',new)

The image after adding noise is:

adding noise to image

Adding seed to numpy random poisson function

The seed function is used to set the random state for random class in numpy. When used with the random poisson function, we can manipulate the result obtained from the poisson function. With the seed function, we can ensure that the same random numbers appear every time.

Let us understand with the help of an example

We will take the seed value of 2 and generate numpy random variables of 2 by 2 dimension:

import numpy as np
np.random.seed(2)
print(np.random.poisson(lam =(10., 50.), size = (2,2)))

The output is:

[[ 9 51]
 [ 9 43]]

Now if we try to generate the same code again, it will generate the same random numbers unlike before where it was generating different values every time. This is because of the seed() function

[[ 9 51]
 [ 9 43]]

We obtain the same output again.

FAQ’s

What are the applications of the Numpy Random Poisson Function?

The Random Poisson Function finds applications in hypothesis testing. Based on the average rate of occurrence of an event, the function can predict the amount of variation in a given interval.

This sums up the Random Poisson Function in numpy. If you have any questions in your mind or any thoughts to share, don’t forget to let us know in the comments below.

Until then, Keep Learning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments