Python模擬伯努利試驗和二項分布代碼實例
更新時間:2020年05月27日 11:07:02 作者:百里希文
這篇文章主要介紹了Python模擬伯努利試驗和二項分布代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、模擬 27 次投擲硬幣的伯努利試驗
代碼:
from scipy import stats import numpy as np p = 0.5 # 生成凍結分布函數(shù) 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()
結果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python pyautogui手動活動(模擬鼠標鍵盤)自動化庫使用
這篇文章主要為大家介紹了python pyautogui手動活動(模擬鼠標鍵盤)自動化庫使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01利用python實現(xiàn)JSON文檔與Python對象互相轉換
這篇文章主要介紹了利用python實現(xiàn)JSON文檔與Python對象互相轉換,通過對將一個JSON文檔映射為Python對象問題的展開介紹主題內(nèi)容,需要的朋友可以參考一下2022-06-06