在近期的tensorflow學習中,我發現,numpy作為python的數學運算庫,學習tensorflow過程中經常需要用到,而numpy的random函式功能很多,每次用的時候都需要另行google,所以我決定將它的常用用法彙總一下。
0. first of all
import numpy as numpy
既然是講隨機數,眾所周知,計算機世界的隨機數都是偽隨機,都有一個叫做種子(seed)的東西
numpy.random.seed(seed=None)
可以通過輸入int或arrat_like來使得隨機的結果固定
>>> np.random.rand(3, 3)
array([[0.43267997, 0.72368429, 0.72366367],
[0.28496145, 0.44920635, 0.8924199 ],
[0.31974178, 0.55658518, 0.01755763]])
>>> np.random.rand(3, 3)
array([[0.75196574, 0.33708946, 0.64345504],
[0.85048542, 0.18109553, 0.69524277],
[0.06390142, 0.30589554, 0.51643863]])
>>> np.random.seed(5)
>>> np.random.rand(3, 3)
array([[0.22199317, 0.87073231, 0.20671916],
[0.91861091, 0.48841119, 0.61174386],
[0.76590786, 0.51841799, 0.2968005 ]])
>>> np.random.seed(5)
>>> np.random.rand(3, 3)
rray([[0.22199317, 0.87073231, 0.20671916],
[0.91861091, 0.48841119, 0.61174386],
[0.76590786, 0.51841799, 0.2968005 ]])
1. numpy.random.rand()
numpy.random.rand(d0,d1...dn)
- rand函式根據給定維度生成半開區間[0,1)之間的資料,包含0,不包含1
- dn表示每個維度
- 返回值為指定緯度的numpy.ndarray
>>> np.random.rand(3, 3) # shape: 3*3
array([[0.94340617, 0.96183216, 0.88510322],
[0.44543261, 0.74930098, 0.73372814],
[0.29233667, 0.3940114 , 0.7167332 ]])
>>> np.random.rand(3, 3, 3) # shape: 3*3*3
array([[[0.64794467, 0.17450186, 0.01016758],
[0.36435826, 0.37682548, 0.19501414],
[0.26438152, 0.28520726, 0.01617747]],
[[0.43803165, 0.4096238 , 0.77309074],
[0.42280405, 0.02623488, 0.82081416],
[0.7611891 , 0.84823656, 0.64481959]],
[[0.24420439, 0.62015463, 0.13258205],
[0.87108689, 0.14997182, 0.43524276],
[0.58190788, 0.32348629, 0.12158832]]])
2. np.random.randn()
numpy.random.randn(d0,d1,…,dn)
- randn函式返回一個或一組樣本,具有標準正態分佈。
- dn表示每個維度
- 返回值為指定維度的numpy.ndarray
>>> np.random.randn() # 當沒有輸入引數時,僅返回一個值
-0.7377941002942127
>>> np.random.randn(3, 3)
array([[-0.20565666, 1.23580939, -0.27814622],
[ 0.53923344, -2.7092927 , 1.27514363],
[ 0.38570597, -1.90564739, -0.10438987]])
>>> np.random.randn(3, 3, 3)
array([[[ 0.64235451, -1.64327647, -1.27366899],
[ 0.69706885, 0.75246699, 2.16235763],
[ 1.01141338, -0.19188666, 0.07684428]],
[[ 1.34367043, -0.76837057, 0.27803575],
[ 0.97007349, 0.41297538, -1.65008923],
[-3.78282033, 0.67567421, -0.0753552 ]],
[[-0.86540385, 0.14603592, 0.29318291],
[-0.8167798 , -0.25492782, -0.58758 ],
[ 0.02612474, 0.17882535, -0.95483945]]])
3. numpy.random.randint()
numpy.random.randint(low, high=None, size=None, dtype=’l’)
- 從區間[low,high)返回隨機整形
- 引數:low為最小值,high為最大值,size為陣列維度大小,dtype為資料型別,預設的資料型別是np.int
- high沒有填寫時,預設生成隨機數的範圍是[0,low)
>>> np.random.randint(1, size = 10) # 返回[0, 1)之間的整數,所以只有0
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> np.random.randint(1, 5) # 返回[1, 5)之間隨機的一個數字
2
>>> np.random.randint(-3, 3, size=(3, 3))
array([[-1, -2, -2],
[-3, -1, -2],
[ 2, 2, 2]])
4. numpy.random.random_sample()
numpy.random.random_sample(size=None)
- 從[0.0, 1.0)的半開區間返回浮點數
>>> np.random.random_sample()
0.47108547995356098
>>> np.random.random_sample((5,))
array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428])
>>> 5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
[-2.99091858, -0.79479508],
[-1.23204345, -1.75224494]])
類似功能的還有:numpy.random.random(size=None)
numpy.random.ranf(size=None)
numpy.random.sample(size=None)
5. numpy.random.choice() ✡️
numpy.random.choice(a, size=None, replace=True, p=None)
- 從給定的一位陣列中生成一個隨機樣本
- a要求輸入一維陣列類似資料或者是一個int;size是生成的陣列緯度,要求數字或元組;replace為布林型,決定樣本是否有替換;p為樣本出現概率
>>> np.random.choice(5, 3) # 這個等同於np.random.randint(0,5,3)
array([0, 3, 4])
>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])
>>> np.random.choice(5, 3, replace=False)
array([3,1,0])
>>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([2, 3, 0])
>>> aa_milne_arr = [`pooh`, `rabbit`, `piglet`, `Christopher`]
>>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
array([`pooh`, `pooh`, `pooh`, `Christopher`, `piglet`],
dtype=`|S11`)
感謝您的閱讀?