numpy.random抽樣

numpy.random中的函數

rand()

生成隨機值

>>>np.random.rand(3)
array([0.68256392, 0.14294158, 0.72120024])

randn()

生成標準正態分佈樣本

>>>np.random.randn(3)
array([-2.49001983, -0.33634447, -0.57466392])

randint(low,high,size)

生成左閉右開的隨機整數
randint(high[,size]) 取小於high的整數[個數爲size]

>>>np.random.randint(2,10,3)
array([9, 7, 4])

random_integers(low,high,size)

左閉右閉的隨機整數,注意,python3.8.1已經不再使用這個函數

>>>np.random.random_integers(4,size=10)

Warning (from warnings module):
  File "<pyshell#11>", line 1
DeprecationWarning: This function is deprecated. Please call randint(1, 4 + 1) instead
array([3, 1, 3, 4, 2, 1, 4, 4, 1, 4])

random_sample([size])、random([size]) 、ranf([size])、sample([size])

生成[0.0,1.0)間的隨機浮點數

>>>np.random.random_sample(3)
array([0.07828906, 0.92357492, 0.23780065])

>>>np.random.random(3)
array([0.92554691, 0.98998066, 0.1320273 ])

>>>np.random.ranf(3)
array([0.57944383, 0.89816435, 0.8438508 ])

>>>np.random.sample(3)
array([0.01619052, 0.04228103, 0.89007802])

choice(a[, size, replace, p])

從一個給定的數組a中生成隨機樣本,其中p爲a中元素被抽取的概率分佈

>>>np.random.choice([2,3,1,4,8,5],10,p=[0.3,0.5,0,0,0.1,0.1])
array([3, 3, 3, 3, 2, 3, 8, 3, 5, 3])

uniform(low,high,size)

生成 [low.high) 間的均勻分佈,注意是左閉右開

>>>np.random.uniform(-1,1,3)
array([-0.52535198, -0.81110613, -0.69339663])
發佈了2 篇原創文章 · 獲贊 0 · 訪問量 115
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章