Creating a sliding window with a shift using array with elements

68 Views Asked by At

Hi just having an issue here with respect to creating a sliding window with a shift over a numpy array with X number of elements.

weather_radiance_copy is a numpy array with 10 elements (although it could be any number). I have defined a sliding window that gives me a window_size.

The issue is that it has a default shift of one position whereas I want that number to be arbitrary so I can set it to whatever shift I want.

I also want to be able to drop the remainder portion at the end if the window and shift do not perfectly end up in line with the number of elements. So for example, if there are 10 elements in the numpy array and I have a sliding window of 3, then I would have a three windows of three elements and then one window of one element....I want to chop off that uneven last window using something akin to drop_remainder = True.

My function therefore should have three inputs, the elements, window_size and shift value.

weather_radiance_copy = [5.11, 3.21, 6.43, 1.09, 5.78, 9.9, 0.1232, 3.214, 6.5321, 1.01]

def sliding_window(elements, window_size):

    if len(elements) <= window_size:
       return elements
    for i in range(len(elements)):
        print(elements[i:i + window_size])

sliding_window(weather_radiance_copy, 5)
0

There are 0 best solutions below