轉(zhuǎn)自為什么你用不好Numpy的random函數(shù)椒涯?
Table of Contents
- 1 numpy.random.rand()
- 2 numpy.random.randn()
- 3 numpy.random.randint()
- 4 生成(0,1)之間的浮點數(shù)
- 5 numpy.random.choice()
- 6 numpy.random.seed()
在python數(shù)據(jù)分析的學(xué)習(xí)和應(yīng)用過程中誉简,經(jīng)常需要用到numpy的隨機函數(shù)碉就,由于隨機函數(shù)random的功能比較多,經(jīng)常會混淆或記不住闷串,下面我們一起來匯總學(xué)習(xí)下瓮钥。
1、numpy.random.rand()
numpy.random.rand(d0,d1,…,dn)
- rand函數(shù)根據(jù)給定維度生成[0,1)之間的數(shù)據(jù),包含0碉熄,不包含1
- dn表格每個維度
- 返回值為指定維度的array
In [1]: import numpy as np
In [2]: np.random.rand(4,2)
Out[2]:
array([[0.03543649, 0.65699022],
[0.9640137 , 0.41203671],
[0.0274156 , 0.39526102],
[0.3824676 , 0.45293477]])
In [3]: np.random.rand(4,3,2) # shape: 4*3*2
Out[3]:
array([[[0.28132306, 0.55141521],
[0.54143807, 0.70934416],
[0.32648501, 0.09187119]],
[[0.95055997, 0.23998789],
[0.01906987, 0.60294769],
[0.61123351, 0.73415631]],
[[0.58635931, 0.02097799],
[0.19919756, 0.80204155],
[0.60294895, 0.52479401]],
[[0.89149168, 0.4630619 ],
[0.96669213, 0.13155044],
[0.78773533, 0.86499175]]])
2桨武、numpy.random.randn()
numpy.random.randn(d0,d1,…,dn)
- randn函數(shù)返回一個或一組樣本,具有標準正態(tài)分布锈津。
- dn表格每個維度
- 返回值為指定維度的array
In [4]: np.random.randn() # 當(dāng)沒有參數(shù)時呀酸,返回單個數(shù)據(jù)
Out[4]: 0.6788858950501044
In [5]: np.random.randn(2,4)
Out[5]:
array([[ 1.32778139, -1.13259764, 0.45843893, -0.64685327],
[-0.82564083, -1.28328157, -2.01779807, 1.13130852]])
In [6]: np.random.randn(4,3,2)
Out[6]:
array([[[-0.69405862, 1.35067816],
[-0.06843673, 0.94591779],
[-0.35769003, -1.12850492]],
[[-0.4138253 , 1.29171803],
[ 1.14909845, -1.06347911],
[ 0.70011367, 0.08417853]],
[[-0.01840155, -0.10676455],
[ 0.03287281, -0.01085714],
[ 1.23423707, 0.37175353]],
[[-0.9376414 , -1.25016235],
[ 0.53927611, 0.16836484],
[-0.41082093, 1.87387039]]])
3、numpy.random.randint()
3.1 numpy.random.randint()
numpy.random.randint(low, high=None, size=None, dtype='l')
- 返回隨機整數(shù)琼梆,范圍區(qū)間為[low,high)性誉,包含low,不包含high
- 參數(shù):low為最小值茎杂,high為最大值错览,size為數(shù)組維度大小,dtype為數(shù)據(jù)類型煌往,默認的數(shù)據(jù)類型是np.int
- high沒有填寫時倾哺,默認生成隨機數(shù)的范圍是[0,low)
In [7]: np.random.randint(1,size=5) # 返回[0,1)之間的整數(shù)刽脖,所以只有0
Out[7]: array([0, 0, 0, 0, 0])
In [8]: np.random.randint(1,5) # 返回1個[1,5)時間的隨機整數(shù)
Out[8]: 2
In [9]: np.random.randint(-5, 5, size=(2,2))
Out[9]:
array([[3, 1],
[3, 0]])
3.2 numpy.random.random_integers
numpy.random.random_integers(low, high=None, size=None)
- 返回隨機整數(shù)羞海,范圍區(qū)間為[low,high],包含low和high
- 參數(shù):low為最小值曾棕,high為最大值扣猫,size為數(shù)組維度大小
- high沒有填寫時,默認生成隨機數(shù)的范圍是[1翘地,low]
- 該函數(shù)在最新的numpy版本中已被替代,建議使用randint函數(shù)
In [11]: np.random.random_integers(1, size=5)
C:\Users\shexuan\AppData\Local\Continuum\anaconda3\Scripts\ipython:1: DeprecationWarning: This function is deprecated. Please call randint(1, 1 + 1) instead
Out[11]: array([1, 1, 1, 1, 1])
4癌幕、 生成[0,1)之間的浮點數(shù)
numpy.random.random_sample(size=None)
numpy.random.random(size=None)
numpy.random.ranf(size=None)
-
numpy.random.sample(size=None)
這四個函數(shù)其實都是一樣的衙耕,其余的三個只是numpy.random.random_sample
的重命名。
In [12]: np.random.random
Out[12]: <function RandomState.random_sample>
In [13]: np.random.ranf
Out[13]: <function RandomState.random_sample>
In [14]: np.random.sample
Out[14]: <function RandomState.random_sample>
In [15]: np.random.random_sample is np.random.random
Out[15]: True
In [16]: np.random.random_sample is np.random.ranf
Out[16]: True
In [17]: np.random.random_sample is np.random.sample
Out[17]: True
In [18]: np.random.random(size=(2,2))
Out[18]:
array([[0.31486874, 0.37654112],
[0.05517854, 0.42868528]])
5勺远、numpy.random.choice()
numpy.random.choice(a, size=None, replace=True, p=None)
- 從給定的一維數(shù)組中生成隨機數(shù)
- 參數(shù): a為一維數(shù)組類似數(shù)據(jù)或整數(shù)橙喘;size為數(shù)組維度;p為數(shù)組中的數(shù)據(jù)出現(xiàn)的概率
- a為整數(shù)時胶逢,對應(yīng)的一維數(shù)組為
np.arange(a)
In [20]: np.random.choice(5, 3, replace=False) # 當(dāng)replace為False時厅瞎,生成的隨機數(shù)不能有重復(fù)的數(shù)值
Out[20]: array([2, 0, 1])
In [21]: demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
...: np.random.choice(demo_list,size=(3,3))
...:
...:
Out[21]:
array([['iphone', 'moto', 'sansumg'],
['xiaomi', 'xiaomi', 'lenovo'],
['iphone', 'sansumg', 'xiaomi']], dtype='<U7')
# 參數(shù)p的長度與參數(shù)a的長度需要一致;
# 參數(shù)p為概率初坠,p里的數(shù)據(jù)之和應(yīng)為1
In [24]: demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
...: np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])
...:
...:
Out[24]:
array([['lenovo', 'sansumg', 'sansumg'],
['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'lenovo', 'sansumg']], dtype='<U7')
6和簸、numpy.random.seed()
- np.random.seed()的作用:使得隨機數(shù)據(jù)可預(yù)測。
- 當(dāng)我們設(shè)置相同的seed碟刺,每次生成的隨機數(shù)相同锁保。如果不設(shè)置seed,則每次會生成不同的隨機數(shù)
In [25]: np.random.seed(0)
In [26]: np.random.rand(5)
Out[26]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
In [27]: np.random.rand(5)
Out[27]: array([0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152])
In [28]: np.random.seed(0)
In [29]: np.random.rand(5)
Out[29]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])