python如何生成各種隨機分布圖
在學習生活中,我們經(jīng)常性的發(fā)現(xiàn)有很多事物背后都有某種規(guī)律,而且,這種規(guī)律可能符合某種隨機分布,比如:正態(tài)分布、對數(shù)正態(tài)分布、beta分布等等。
所以,了解某種分布對一些事物有更加深入的理解并能清楚的闡釋事物的規(guī)律性?,F(xiàn)在,用python產(chǎn)生一組隨機數(shù)據(jù),來演示這些分布:
import random import matplotlib import matplotlib.pyplot as plt SAMPLE_SIZE = 1000 buckets = 100 fig = 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 = 1 b = SAMPLE_SIZE res_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 = 1 high = SAMPLE_SIZE res_3 = [random.uniform(low, high) for _ in xrange(1, SAMPLE_SIZE)] ax_3.hist(res_3, buckets) #第四個圖形是一個beta分布(beta distribution)。參數(shù)的條件是alpha 和 beta 都要大于0, 返回值在0~1之間。 plt.subplot(5,2,4) plt.xlabel("random.betavariate") alpha = 1 beta = 10 res_4 = [random.betavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)] plt.hist(res_4, buckets) #第五個圖形是一個指數(shù)分布(exponential distribution)。 lambd 的值是 1.0 除以期望的中值,是一個不為零的數(shù)(參數(shù)應該叫做lambda沒但它是python的一個保留字)。如果lambd是整數(shù),返回值的范圍是零到正無窮大;如果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), 要求參數(shù)alpha 和beta都大于零。 plt.subplot(5,2,6) plt.xlabel("random.gammavariate") alpha = 1 beta = 10 res_6 = [random.gammavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)] plt.hist(res_6, buckets) #第七個圖形是對數(shù)正態(tài)分布(Log normal distribution)。如果取這個分布的自然對數(shù),會得到一個中值為mu,標準差為sigma的正態(tài)分布。mu可以取任何值,sigma必須大于零。 plt.subplot(5,2,7) plt.xlabel("random.lognormalvariate") mu = 1 sigma = 0.5 res_7 = [random.lognormvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)] plt.hist(res_7, buckets) #第八個圖形是正態(tài)分布(normal distribution)。 plt.subplot(5,2,8) plt.xlabel("random.normalvariate") mu = 1 sigma = 0.5 res_8 = [random.normalvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)] plt.hist(res_8, buckets) #最后一個圖形是帕累托分布(Pareto distribution), alpha 是形狀參數(shù)。 plt.subplot(5,2,9) plt.xlabel("random.normalvariate") alpha = 1 res_9 = [random.paretovariate(alpha) for _ in xrange(1, SAMPLE_SIZE)] plt.hist(res_9, buckets) plt.show()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?matplotlib?seaborn繪圖教程詳解
Seaborn是在matplotlib的基礎上進行了更高級的API封裝,從而使得作圖更加容易,在大多數(shù)情況下使用seaborn就能做出很具有吸引力的圖。本文將詳細講解如何利用Seaborn繪制圖表,需要的可以參考一下2022-03-03Python+matplotlib+numpy實現(xiàn)在不同平面的二維條形圖
這篇文章主要介紹了Python+matplotlib+numpy實現(xiàn)在不同平面的二維條形圖,具有一定借鑒價值,需要的朋友可以參考下2018-01-01解決List.append()?在?Python?中不起作用的問題
在?Python?中,我們通常使用?List.append()?方法向列表末尾添加元素,然而,在某些情況下,你可能會遇到?List.append()?方法不起作用的問題,本文將詳細討論這個問題并提供解決方法,需要的朋友可以參考下2023-06-06Python?如何將?matplotlib?圖表集成進到PDF?中
這篇文章主要介紹了Python?如何將?matplotlib?圖表集成進到PDF?中,文章介紹內(nèi)容詳細,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學習有所幫助2022-03-03