np.random的幾種方法
numpy.random
DESCRIPTION ======================== Random Number Generation ======================== ==================== ========================================================= Utility functions ============================================================================== random_sample Uniformly distributed floats over ``[0, 1)``. random Alias for `random_sample`. bytes Uniformly distributed random bytes. random_integers Uniformly distributed integers in a given range. permutation Randomly permute a sequence / generate a random sequence. shuffle Randomly permute a sequence in place. seed Seed the random number generator. choice Random sample from 1-D array. ==================== ========================================================= ==================== ========================================================= Compatibility functions ============================================================================== rand Uniformly distributed values. randn Normally distributed values. ranf Uniformly distributed floating point numbers. randint Uniformly distributed integers in a given range. ==================== ========================================================= 解釋:
-
numpy.random.rand(d0, d1, ..., dn):生成一個[0,1)之間的隨機浮點數或N維浮點陣列。
-
numpy.random.randn(d0, d1, ..., dn):生成一個浮點數或N維浮點陣列,取數範圍:正態分佈的隨機樣本數。
-
numpy.random.randint(low, high=None, size=None, dtype='l'):生成一個整數或N維整數陣列,取數範圍:若high不為None時,取[low,high)之間隨機整數,否則取值[0,low)之間隨機整數。
-
numpy.random.random_integers(low, high=None, size=None):生成一個整數或一個N維整數陣列,取值範圍:若high不為None,則取[low,high]之間隨機整數,否則取[1,low]之間隨機整數。
-
numpy.random.random_sample(size=None):生成一個[0,1)之間隨機浮點數或N維浮點陣列。
-
numpy.random.choice(a, size=None, replace=True, p=None):從序列a中獲取元素,若a為整數,元素取值為np.arange(a)中隨機數;若a為陣列,取值為a陣列元素中隨機元素。replace=False表示不能重複,replace=True可以重複。p為與a長度相同的陣列,可以決定a中每個元素隨機出現的概率。p.sum()=1
-
numpy.random.shuffle(x):對X進行重排序,如果X為多維陣列,只沿第一條軸洗牌,輸出為None,改變原來的X
-
numpy.random.permutation(x):與numpy.random.shuffle(x)函式功能相同。輸出為X洗牌後的新陣列,原來的X不變。