numpy 随机采样 sample() 用法及代码示例
numpy.random.sample()
是用于在numpy中进行随机采样的功能之一。它返回指定形状的数组,并在半开间隔中将其填充为随机浮点数[0.0, 1.0).
用法:
numpy.random.sample(size=None)
参数:
size :[int或int元组,可选]输出形状。如果给定的形状是例如(m,n,k),则绘制m * n * k个样本。默认值为无,在这种情况下,将返回单个值。
**Return :**间隔中的随机浮动数组[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.sample()
print ("Output random value:", out_val)
输出:
Output random value: 0.9261509680895836
代码2:
# Python program explaining
# numpy.random.sample() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.sample(size =(3, 3))
print ("Output 2D Array filled with random floats:", out_arr)
输出:
Output 2D Array filled with random floats: [[ 0.75908777 0.88295677 0.60979136] [ 0.68157065 0.75100312 0.08321613] [ 0.8360331 0.64808891 0.14731635]]
代码3:
# Python program explaining
# numpy.random.sample() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.sample((2, 2, 3))
print ("Output 3D Array filled with random floats:", out_arr)
输出:
Output 3D Array filled with random floats: [[[ 0.3073475 0.75709465 0.86934712] [ 0.21953745 0.48138292 0.30686482]] [[ 0.48925625 0.60222083 0.14403257] [ 0.87030919 0.87298872 0.2222136 ]]]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。