I have a large array where each row is a time series and thus needs to stay in order.
I want to select a random window of a given size for each row.
In the return statement, change the slicing to advanced indexing, also you need to fix the sampling code a little bit:
def select_random_windows(arr, window_size):
offsets = np.random.randint(0, arr.shape[1]-window_size+1, size=arr.shape[0])
return arr[np.arange(arr.shape[0])[:,None], offsets[:,None] + np.arange(window_size)]
select_random_windows(arr, 3)
#array([[ 4, 5, 6],
# [ 7, 8, 9],
# [17, 18, 19],
# [25, 26, 27],
# [31, 32, 33],
# [39, 40, 41]])