Hướng dẫn dùng poissonian distribution python

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

Draw samples from a Poisson distribution.

Nội dung chính

  • Phân phối xác suất Python là gì?
  • Làm thế nào để triển khai phân phối xác suất Python?
  • Một. Phân phối Chuẩn trong Python 
  • Phân phối nhị thức trong Python
  • Phân phối Poisson bằng Python
  • Phân phối Bernoulli bằng Python
  • Kết luận
  • How to Generate a Poisson Distribution
  • How to Calculate Probabilities Using a Poisson Distribution
  • How to Plot a Poisson Distribution
  • Additional Resources

The Poisson distribution is the limit of the binomial distribution for large N.

Note

New code should use the poisson method of a default_rng() instance instead; please see the Quick Start.

Parameterslamfloat or array_like of floats

Expected number of events occurring in a fixed-time interval, must be >= 0. A sequence must be broadcastable over the requested size.

sizeint or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if lam is a scalar. Otherwise, np.array(lam).size samples are drawn.

Returnsoutndarray or scalar

Drawn samples from the parameterized Poisson distribution.

Notes

The Poisson distribution

\[f(k; \lambda)=\frac{\lambda^k e^{-\lambda}}{k!}\]

For events with an expected separation \(\lambda\) the Poisson distribution \(f(k; \lambda)\) describes the probability of \(k\) events occurring within the observed interval \(\lambda\).

Because the output is limited to the range of the C int64 type, a ValueError is raised when lam is within 10 sigma of the maximum representable value.

References

1

Weisstein, Eric W. “Poisson Distribution.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/PoissonDistribution.html

2

Wikipedia, “Poisson distribution”, https://en.wikipedia.org/wiki/Poisson_distribution

Examples

Draw samples from the distribution:

>>> import numpy as np
>>> s = np.random.poisson(5, 10000)

Display histogram of the sample:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 14, density=True)
>>> plt.show()

Hướng dẫn dùng poissonian distribution python

Draw each 100 values for lambda 100 and 500:

>>> s = np.random.poisson(lam=(100., 500.), size=(100, 2))

Sau khi nghiên cứu thống kê mô tả trong Python , bây giờ chúng ta sẽ khám phá 4 phân phối xác suất chính của Python: Phân phối bình thường, nhị thức, Poisson và Bernoulli trong Python. Hơn nữa, chúng ta sẽ học cách triển khai các phân phối xác suất Python này với Lập trình Python.

Nội dung chính

  • Phân phối xác suất Python là gì?
  • Làm thế nào để triển khai phân phối xác suất Python?
  • Một. Phân phối Chuẩn trong Python 
  • Phân phối nhị thức trong Python
  • Phân phối Poisson bằng Python
  • Phân phối Bernoulli bằng Python
  • Kết luận
  • How to Generate a Poisson Distribution
  • How to Calculate Probabilities Using a Poisson Distribution
  • How to Plot a Poisson Distribution
  • Additional Resources

Nội dung chính

  • Phân phối xác suất Python là gì?
  • Làm thế nào để triển khai phân phối xác suất Python?
  • Một. Phân phối Chuẩn trong Python 
  • Phân phối nhị thức trong Python
  • Phân phối Poisson bằng Python
  • Phân phối Bernoulli bằng Python
  • Kết luận

Nội dung chính

  • Phân phối xác suất Python là gì?
  • Làm thế nào để triển khai phân phối xác suất Python?
  • Một. Phân phối Chuẩn trong Python 
  • Phân phối nhị thức trong Python
  • Phân phối Poisson bằng Python
  • Phân phối Bernoulli bằng Python
  • Kết luận

Các bài viết liên quan:

Phân phối xác suất Python là gì?

Phân phối xác suất là một hàm theo lý thuyết xác suất và thống kê – một hàm cho chúng ta biết các kết quả khác nhau có thể xảy ra như thế nào trong một thử nghiệm. Nó mô tả các sự kiện dưới dạng xác suất của chúng; điều này nằm ngoài tất cả các kết quả có thể xảy ra. Hãy lấy phân phối xác suất của một lần tung đồng xu công bằng. Ở đây, đầu nhận giá trị X = 0,5 và đuôi cũng nhận X = 0,5.

Hai lớp của phân phối như vậy là rời rạc và liên tục. Cái trước được biểu thị bằng hàm khối lượng xác suất và cái sau được biểu thị bằng hàm mật độ xác suất.

Xem thêm Địa chỉ nhị phân của bộ nhớ máy tính trong hệ điều hành

Làm thế nào để triển khai phân phối xác suất Python?

Hãy triển khai các loại Phân phối xác suất Python này, hãy xem chúng:

Một. Phân phối Chuẩn trong Python 

Phân phối chuẩn trong Python là một hàm phân phối các biến ngẫu nhiên trong một đồ thị có hình dạng như một cái chuông đối xứng. Nó làm như vậy bằng cách sắp xếp phân phối xác suất cho mỗi giá trị. Hãy sử dụng Python numpy cho việc này.

import scipy.stats # thêm thư viện scipy.stats
import numpy as np # thêm thư viện numpy
import matplotlib.pyplot as plt # thêm thư viện matplotlib.pyplot
np.random.seed(1234) # random mẫu
samples=np.random.lognormal(mean=1.,sigma=.4,size=10000) # khai báo mẫu phân phối chuẩn
shape,loc,scale=scipy.stats.lognorm.fit(samples,floc=0) # khai báo 3 biến shape,loc,scale
num_bins=50
clr="#EFEFEF"
counts,edges,patches=plt.hist(samples,bins=num_bins,color=clr)
centers=0.5*(edges[:-1]+edges[1:])
cdf=scipy.stats.lognorm.cdf(edges,shape,loc=loc,scale=scale)
prob=np.diff(cdf)
plt.plot(centers,samples.size*prob,'k-',linewidth=2) # vẽ phân phối chuẩn

