Numpy summation with sliding window is really slow
问题 Code: shape = np.array([6, 6]) grid = np.array([x.ravel() for x in np.meshgrid(*[np.arange(x) for i, x in enumerate(shape)], indexing='ij')]).T slices = [tuple(slice(box[i], box[i] + 2) for i in range(len(box))) for box in grid] score = np.zeros((7,7,3)) column = np.random.randn(36, 12) #just for example column >> array([[ 0, 1, 2, 3, ... 425, 426, 427, 428, 429, 430, 431]]) column = column.reshape((16, 3, 3, 3)) for i, window in enumerate(slices): score[window] += column[i] score >> array([[