Python 模拟伯努利试验和二项分布
生活随笔
收集整理的這篇文章主要介紹了
Python 模拟伯努利试验和二项分布
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、模擬 27 次投擲硬幣的伯努利試驗
代碼:
from scipy import stats import numpy as np p = 0.5 # 生成凍結分布函數 bernoulliDist = stats.bernoulli(p) # 模擬 27 次伯努利實驗 trails = bernoulliDist.rvs(27) # 查看結果 trails
2、模擬二項分布
代碼
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
Ps = [0.5, 0.6, 0.7]
Ns = [20, 20, 20]
colors = ['blue', 'green', 'red']
# 模擬試驗繪制圖形
for p,n, c in zip(Ps, Ns, colors):
binomDist = stats.binom(n, p)
P_k = binomDist.pmf(np.arange(n + 1))
label='p={},n={}'.format(p, n)
plt.plot(P_k, '--',marker='o', label=label, ms=5)
plt.xlabel('X')
plt.ylabel('P(X)')
plt.legend()
plt.show()
圖形
。。。
總結
以上是生活随笔為你收集整理的Python 模拟伯努利试验和二项分布的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3ds Max如何结合PS制作一个全新又
- 下一篇: std::reserve和std::re