Phân phối nhị thức trong Python

Phân phối nhị thức Python cho chúng ta biết xác suất tần suất thành công trong n thử nghiệm độc lập. Những thí nghiệm như vậy là câu hỏi có-không. Một ví dụ có thể là tung đồng xu.

import seaborn # thêm thư viện seaborn
from scipy.stats import binom #khai báo thư viện binom
data=binom.rvs(n=17,p=0.7,loc=0,size=1010) # tạo mẫu phân bố nhị thức
ax=seaborn.distplot(data,
                kde=True,
                color='pink',
                hist_kws={"linewidth": 22,'alpha':0.77}) # vẽ ra phân bố
ax.set(xlabel='Binomial',ylabel='Frequency')

Phân phối Poisson bằng Python

Phân phối Python Poisson cho chúng ta biết về khả năng xảy ra một số sự kiện nhất định trong một khoảng thời gian hoặc không gian cố định. Điều này giả định rằng những sự kiện này xảy ra với tốc độ không đổi và cũng không phụ thuộc vào sự kiện cuối cùng.

Xem thêm Phân phối Binomial và Poisson trong R

import numpy as np # thêm thư viện numpy
s=np.random.poisson(5, 10000) # tạo ngẫu nghiên phân phối poisson
import matplotlib.pyplot as plt # thêm thư viên với plt
plt.hist(s,16,normed=True,color='Green')

Phân phối Bernoulli bằng Python

Phân phối Python Bernoulli là một trường hợp của phân phối nhị thức trong đó chúng tôi tiến hành một thử nghiệm duy nhất. Đây là một phân phối xác suất rời rạc với xác suất p cho giá trị 1 và xác suất q = 1-p cho giá trị 0. p có thể là thành công, đúng, đúng hoặc một. Tương tự, q = 1-p có thể là fail, no, false hoặc zero.

s=np.random.binomial(10,0.5,1000) # tạo ngẫu nhiên mẫu binomial
plt.hist(s,16,normed=True,color='Brown')

Vì vậy, đây là tất cả về Phân phối xác suất Python. Hy vọng bạn thích giải thích của chúng tôi.

Kết luận

Do đó, chúng tôi đã nghiên cứu Phân phối xác suất Python và 4 loại của nó với một ví dụ. Ngoài ra, chúng tôi đã học cách triển khai các phân phối xác suất Python này. Hơn nữa, nếu bạn có bất kỳ nghi ngờ nào, hãy hỏi trong phần bình luận.

Xem thêm Normal Distribution- Phân phối chuẩn trong R


The Poisson distribution describes the probability of obtaining k successes during a given time interval.

Nội dung chính

  • How to Generate a Poisson Distribution
  • How to Calculate Probabilities Using a Poisson Distribution
  • How to Plot a Poisson Distribution
  • Additional Resources

Nội dung chính

  • How to Generate a Poisson Distribution
  • How to Calculate Probabilities Using a Poisson Distribution
  • How to Plot a Poisson Distribution
  • Additional Resources

If a random variable X follows a Poisson distribution, then the probability that X = k successes can be found by the following formula:

P(X=k) = λk * e– λ / k!

where:

  • λ: mean number of successes that occur during a specific interval
  • k: number of successes
  • e: a constant equal to approximately 2.71828

This tutorial explains how to use the Poisson distribution in Python.

How to Generate a Poisson Distribution

You can use the poisson.rvs(mu, size) function to generate random values from a Poisson distribution with a specific mean value and sample size:

from scipy.stats import poisson

#generate random values from Poisson distribution with mean=3 and sample size=10
poisson.rvs(mu=3, size=10)

array([2, 2, 2, 0, 7, 2, 1, 2, 5, 5])

How to Calculate Probabilities Using a Poisson Distribution

You can use the poisson.pmf(k, mu) and poisson.cdf(k, mu) functions to calculate probabilities related to the Poisson distribution.

Example 1: Probability Equal to Some Value

A store sells 3 apples per day on average. What is the probability that they will sell 5 apples on a given day? 

from scipy.stats import poisson

#calculate probability
poisson.pmf(k=5, mu=3)

0.100819

The probability that the store sells 5 apples in a given day is 0.100819.

Example 2: Probability Less than Some Value

A certain store sells seven footballs per day on average. What is the probability that this store sells four or less footballs in a given day?

from scipy.stats import poisson

#calculate probability
poisson.cdf(k=4, mu=7)

0.172992

The probability that the store sells four or less footballs in a given day is 0.172992.

Example 3: Probability Greater than Some Value

A certain store sells 15 cans of tuna per day on average. What is the probability that this store sells more than 20 cans of tuna in a given day?

from scipy.stats import poisson

#calculate probability
1-poisson.cdf(k=20, mu=15)

0.082971

The probability that the store sells more than 20 cans of tuna in a given day is 0.082971.

How to Plot a Poisson Distribution

You can use the following syntax to plot a Poisson distribution with a given mean:

from scipy.stats import poisson
import matplotlib.pyplot as plt

#generate Poisson distribution with sample size 10000
x = poisson.rvs(mu=3, size=10000)

#create plot of Poisson distribution
plt.hist(x, density=True, edgecolor='black')

Additional Resources

An Introduction to the Poisson Distribution
5 Real-Life Examples of the Poisson Distribution
Online Poisson Distribution Calculator