Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

# What is the random.poisson function in Python?

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.   The random.poisson function is part of Python’s NumPy library and is used to model probabilistic experiments with the help of the Poisson distribution.

To use the random.poisson function, you must import numpy at the very start of the program:

from numpy import random


### Syntax

The random.poisson method has the following syntax:

numpy.random.poisson(l,n)


### Parameters

The random.poisson function takes in two parameters:

• l: l stands for Lamba and is the rate at which a Poisson process occurs. l must be greater than or equal to zero and of type float.
• n (optional): the number of samples to be drawn. The default value is 1.

The Poisson distribution is used to model a situation or event that occurs at a constant rate, which we denote as Lambda.

### Return value

The random.poisson method returns an array of length n. This is the bunch of samples drawn from the Poisson distribution modeled with Lambda equal to l.

### Code

The code below shows how to use the random.poisson function in Python.

First, we set the values of n and l to 3000 and 2.34, respectively. This is to denote that a particular event occurs at a constant rate of 2.34. The units of l must be consistent with the units of the drawn samples.

Next, we call the random.poisson function and print its return value with the print function. The return value is a list of 3000 values, which corresponds to the 3000 samples drawn from the Poisson process modeled with l as the parameter.

We use the print function to print the length of the return value. As expected, it turns out to be 3000.

from numpy import random#declaring and assigning values to n and ln=3000l=2.34#calling random.poisson methodret_val=random.poisson(l, n)#printing return value and its lengthprint("Return value:", ret_val)print("Length of return value:", len(ret_val))

If we graph this Poisson process, our plot would resemble the following sketch:

import seaborn as sns
import matplotlib.pyplot as plt
#ret_val is the return value obtained from the code snippet above
sns.distplot(ret_val, kde=True)
plt.show()
3000 samples from a Poisson process with Lambda=2.34 plotted in the Cartesian plane.

As expected, the probability reaches the maximum when the number of occurrences is equal to Lambda.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.   Keep Exploring

Learn in-demand tech skills in half the time 