Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Chi-Square Distribution in NumPy
Next article icon

The Poisson Distribution model the number of times an event happens within a fixed time or space when we know the average number of occurrences. It is used for events that occur independently such as customer arrivals at a store, Website clicks where events happen independently.

numpy.random.poisson() Method

In Python'sNumPylibrary we can generate random numbers following a Poisson Distribution using thenumpy.random.poisson() method. It has two key parameters:

  • lam: The average number of events (λ) expected to occur in the interval.
  • size: The shape of the returned array.

Syntax:

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

Example 1: Generate a Single Random Number

To generate a single random number from a Poisson Distribution with an average rate of λ = 5:

Python
importnumpyasnprandom_number=np.random.poisson(lam=5)print(random_number)

Output :

5

Example 2: Generate an Array of Random Numbers

To generate multiple random numbers:

Python
random_numbers=np.random.poisson(lam=5,size=5)print(random_numbers)

Output :

[13 6 4 4 10]

Visualizing the Poisson Distribution

To understand the distribution better we can visualize the generated numbers. Here is an example of plotting a histogram of random numbers generated usingnumpy.random.poisson.

Python
importnumpyasnpfromnumpyimportrandomimportmatplotlib.pyplotaspltimportseabornassnslam=2size=1000data=random.poisson(lam=lam,size=size)sns.displot(data,kde=False,bins=np.arange(-0.5,max(data)+1.5,1),color='skyblue',edgecolor='black')plt.title(f"Poisson Distribution (λ={lam})")plt.xlabel("Number of Events")plt.ylabel("Frequency")plt.grid(True)plt.show()

Output:

poisson-distribution
Poisson Distribution

The image shows a Poisson Distribution withλ=2 displaying the frequency of events. The histogram represents simulated data highlighting the peak at 0 and 1 events, with frequencies decreasing as the number of events increases.



Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp