国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python如何生成各種隨機分布圖

2020-01-04 14:38:36
字體:
來源:轉載
供稿:網友

在學習生活中,我們經常性的發現有很多事物背后都有某種規律,而且,這種規律可能符合某種隨機分布,比如:正態分布、對數正態分布、beta分布等等。

所以,了解某種分布對一些事物有更加深入的理解并能清楚的闡釋事物的規律性。現在,用python產生一組隨機數據,來演示這些分布:

import randomimport matplotlibimport matplotlib.pyplot as pltSAMPLE_SIZE = 1000buckets = 100fig = plt.figure()matplotlib.rcParams.update({"font.size": 7})#第一個圖形是在[0,1)之間分布的隨機變量(normal distributed random variable)。ax = fig.add_subplot(5,2,1)ax.set_xlabel("random.random")res = [random.random() for _ in xrange(1, SAMPLE_SIZE)]ax.hist(res, buckets)#第二個圖形是一個均勻分布的隨機變量(uniformly distributed random variable)。ax_2 = fig.add_subplot(5,2,2)ax_2.set_xlabel("random.uniform")a = 1b = SAMPLE_SIZEres_2 = [random.uniform(a, b) for _ in xrange(1, SAMPLE_SIZE)]ax_2.hist(res_2, buckets)#第三個圖形是一個三角形分布(triangular distribution)。ax_3 = fig.add_subplot(5,2,3)ax_3.set_xlabel("random.triangular")low = 1high = SAMPLE_SIZEres_3 = [random.uniform(low, high) for _ in xrange(1, SAMPLE_SIZE)]ax_3.hist(res_3, buckets)#第四個圖形是一個beta分布(beta distribution)。參數的條件是alpha 和 beta 都要大于0, 返回值在0~1之間。plt.subplot(5,2,4)plt.xlabel("random.betavariate")alpha = 1beta = 10res_4 = [random.betavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_4, buckets)#第五個圖形是一個指數分布(exponential distribution)。 lambd 的值是 1.0 除以期望的中值,是一個不為零的數(參數應該叫做lambda沒但它是python的一個保留字)。如果lambd是整數,返回值的范圍是零到正無窮大;如果lambd為負,返回值的范圍是負無窮大到零。plt.subplot(5,2,5)plt.xlabel("random.expovariate")lambd = 1.0/ ((SAMPLE_SIZE + 1) / 2.)res_5 = [random.expovariate(lambd) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_5, buckets)#第六個圖形是gamma分布(gamma distribution), 要求參數alpha 和beta都大于零。plt.subplot(5,2,6)plt.xlabel("random.gammavariate")alpha = 1beta = 10res_6 = [random.gammavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_6, buckets)#第七個圖形是對數正態分布(Log normal distribution)。如果取這個分布的自然對數,會得到一個中值為mu,標準差為sigma的正態分布。mu可以取任何值,sigma必須大于零。plt.subplot(5,2,7)plt.xlabel("random.lognormalvariate")mu = 1sigma = 0.5res_7 = [random.lognormvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_7, buckets)#第八個圖形是正態分布(normal distribution)。plt.subplot(5,2,8)plt.xlabel("random.normalvariate")mu = 1sigma = 0.5res_8 = [random.normalvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_8, buckets) #最后一個圖形是帕累托分布(Pareto distribution), alpha 是形狀參數。plt.subplot(5,2,9)plt.xlabel("random.normalvariate")alpha = 1res_9 = [random.paretovariate(alpha) for _ in xrange(1, SAMPLE_SIZE)]plt.hist(res_9, buckets)plt.show()

python,生成,隨機,分布圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 原平市| 岗巴县| 伊宁市| 铜山县| 安化县| 舟曲县| 正阳县| 尉犁县| 广平县| 军事| 石景山区| 闽侯县| 徐汇区| 崇阳县| 高清| 武汉市| 陕西省| 海城市| 威海市| 托克托县| 徐州市| 乐业县| 紫金县| 云安县| 湖北省| 宁蒗| 屯门区| 高邮市| 清河县| 略阳县| 漳浦县| 屏东市| 武山县| 岳阳市| 四平市| 英吉沙县| 灵山县| 靖边县| 从化市| 河池市| 卢龙县|