python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)...

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from pylab import mpl
#伯努利分布
X1 = np.arange(0,2,1)
X1
array([0, 1])
p1 = 0.5 # 硬币朝上的概率
pList1 = stats.bernoulli.pmf(X1,p1)
pList1
array([0.5, 0.5])
plt.plot(X1,pList1,marker='o',linestyle='None')
[]

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第1张图片
plt.vlines(X1,0,pList1)
plt.xlabel('随机变量:抛1次硬币')
plt.ylabel('概率')
plt.title('伯努利分布:p=%.2f' % p1)
plt.show()

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第2张图片
#二项分布
n2 = 5    # 做某件事情的次数
p2 = 0.5  # 做某件事情成功的概率(抛硬币正面朝上的概率)
X2 = np.arange(0,n2+1,1) # 做某件事成功的次数(抛硬币正面朝上的次数)
X2
array([0, 1, 2, 3, 4, 5])
pList2 = stats.binom.pmf(X2,n2,p2)
pList2
array([0.03125, 0.15625, 0.3125 , 0.3125 , 0.15625, 0.03125])
plt.plot(X2,pList2,marker='o',linestyle='None')
[]

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第3张图片
plt.vlines(X2,0,pList2)
plt.xlabel('随机变量:抛硬币正面朝上的次数')
plt.ylabel('概率')
plt.title('二项分布:n=%i,p=%.2f' % (n2,p2))
plt.show()

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第4张图片
#几何分布
n2 = 5    # 做某件事情的次数
p2 = 0.5  # 做某件事情成功的概率(抛硬币正面朝上的概率)
X2 = np.arange(0,n2+1,1) # 做某件事成功的次数(抛硬币正面朝上的次数)
X2
array([0, 1, 2, 3, 4, 5])
pList2 = stats.binom.pmf(X2,n2,p2)
pList2
array([0.03125, 0.15625, 0.3125 , 0.3125 , 0.15625, 0.03125])
plt.plot(X2,pList2,marker='o',linestyle='None')
[]

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第5张图片
plt.vlines(X2,0,pList2)
plt.xlabel('随机变量:抛硬币正面朝上的次数')
plt.ylabel('概率')
plt.title('几何分布:n=%i,p=%.2f' % (n2,p2))
plt.show()

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第6张图片
#泊松分布
mu4 = 2  # 平均值:每天发生2次事故
k4 = 4   # 次数,现在想知道每天发生4次事故的概率
X4 = np.arange(0,k4+1,1)
X4
array([0, 1, 2, 3, 4])
pList4 = stats.poisson.pmf(X4,mu4)
pList4
print('X4:',X4)
print('pList4',pList4)
X4: [0 1 2 3 4]
pList4 [0.13533528 0.27067057 0.27067057 0.18044704 0.09022352]
plt.plot(X4,pList4,marker='o',linestyle='None')
plt.vlines(X4,0,pList4)
plt.xlabel('某路口发生k次事故')
plt.ylabel('概率')
plt.title('泊松分布:平均值mu=%i' % mu4 )
plt.show()

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第7张图片
#正态分布
mu5 = 0   # 平均值
sigma = 1 # 标准差
X5 = np.arange(-5,5,0.1)
X5
array([-5.00000000e+00, -4.90000000e+00, -4.80000000e+00, -4.70000000e+00,
       -4.60000000e+00, -4.50000000e+00, -4.40000000e+00, -4.30000000e+00,
       -4.20000000e+00, -4.10000000e+00, -4.00000000e+00, -3.90000000e+00,
       -3.80000000e+00, -3.70000000e+00, -3.60000000e+00, -3.50000000e+00,
       -3.40000000e+00, -3.30000000e+00, -3.20000000e+00, -3.10000000e+00,
       -3.00000000e+00, -2.90000000e+00, -2.80000000e+00, -2.70000000e+00,
       -2.60000000e+00, -2.50000000e+00, -2.40000000e+00, -2.30000000e+00,
       -2.20000000e+00, -2.10000000e+00, -2.00000000e+00, -1.90000000e+00,
       -1.80000000e+00, -1.70000000e+00, -1.60000000e+00, -1.50000000e+00,
       -1.40000000e+00, -1.30000000e+00, -1.20000000e+00, -1.10000000e+00,
       -1.00000000e+00, -9.00000000e-01, -8.00000000e-01, -7.00000000e-01,
       -6.00000000e-01, -5.00000000e-01, -4.00000000e-01, -3.00000000e-01,
       -2.00000000e-01, -1.00000000e-01, -1.77635684e-14,  1.00000000e-01,
        2.00000000e-01,  3.00000000e-01,  4.00000000e-01,  5.00000000e-01,
        6.00000000e-01,  7.00000000e-01,  8.00000000e-01,  9.00000000e-01,
        1.00000000e+00,  1.10000000e+00,  1.20000000e+00,  1.30000000e+00,
        1.40000000e+00,  1.50000000e+00,  1.60000000e+00,  1.70000000e+00,
        1.80000000e+00,  1.90000000e+00,  2.00000000e+00,  2.10000000e+00,
        2.20000000e+00,  2.30000000e+00,  2.40000000e+00,  2.50000000e+00,
        2.60000000e+00,  2.70000000e+00,  2.80000000e+00,  2.90000000e+00,
        3.00000000e+00,  3.10000000e+00,  3.20000000e+00,  3.30000000e+00,
        3.40000000e+00,  3.50000000e+00,  3.60000000e+00,  3.70000000e+00,
        3.80000000e+00,  3.90000000e+00,  4.00000000e+00,  4.10000000e+00,
        4.20000000e+00,  4.30000000e+00,  4.40000000e+00,  4.50000000e+00,
        4.60000000e+00,  4.70000000e+00,  4.80000000e+00,  4.90000000e+00])
