Evan Kim
Evan Kim

Reputation: 829

How to make a non-square rolling window for numpy array?

How do I change the rolling window function size to be a 2x3 window as opposed to the 3x3 window it is at right now? I am not skilled enough and have no idea how to reverse engineer the function to understand how window_size works :(

import numpy as np

def rolling_window(array, window_size):
    itemsize = array.itemsize
    shape = (array.shape[0] - window_size + 1,
             array.shape[1] - window_size + 1,
             window_size, window_size)
    strides = (array.shape[1] * itemsize, itemsize,
               array.shape[1] * itemsize, itemsize)
    return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)

roller = np.arange(1,16).reshape(3,5)
print(roller)
run = rolling_window(roller,3)
print(run)

Upvotes: 3

Views: 844

Answers (1)

sacuL
sacuL

Reputation: 51335

If you know you want a 2D window, then specify window_size[0] to go with array.shape[0] and window_size[1] with array.shape[1] when you define shape:

def rolling_window(array, window_size):
    itemsize = array.itemsize
    # broadcast window_size so it will still work with a scalar:
    window_size = np.broadcast_to(window_size,[2,])
    shape = (array.shape[0] - window_size[0] + 1,
             array.shape[1] - window_size[1] + 1,
             window_size[0], window_size[1])
    # the following would also work:
    # shape = (*np.array(array.shape) - window_size + 1, *window_size)
    strides = (array.shape[1] * itemsize, itemsize,
               array.shape[1] * itemsize, itemsize)
    return np.lib.stride_tricks.as_strided(array, shape=shape, strides=strides)

roller = np.arange(1,16).reshape(3,5)
>>> print(roller)
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]

run = rolling_window(roller,[2,3])

>>> run
array([[[[ 1,  2,  3],
         [ 6,  7,  8]],

        [[ 2,  3,  4],
         [ 7,  8,  9]],

        [[ 3,  4,  5],
         [ 8,  9, 10]]],


       [[[ 6,  7,  8],
         [11, 12, 13]],

        [[ 7,  8,  9],
         [12, 13, 14]],

        [[ 8,  9, 10],
         [13, 14, 15]]]])

Upvotes: 4

Related Questions