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

2021年3月10日16:12:14 发表评论 630 次浏览

numpy.random.random_integers()是用于在numpy中进行随机采样的功能之一。它返回指定形状的数组, 并用从低(包含)到高(不含)的随机整数填充, 即在区间[低高)。

语法:numpy.random.random_integers(low, high = None, size = None)参数:low:[int]要从分布中绘制的最低(带符号)整数。但是, 如果高, 它将作为样本中的最高整数=无。 high:[int, 可选]从分布中提取的最大(有符号)整数。 size:[int或int元组, 可选]输出形状。如果给定的形状是例如(m, n, k), 则绘制m * n * k个样本。默认值为无, 在这种情况下, 将返回一个值。返回:区间为[low, high]的随机整数数组;如果未提供大小, 则为单个此类随机int。

代码1:

# Python program explaining
# numpy.random.random_integers() function
  
# importing numpy
import numpy as geek
  
# output array
out_arr = geek.random.random_integers(low = 0 , high = 5 , size = 4 )
print ( "Output 1D Array filled with random integers : " , out_arr)

输出:

Output 1D Array filled with random integers :  [1 1 4 1]

代码2:

# Python program explaining
# numpy.random.random_integers() function
  
# importing numpy
import numpy as geek
  
  
# output array
out_arr = geek.random.random_integers(low = 3 , size = ( 3 , 3 ))
print ( "Output 2D Array filled with random integers : " , out_arr)

输出:

Output 2D Array filled with random integers :  [[2 3 1]
 [2 2 3]
 [3 3 3]]

代码3:

# Python program explaining
# numpy.random.random_integers() function
  
# importing numpy
import numpy as geek
  
# output array
out_arr = geek.random.random_integers( 1 , 6 , ( 2 , 2 , 3 ))
print ( "Output 3D Array filled with random integers : " , out_arr)

输出:

Output 3D Array filled with random integers :  [[[4 8 5 7]
Output 3D Array filled with random integers :  [[[5 1 5]
  [5 4 1]]

 [[3 6 4]
  [4 5 3]]]

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

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


木子山

发表评论

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