📜  numpy 中的随机抽样 | ranf()函数

📅  最后修改于: 2022-05-13 01:54:53.607000             🧑  作者: Mango

numpy 中的随机抽样 | ranf()函数

numpy.random.ranf()是在 numpy 中进行随机采样的函数之一。它返回一个指定形状的数组,并在半开区间[0.0, 1.0).

代码#1:

# Python program explaining
# numpy.random.ranf() function
  
# importing numpy
import numpy as geek
  
  
# output random float value
out_val = geek.random.ranf()
print ("Output random float value : ", out_val) 
输出 :
Output random float value :  0.0877051588430926

代码#2:

# Python program explaining
# numpy.random.ranf() function
  
# importing numpy
import numpy as geek
  
  
# output array
out_arr = geek.random.ranf(size =(2, 1))
print ("Output 2D Array filled with random floats : ", out_arr) 
输出 :
Output 2D Array filled with random floats :  [[ 0.14186407]
 [ 0.58068259]]


代码#3:

# Python program explaining
# numpy.random.ranf() function
  
# importing numpy
import numpy as geek
  
# output array
out_arr = geek.random.ranf((3, 3, 2))
print ("Output 3D Array filled with random floats : ", out_arr) 
输出 :
Output 3D Array filled with random floats :  [[[ 0.11013584  0.67844746]
  [ 0.84691569  0.09467084]
  [ 0.69918864  0.12137178]]

 [[ 0.30629051  0.28301093]
  [ 0.1302665   0.2196221 ]
  [ 0.51555358  0.73191852]]

 [[ 0.72806359  0.66485275]
  [ 0.80654791  0.04947181]
  [ 0.06380535  0.99306064]]]