在科學(xué)技術(shù)和機(jī)器學(xué)習(xí)等其他算法相關(guān)任務(wù)中,我們經(jīng)常需要用到隨機(jī)數(shù),為了把握隨機(jī)數(shù)的生成特性,從隨機(jī)數(shù)的隨機(jī)無序中獲得確定和秩序。我們可以利用隨機(jī)數(shù)種子(random seed)來實現(xiàn)這一目標(biāo),隨機(jī)數(shù)種子,可以使得引入了隨機(jī)數(shù)的整個程序,在多次運(yùn)行中得到確定的,一致的結(jié)果。
很多博文談到隨機(jī)數(shù)種子,只是簡單論及,利用隨機(jī)數(shù)種子,可以每次生成相同的隨機(jī)數(shù)。想真正用好掌握它,對此很容易產(chǎn)生疑惑,生成相同的隨機(jī)數(shù)數(shù)怎么個相同法?隨機(jī)數(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é)果,這說明了以下幾點:
以上幾點囊括了隨機(jī)數(shù)種子的基本特性,下面我們來對numpy中的隨機(jī)數(shù)種子作進(jìn)一步的拓展研究。
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模塊時相似的情形,進(jìn)一步驗證了我們總結(jié)的關(guān)于隨機(jī)數(shù)種子的特性。
此外,我們可以對多維隨機(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.2408932 1.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)行兩個一行的多維正態(tài)分布的結(jié)果,與一次運(yùn)行兩行的多維正態(tài)分布的結(jié)果的第一行完全相同。
這個結(jié)果,說明了對相同類型的隨機(jī)數(shù)分布,形狀特征不會影響分布的生成秩序,程序中,np.random.randn(1, 2),這一行不像是第二次運(yùn)行多維正態(tài)分布的隨機(jī)數(shù)組,它"幾乎"是后綴于它的前一行一次性生成的。
至此,我們對隨機(jī)數(shù)生成順序有了初步印象,但是這里的順序,其實比我們的樸素觀察更復(fù)雜,我們來進(jì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.2683282 1.33354538 -0.84272405]] [[ 1.76405235 0.40015721 0.97873798 2.2408932 1.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ù)組,實際上并不是,np.random.randn(2, 5)才是真正的第一次運(yùn)行多維正態(tài)分布隨機(jī)數(shù)組。
這說明,前面的np.random.random()對np.random.randn產(chǎn)生了干擾,使得這次正態(tài)分布的隨機(jī)數(shù)組中的任何一個數(shù),都不在np.random.randn(2, 5)中,這樣它顯示了一種不可把握的隨機(jī)性。
我們可以把這一點考察得更加深入一點:
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.2408932 1.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.2408932 1.86755799] [-0.97727788 0.95008842 -0.15135721 -0.10321885 0.4105985 ]]
緊接上面對隨機(jī)數(shù)干擾項對考察,我們看到,這次我們改變了干擾項隨機(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ā)生器。這一點,也不難在其他類型的隨機(jī)數(shù)分布中得到驗證。
到此這篇關(guān)于Python隨機(jī)數(shù)種子(random seed)的使用的文章就介紹到這了,更多相關(guān)Python隨機(jī)數(shù)種子內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:七臺河 許昌 濰坊 渭南 西安 贛州 雅安 辛集
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python隨機(jī)數(shù)種子(random seed)的使用》,本文關(guān)鍵詞 Python,隨機(jī)數(shù),種子,random,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。