How to sample a numpy array and perform computation on each sample efficiently?
问题 Assume I have a 1d array, what I want is to sample with a moving window and within the window divide each element by the first element. For example if I have [2, 5, 8, 9, 6] and a window size of 3, the result will be [[1, 2.5, 4], [1, 1.6, 1.8], [1, 1.125, 0.75]]. What I'm doing now is basically a for loop import numpy as np arr = np.array([2., 5., 8., 9., 6.]) window_size = 3 for i in range(len(arr) - window_size + 1): result.append(arr[i : i + window_size] / arr[i]) etc. When the array is