在python數(shù)據(jù)分析的學(xué)習(xí)和應(yīng)用過程中,經(jīng)常需要用到numpy的隨機函數(shù)舵匾,由于隨機函數(shù)random的功能比較多俊抵,經(jīng)常會混淆或記不住,下面我們一起來匯總學(xué)習(xí)下坐梯。
import numpy as np
1 numpy.random.rand()
numpy.random.rand(d0,d1,...,dn)
- rand函數(shù)根據(jù)給定維度生成[0,1)之間的數(shù)據(jù)徽诲,包含0,不包含1
- dn表格每個維度
- 返回值為指定維度的array
np.random.rand(4,2)
array([[ 0.02173903, 0.44376568],
[ 0.25309942, 0.85259262],
[ 0.56465709, 0.95135013],
[ 0.14145746, 0.55389458]])
np.random.rand(4,3,2) # shape: 4*3*2
array([[[ 0.08256277, 0.11408276],
[ 0.11182496, 0.51452019],
[ 0.09731856, 0.18279204]],
[[ 0.74637005, 0.76065562],
[ 0.32060311, 0.69410458],
[ 0.28890543, 0.68532579]],
[[ 0.72110169, 0.52517524],
[ 0.32876607, 0.66632414],
[ 0.45762399, 0.49176764]],
[[ 0.73886671, 0.81877121],
[ 0.03984658, 0.99454548],
[ 0.18205926, 0.99637823]]])
2 numpy.random.randn()
numpy.random.randn(d0,d1,...,dn)
- randn函數(shù)返回一個或一組樣本吵血,具有標(biāo)準(zhǔn)正態(tài)分布馏段。
- dn表格每個維度
- 返回值為指定維度的array
np.random.randn() # 當(dāng)沒有參數(shù)時,返回單個數(shù)據(jù)
-1.1241580894939212
np.random.randn(2,4)
array([[ 0.27795239, -2.57882503, 0.3817649 , 1.42367345],
[-1.16724625, -0.22408299, 0.63006614, -0.41714538]])
np.random.randn(4,3,2)
array([[[ 1.27820764, 0.92479163],
[-0.15151257, 1.3428253 ],
[-1.30948998, 0.15493686]],
[[-1.49645411, -0.27724089],
[ 0.71590275, 0.81377671],
[-0.71833341, 1.61637676]],
[[ 0.52486563, -1.7345101 ],
[ 1.24456943, -0.10902915],
[ 1.27292735, -0.00926068]],
[[ 0.88303 , 0.46116413],
[ 0.13305507, 2.44968809],
[-0.73132153, -0.88586716]]])
標(biāo)準(zhǔn)正態(tài)分布介紹
- 標(biāo)準(zhǔn)正態(tài)分布---standard normal distribution
- 標(biāo)準(zhǔn)正態(tài)分布又稱為u分布践瓷,是以0為均值院喜、以1為標(biāo)準(zhǔn)差的正態(tài)分布,記為N(0晕翠,1)喷舀。
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)
np.random.randint(1,size=5) # 返回[0,1)之間的整數(shù)券敌,所以只有0
array([0, 0, 0, 0, 0])
np.random.randint(1,5) # 返回1個[1,5)時間的隨機整數(shù)
4
np.random.randint(-5,5,size=(2,2))
array([[ 2, -1],
[ 2, 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ù)
np.random.random_integers(1,size=5)
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)
print('-----------random_sample--------------')
print(np.random.random_sample(size=(2,2)))
print('-----------random--------------')
print(np.random.random(size=(2,2)))
print('-----------ranf--------------')
print(np.random.ranf(size=(2,2)))
print('-----------sample--------------')
print(np.random.sample(size=(2,2)))
-----------random_sample--------------
[[ 0.34966859 0.85655008]
[ 0.16045328 0.87908218]]
-----------random--------------
[[ 0.25303772 0.45417512]
[ 0.76053763 0.12454433]]
-----------ranf--------------
[[ 0.0379055 0.51288667]
[ 0.71819639 0.97292903]]
-----------sample--------------
[[ 0.59942807 0.80211491]
[ 0.36233939 0.12607092]]
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)
np.random.choice(5,3)
array([4, 1, 4])
np.random.choice(5, 3, replace=False)
# 當(dāng)replace為False時鬼吵,生成的隨機數(shù)不能有重復(fù)的數(shù)值
array([0, 3, 1])
np.random.choice(5,size=(3,2))
array([[1, 0],
[4, 2],
[3, 3]])
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
np.random.choice(demo_list,size=(3,3))
array([['moto', 'iphone', 'xiaomi'],
['lenovo', 'xiaomi', 'xiaomi'],
['xiaomi', 'lenovo', 'iphone']],
dtype='<U7')
- 參數(shù)p的長度與參數(shù)a的長度需要一致弛房;
- 參數(shù)p為概率,p里的數(shù)據(jù)之和應(yīng)為1
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])
array([['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'xiaomi', 'iphone']],
dtype='<U7')
6 numpy.random.seed()
- np.random.seed()的作用:使得隨機數(shù)據(jù)可預(yù)測而柑。
- 當(dāng)我們設(shè)置相同的seed,每次生成的隨機數(shù)相同荷逞。如果不設(shè)置seed媒咳,則每次會生成不同的隨機數(shù)
np.random.seed(0)
np.random.rand(5)
array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])
更多精彩內(nèi)容請關(guān)注公眾號:
“Python數(shù)據(jù)之道”