Python隨機(jī)數(shù)種子(random seed)的使用
在科學(xué)技術(shù)和機(jī)器學(xué)習(xí)等其他算法相關(guān)任務(wù)中,我們經(jīng)常需要用到隨機(jī)數(shù),為了把握隨機(jī)數(shù)的生成特性,從隨機(jī)數(shù)的隨機(jī)無序中獲得確定和秩序。我們可以利用隨機(jī)數(shù)種子(random seed)來實(shí)現(xiàn)這一目標(biāo),隨機(jī)數(shù)種子,可以使得引入了隨機(jī)數(shù)的整個(gè)程序,在多次運(yùn)行中得到確定的,一致的結(jié)果。
很多博文談到隨機(jī)數(shù)種子,只是簡(jiǎn)單論及,利用隨機(jī)數(shù)種子,可以每次生成相同的隨機(jī)數(shù)。想真正用好掌握它,對(duì)此很容易產(chǎn)生疑惑,生成相同的隨機(jī)數(shù)數(shù)怎么個(gè)相同法?隨機(jī)數(shù)種子又作何用處?
1. 隨機(jī)數(shù)種子
下面我們從實(shí)例中揭開隨機(jī)數(shù)種子的神秘面紗:
import random # print(help(random)) def test_random_seed_in_std_lib(seed=0, cnt=3): random.seed(seed) print("test seed: ", seed) for _ in range(cnt): print(random.random()) print(random.randint(0,100)) print(random.uniform(1, 10)) print('\n') test_random_seed_in_std_lib() test seed: 0 0.8444218515250481 97 9.01219528753418 0.04048437818077755 65 5.373349269065314 0.9182343317851318 38 9.710199954281542 test_random_seed_in_std_lib() test seed: 0 0.8444218515250481 97 9.01219528753418 0.04048437818077755 65 5.373349269065314 0.9182343317851318 38 9.710199954281542 test_random_seed_in_std_lib(99) test seed: 99 0.40397807494366633 25 6.39495190686897 0.23026272839629136 17 7.8388969285727015 0.2511510083752201 49 5.777313434770537
通過兩次運(yùn)行以上程序,我們得到相同的結(jié)果,這說明了以下幾點(diǎn):
- 在確定了一次隨機(jī)數(shù)種子后,隨機(jī)數(shù)函數(shù),無論任何分布任何類型,在多次重復(fù)調(diào)用中(for循環(huán))生成的隨機(jī)數(shù)不同;
- 當(dāng)再次聲明相同的隨機(jī)數(shù)種子時(shí)(第二次調(diào)用test_random_seed_in_std_lib函數(shù),random.seed(seed)這一行),隨機(jī)數(shù)將從“頭”開始, 按相同的順序生成隨機(jī)數(shù)。這里的“頭”,即是random.seed(seed)聲明后,隨機(jī)數(shù)函數(shù)的首次調(diào)用;
- 若指定不同的隨機(jī)數(shù)種子(seed=99),無論任何隨機(jī)數(shù)函數(shù),生成的隨機(jī)數(shù)將不同于,之前的(隨機(jī)數(shù)種子為0)的運(yùn)行結(jié)果。
- 上面的幾點(diǎn)解釋了隨機(jī)數(shù)種子可以使得每次生成相同隨機(jī)數(shù)的具體含義。這里的相同,其實(shí)還有一種更普遍的內(nèi)涵,即環(huán)境獨(dú)立和跨平臺(tái)。上面的實(shí)驗(yàn),在任何電腦或主機(jī),運(yùn)行以上代碼,可以復(fù)現(xiàn)完全一致的結(jié)果。
以上幾點(diǎn)囊括了隨機(jī)數(shù)種子的基本特性,下面我們來對(duì)numpy中的隨機(jī)數(shù)種子作進(jìn)一步的拓展研究。
2. numpy中的隨機(jī)數(shù)種子
import numpy as np def test_numpy_random_seed(seed=0, cnt=3): np.random.seed(seed) print("test numpy seed: ", seed) for _ in range(cnt): print(np.random.random()) print(np.random.randn(1, 5)) print(np.random.uniform(1, 10, 5)) print('\n')
多次運(yùn)行以上的test_numpy_random_seed函數(shù),你可以觀察到與使用random模塊時(shí)相似的情形,進(jìn)一步驗(yàn)證了我們總結(jié)的關(guān)于隨機(jī)數(shù)種子的特性。
此外,我們可以對(duì)多維隨機(jī)數(shù)組做一些有益的探索:
def test_mult_shape(seed=0): np.random.seed(seed) print(np.random.randn(1, 3)) print(np.random.randn(1, 2)) np.random.seed(seed) print(np.random.randn(2, 5)) test_mult_shape() [[1.76405235 0.40015721 0.97873798]] [[2.2408932 1.86755799]] [[ 1.76405235 0.40015721 0.97873798 2.24089321.86755799] [-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
運(yùn)行test_mult_shape函數(shù),我們發(fā)現(xiàn),設(shè)定相同的隨機(jī)數(shù)組,兩次運(yùn)行兩個(gè)一行的多維正態(tài)分布的結(jié)果,與一次運(yùn)行兩行的多維正態(tài)分布的結(jié)果的第一行完全相同。
這個(gè)結(jié)果,說明了對(duì)相同類型的隨機(jī)數(shù)分布,形狀特征不會(huì)影響分布的生成秩序,程序中,np.random.randn(1, 2),這一行不像是第二次運(yùn)行多維正態(tài)分布的隨機(jī)數(shù)組,它"幾乎"是后綴于它的前一行一次性生成的。
3. 隨機(jī)數(shù)“順序”的奧秘
至此,我們對(duì)隨機(jī)數(shù)生成順序有了初步印象,但是這里的順序,其實(shí)比我們的樸素觀察更復(fù)雜,我們來進(jìn)一步考察這一點(diǎn)。
def test_numpy_random_seed_order(seed=0): np.random.seed(seed) print(np.random.random()) # print(np.random.randint(1, 10)) print(np.random.randn(1, 5)) np.random.seed(seed) print(np.random.randn(2, 5)) test_numpy_random_seed_order() 0.5488135039273248 [[ 0.74159174 1.55291372 -2.26832821.33354538 -0.84272405]] [[ 1.76405235 0.40015721 0.97873798 2.24089321.86755799] [-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
運(yùn)行以上程序,我們看到,設(shè)定了相同的隨機(jī)數(shù)種子,np.random.randn(1, 5)看起來是第一次運(yùn)行多維正態(tài)分布數(shù)組,實(shí)際上并不是,np.random.randn(2, 5)才是真正的第一次運(yùn)行多維正態(tài)分布隨機(jī)數(shù)組。
這說明,前面的np.random.random()對(duì)np.random.randn產(chǎn)生了干擾,使得這次正態(tài)分布的隨機(jī)數(shù)組中的任何一個(gè)數(shù),都不在np.random.randn(2, 5)中,這樣它顯示了一種不可把握的隨機(jī)性。
我們可以把這一點(diǎn)考察得更加深入一點(diǎn):
def test_numpy_random_seed_order_further(seed=0, randint_high=10): np.random.seed(seed) print(np.random.randint(1, randint_high)) print(np.random.randn(1, 5)) np.random.seed(seed) print(np.random.randn(2, 5)) test_numpy_random_seed_order_further() 6 [[ 0.11849646 0.11396779 0.37025538 1.04053075 -1.51698273]] [[ 1.76405235 0.40015721 0.97873798 2.24089321.86755799] [-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]] test_numpy_random_seed_order_further(randint_high=5) 1 [[ 1.12279492 0.30280522 0.07085926 0.07304142 -1.42232584]] [[ 1.76405235 0.40015721 0.97873798 2.24089321.86755799] [-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
緊接上面對(duì)隨機(jī)數(shù)干擾項(xiàng)對(duì)考察,我們看到,這次我們改變了干擾項(xiàng)隨機(jī)數(shù)生成器,np.random.randn(1, 5)的生成結(jié)果不同于test_numpy_random_seed_order中同一行的運(yùn)行結(jié)果。
另外,兩次設(shè)置不同的randint的右邊界,np.random.randn(1, 5)生成的結(jié)果也全然不同,這說明了np.random.randint設(shè)置不同的參數(shù),即是全然不同的隨機(jī)數(shù)發(fā)生器。這一點(diǎn),也不難在其他類型的隨機(jī)數(shù)分布中得到驗(yàn)證。
到此這篇關(guān)于Python隨機(jī)數(shù)種子(random seed)的使用的文章就介紹到這了,更多相關(guān)Python隨機(jī)數(shù)種子內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。