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]]]

发表回复

登录... 后才能评论