y = stats.norm.pdf(X5,mu5,sigma)
print('X5:',X5)
print('y:',y)
X5: [-5.00000000e+00 -4.90000000e+00 -4.80000000e+00 -4.70000000e+00
 -4.60000000e+00 -4.50000000e+00 -4.40000000e+00 -4.30000000e+00
 -4.20000000e+00 -4.10000000e+00 -4.00000000e+00 -3.90000000e+00
 -3.80000000e+00 -3.70000000e+00 -3.60000000e+00 -3.50000000e+00
 -3.40000000e+00 -3.30000000e+00 -3.20000000e+00 -3.10000000e+00
 -3.00000000e+00 -2.90000000e+00 -2.80000000e+00 -2.70000000e+00
 -2.60000000e+00 -2.50000000e+00 -2.40000000e+00 -2.30000000e+00
 -2.20000000e+00 -2.10000000e+00 -2.00000000e+00 -1.90000000e+00
 -1.80000000e+00 -1.70000000e+00 -1.60000000e+00 -1.50000000e+00
 -1.40000000e+00 -1.30000000e+00 -1.20000000e+00 -1.10000000e+00
 -1.00000000e+00 -9.00000000e-01 -8.00000000e-01 -7.00000000e-01
 -6.00000000e-01 -5.00000000e-01 -4.00000000e-01 -3.00000000e-01
 -2.00000000e-01 -1.00000000e-01 -1.77635684e-14  1.00000000e-01
  2.00000000e-01  3.00000000e-01  4.00000000e-01  5.00000000e-01
  6.00000000e-01  7.00000000e-01  8.00000000e-01  9.00000000e-01
  1.00000000e+00  1.10000000e+00  1.20000000e+00  1.30000000e+00
  1.40000000e+00  1.50000000e+00  1.60000000e+00  1.70000000e+00
  1.80000000e+00  1.90000000e+00  2.00000000e+00  2.10000000e+00
  2.20000000e+00  2.30000000e+00  2.40000000e+00  2.50000000e+00
  2.60000000e+00  2.70000000e+00  2.80000000e+00  2.90000000e+00
  3.00000000e+00  3.10000000e+00  3.20000000e+00  3.30000000e+00
  3.40000000e+00  3.50000000e+00  3.60000000e+00  3.70000000e+00
  3.80000000e+00  3.90000000e+00  4.00000000e+00  4.10000000e+00
  4.20000000e+00  4.30000000e+00  4.40000000e+00  4.50000000e+00
  4.60000000e+00  4.70000000e+00  4.80000000e+00  4.90000000e+00]
y: [1.48671951e-06 2.43896075e-06 3.96129909e-06 6.36982518e-06
 1.01408521e-05 1.59837411e-05 2.49424713e-05 3.85351967e-05
 5.89430678e-05 8.92616572e-05 1.33830226e-04 1.98655471e-04
 2.91946926e-04 4.24780271e-04 6.11901930e-04 8.72682695e-04
 1.23221917e-03 1.72256894e-03 2.38408820e-03 3.26681906e-03
 4.43184841e-03 5.95253242e-03 7.91545158e-03 1.04209348e-02
 1.35829692e-02 1.75283005e-02 2.23945303e-02 2.83270377e-02
 3.54745928e-02 4.39835960e-02 5.39909665e-02 6.56158148e-02
 7.89501583e-02 9.40490774e-02 1.10920835e-01 1.29517596e-01
 1.49727466e-01 1.71368592e-01 1.94186055e-01 2.17852177e-01
 2.41970725e-01 2.66085250e-01 2.89691553e-01 3.12253933e-01
 3.33224603e-01 3.52065327e-01 3.68270140e-01 3.81387815e-01
 3.91042694e-01 3.96952547e-01 3.98942280e-01 3.96952547e-01
 3.91042694e-01 3.81387815e-01 3.68270140e-01 3.52065327e-01
 3.33224603e-01 3.12253933e-01 2.89691553e-01 2.66085250e-01
 2.41970725e-01 2.17852177e-01 1.94186055e-01 1.71368592e-01
 1.49727466e-01 1.29517596e-01 1.10920835e-01 9.40490774e-02
 7.89501583e-02 6.56158148e-02 5.39909665e-02 4.39835960e-02
 3.54745928e-02 2.83270377e-02 2.23945303e-02 1.75283005e-02
 1.35829692e-02 1.04209348e-02 7.91545158e-03 5.95253242e-03
 4.43184841e-03 3.26681906e-03 2.38408820e-03 1.72256894e-03
 1.23221917e-03 8.72682695e-04 6.11901930e-04 4.24780271e-04
 2.91946926e-04 1.98655471e-04 1.33830226e-04 8.92616572e-05
 5.89430678e-05 3.85351967e-05 2.49424713e-05 1.59837411e-05
 1.01408521e-05 6.36982518e-06 3.96129909e-06 2.43896075e-06]
plt.plot(X5,y)
plt.xlabel('随机变量:x')
plt.ylabel('概率:y')
plt.title('正态分布:$mu$=%.1f,$sigma^2$=%.1f' % (mu5,sigma))
plt.grid()
plt.show()

python 二项分布_Python实现概率分布(二项分布、伯努利分布、泊松分布、几何分布、正态分布等)..._第8张图片

你可能感兴趣的:(python,二项分布)