numpy中的随机抽样:random_sample()函数用法

2021年4月1日15:42:51 发表评论 927 次浏览

numpy.random.random_sample()是用于在numpy中进行随机采样的功能之一。它返回指定形状的数组, 并在半开间隔中填充随机浮点数[0.0, 1.0)。

语法:numpy.random.random_sample(size = None)
参数:size:[int或int元组, 可选]输出形状。如果给定的形状是例如(m, n, k), 则绘制m * n * k个样本。默认值为无, 在这种情况下, 将返回一个值。
返回:间隔为[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]]]

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: