# 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.

`#importing the random modulefrom numpy import random#applying the poisson function with 3 occurrences and 5 distributionsres = random.poisson(lam=3, size=5)#printing the resultprint('1D array having 5 poisson distributions with 3 occurences:\n')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).
`#importing the random modulefrom numpy import random#here we are using poisson function to generate poisson distribution of size 5 x 2 with occurrence 5res = random.poisson(lam=5, size=(5,2))print('2D Poisson Distribution as output from poisson() function:\n')#printing the resultprint(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.
`#importing the random modulefrom numpy import random#here we are using poisson function to generate poisson distribution of size 5 x 2 x 3 with occurrence 8res = random.poisson(lam=8, size=(5,2,3))print('3D Poisson Distribution as output from poisson() function:\n')#printing the resultprint(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.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using Poisson function to generate Poisson distribution of size 100sb.distplot(random.poisson(size=100, lam = 4),kde=False)#plotting the graphmpl.show()`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.