當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


numpy 隨機采樣 random()用法及代碼示例

numpy.random.random()是用於在numpy中進行隨機采樣的函數之一。它返回指定形狀的數組,並在half-open間隔中填充隨機浮點數[0.0, 1.0).

用法: numpy.random.random(size=None)

參數:
size :[int或int元組,可選]輸出形狀。如果給定的形狀是例如(m,n,k),則繪製m * n * k個樣本。默認值為無,在這種情況下,將返回單個值。


Return :間隔中的隨機浮點數數組[0.0, 1.0).如果未提供尺寸,則為單個此類隨機浮點數。

代碼1:

# Python program explaining 
# numpy.random.random() function 
  
# importing numpy 
import numpy as geek 
  
  
# output array 
out_arr = geek.random.random(size = 3) 
print ("Output 1D Array filled with random floats:", out_arr) 
輸出:
Output 1D Array filled with random floats: [ 0.21698734  0.01617363  0.70382199]

代碼2:

# Python program explaining 
# numpy.random.random() function 
  
# importing numpy 
import numpy as geek 
  
  
# output array 
out_arr = geek.random.random(size =(2, 4)) 
print ("Output 2D Array filled with random floats:", out_arr) 
輸出:
Output 2D Array filled with random floats: [[ 0.95423066  0.35595927  0.76048569  0.90163066]
 [ 0.41903408  0.85596254  0.21666156  0.05734769]]


代碼3:

# Python program explaining 
# numpy.random.random() function 
  
# importing numpy 
import numpy as geek 
  
# output array 
out_arr = geek.random.random((2, 3, 2)) 
print ("Output 3D Array filled with random floats:", out_arr) 
輸出:
Output 3D Array filled with random floats: [[[ 0.07861816  0.79132387]
  [ 0.9112629   0.98162851]
  [ 0.0727613   0.03480279]]

 [[ 0.11267727  0.07631742]
  [ 0.47554553  0.83625053]
  [ 0.67781339  0.37856642]]]


相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Random sampling in numpy | random() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。