Poisson Distribution in Python

In this tutorial you will learn:

  • What is a Poisson Distribution?
  • Poisson Distribution Implementation in python
  • Visualization of Poisson Distribution

Poisson Distribution

The Poisson distribution is the discrete probability distribution of the number of events occurring in a given time period, the average number of times the event occurs over that time period is known. The number of experiments in Poisson Distribution are always fixed. For example, if an individual is responsible for keeping a count of number of mails arriving in the post office, and he observes that he receives an average number of 4 mails in a day. The mails arriving in the post office are arriving independently of the time of arrival of the last mail, hence it will be correct to assume that the pieces of mail obey poisson distribution. In this case, the poisson distribution will let us find the probability of receiving 5 mails.

Poisson Distribution in Python

Poisson distribution in python is implemented using poisson() function. The poisson() function takes in two mandatory parameters. Frist parameter “size” is the size of the output of multi dimensional array while the second parameter “lam” is the rate of occurrence of a specific event. In this example we will generate an 1D array(1,5) of distribution having the occurrence 3, if we relate this example with the example explained above, here we are considering that the mails being received in a day are 3, and are finding 5 poisson distributions for it.
  1. #importing the random module
  2. from numpy import random
  3. #applying the poisson function with 3 occurrences and 5 distributions
  4. res = random.poisson(lam=3, size=5)
  5. #printing the result
  6. print('1D array having 5 poisson distributions with 3 occurences:\n')
  7. print(res)
In this example we will generate a 2D array of poisson distribution having size (5,2), in the first line of code we are importing random module from numpy library and in third line we are calling the poisson function while declaring occurrence (lam = 5) and size of the array (5,2).
  1. #importing the random module
  2. from numpy import random
  3. #here we are using poisson function to generate poisson distribution of size 5 x 2 with occurrence 5
  4. res = random.poisson(lam=5, size=(5,2))
  5. print('2D Poisson Distribution as output from poisson() function:\n')
  6. #printing the result
  7. print(res)
Lets take another example, in this example we will generate a 3D array of poisson distribution of the size(5,2,3) with occurrence 8 times.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using poisson function to generate poisson distribution of size 5 x 2 x 3 with occurrence 8
  4. res = random.poisson(lam=8, size=(5,2,3))
  5. print('3D Poisson Distribution as output from poisson() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Poisson Distribution

In this example, for our better understanding of poisson distribution itself and the syntax, we will plot poisson distribution on a graph. Here we will again use distplot() function for graph plot. The occurrences in an interval will be 4 while the number of distributions will be 100.
  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using Poisson function to generate Poisson distribution of size 100
  6. sb.distplot(random.poisson(size=100, lam = 4),kde=False)
  7. #plotting the graph
  8. mpl.show()

Add new comment