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

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

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

numpy.random.randint()是在 numpy 中进行随机采样的函数之一。它返回一个指定形状的数组,并用从low(包括)到high(不包括)的随机整数填充它,即在区间[low, high).

代码#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]]]