How to generate random numbers from Poisson distribution with Numpy

In this post, we will learn how to use Numpy’s Random Generator class to generate random numbers sampled from Poisson distribution using poisson function in Numpy.

The Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space satisfying two conditions. The first being that the these events should happen with a known average rate and the second condition is that the events should occur independent of the last event.

import numpy as np
# find Numpy version
np.__version__

1.24.0

Basic syntax of poisson() function in Numpy for generating random numbers from Poisson random distribution is as shown below.

poisson(lam=1.0, size=None)

Poisson function can take two parameters as input. The first parameter is lam for lambda. In a Poisson distribution, the parameter Lambda refers to the expected number of events occurring in a fixed-time interval and it must be >= 0.

In poisson() function, the argument lam can be a float for generating random numbers from Poisson distribution with lambda=lam. It can also be array like object containing multiple lambdas for generating random numbers from multiple Poisson distributions.

The second parameter in the poisson() function is size and it specifies the number of samples to be drawn from Poisson distribution. The default value for size is None and it will generate a single random number. As with other distribution, size can be a single value or a tuple.

Example 1: Generating a random integer

Let us generate single random number from Poisson distribution using default settings. We first create Random Generator object using default_rng() function.

rng = np.random.default_rng()

And then poisson() function without any arguments to generate a single random number from Poisson distribution with lambda=1.

rng.poisson()

2

In the example below we generate a single random number from Poisson distribution, but with lambda=5

rng.poisson(lam=5)

6

Example 2: Generating an array of random numbers

By using size argument and specifying an integer for size argument, we can generate an array of random numbers from Poisson distribution.
Here, we generate 10 random numbers from Poisson distribution with lambda=5.

rng = np.random.default_rng()
rng.poisson(lam=5, size=10)
array([ 7,  1,  4,  8,  3, 11,  3,  3,  8,  5])

Example 3: Generating a matrix of random numbers

Using the size argument again but this time as a tuple, we can generate a 2 dimensional numpy array or matrix of random numbers from Poisson distribution.

In the code below, we generate a matrix of dimension 5 x 5 containing random numbers from Poisson distribution with lambda = 5.

rng.poisson(lam=5, size=(5,5))

array([[ 6,  7,  3,  4,  5],
       [ 5,  6,  5,  1,  6],
       [ 2,  1, 11,  4,  8],
       [ 5,  2,  2,  4,  3],
       [ 8,  8,  6,  4,  3]])

Similarly, by using a tuple with three elements we can create numpy 3d array of random numbers from Poisson distribution.

# numpy 3d array
rng.poisson(lam=5, size=(2,5,5))

Here is the 3D random matrix generated from poisson() function.

array([[[ 3,  7,  9,  6,  7],
        [ 6,  3,  5,  3,  5],
        [ 8,  3,  2, 10,  4],
        [ 3,  3,  2,  2,  2],
        [ 6,  8,  6,  5,  6]],

       [[ 5,  5,  1,  2,  5],
        [ 8,  9,  3,  9,  5],
        [ 8,  5,  3,  9,  6],
        [ 2,  7,  4,  7,  2],
        [ 5,  8,  5,  8,  5]]])

Example 4: Generating random numbers with seed

If one wants to reproduce the same random numbers, we need to use seed argument while creating the Random Generator object.

Here is an example of using seed to generate a single random number sampled from Poission distribution.

rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5)

5

Using the same seed we can reproduce the random number.

rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5)

5

We can also use seed to generate an array of random numbers sampled from Poission distribution.

rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5, size=10)

array([5, 5, 8, 6, 0, 7, 7, 4, 8, 5])

And we reproduce the same random array using the seed again in the generator object.

rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5, size=10)

array([5, 5, 8, 6, 0, 7, 7, 4, 8, 5])

The third example for using seed argument shows how one can generate a matrix of random numbers and reproduce it.

rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5, size=(5,5))

array([[5, 5, 8, 6, 0],
       [7, 7, 4, 8, 5],
       [6, 3, 4, 4, 6],
       [3, 5, 6, 6, 6],
       [2, 7, 3, 6, 2]])
rng = np.random.default_rng(seed=1234)
rng.poisson(lam=5, size=(5,5))

array([[5, 5, 8, 6, 0],
       [7, 7, 4, 8, 5],
       [6, 3, 4, 4, 6],
       [3, 5, 6, 6, 6],
       [2, 7, 3, 6, 2]])

Example 4: Generating random numbers from multiple poison distribution

Numpy’s poisson() function can also generate rando numbers from Poisson distribution with two different lambdas. In the example below we use a tuple with. two elements for lam argument. And this will generate 2d array with first column having Poisson distribution using the first lam and the second column using the second lam argument.

rng.poisson(lam=(100., 500.), size=(10, 2))

array([[ 95, 463],
       [ 93, 481],
       [ 98, 515],
       [101, 487],
       [ 87, 509],
       [ 86, 524],
       [117, 468],
       [ 86, 503],
       [101, 538],
       [ 81, 498]])