How can I select a random window from a numpy array greater than 2 dimensions wherein the window is random with respect to 2 different dimensions?
I'd like to do something similar to the answer in this post but in 3 dimensions, not 2: Selecting Random Windows from Multidimensional Numpy Array Rows
Example of what I am trying to vectorize (i.e. I'm trying to avoid a for loop):
import random
import numpy as np
ls = []
m = 3 # sequence length
k = 8 #batch_size
np_3D_array = np.random.randint(0,100, size = (5,7,4)) #random 3D array
for ii in range(k):
random_sheet = random.randint(0,np_3D_array.shape[0] - 1)
random_row = random.randint(0, np_3D_array.shape[1] - m)
ls.append(np_3D_array[random_sheet, random_row:random_row + m , :])
final_output = np.array(ls)
print(final_output.shape) #prints (8, 3, 4) to stdoout