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

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

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

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

代码#1:

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

代码#2:

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


代码#3:

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

 [[ 0.54389267]
  [ 0.18491758]]

 [[ 0.97428409]
  [ 0.73729256]]]