numpy 随机采样 randint() 用法及代码示例
numpy.random.randint()
是用于在numpy中进行随机采样的功能之一。它返回一个指定形状的数组,并用从低(包含)到高(不含)的随机整数填充,即在区间[low, high).
用法:
numpy.random.randint(low, high=None, size=None, dtype=’l’)
参数:
low:[int]要从分布中得出的最低(有符号)整数。但是,如果high = None,则它将作为样本中的最高整数。
high:[int,可选]从分布中提取的最大(有符号)整数。
size :[int或int元组,可选]输出形状。如果给定的形状是例如(m,n,k),则绘制m * n * k个样本。默认值为无,在这种情况下,将返回单个值。
dtype :[可选]所需的输出数据类型。
**Return :**间隔中的随机整数数组[low, high)
如果未提供大小,则为单个此类int随机整数。
代码1:
# Python program explaining
# numpy.random.randint() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.randint(low = 0, high = 3, size = 5)
print ("Output 1D Array filled with random integers:", out_arr)
输出:
Output 1D Array filled with random integers: [1 1 0 1 1]
代码2:
# Python program explaining
# numpy.random.randint() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.randint(low = 4, size =(2, 3))
print ("Output 2D Array filled with random integers:", out_arr)
输出:
Output 2D Array filled with random integers: [[1 1 0] [1 0 3]]
代码3:
# Python program explaining
# numpy.random.randint() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.randint(2, 10, (2, 3, 4))
print ("Output 3D Array filled with random integers:", out_arr)
输出:
Output 3D Array filled with random integers: [[[4 8 5 7] [6 5 6 7] [4 3 4 3]] [[2 9 2 2] [3 2 2 3] [6 8 3 2]]]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。