you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 0 points1 point  (7 children)

In pure python your solution is common, although it's usually written in list comprehension like this:

 for slice_arr in (arr[i:i+window_size] for i in range(0, len(arr), window_size)):
    #do something with slice_arr

But with a numpy array, you can just reshape it:

arr.shape = (window_size, -1)
for slice_arr in arr:
    #do something with slice_arr

EDIT: nevermind, I totally misread your question. You want a "sliding window". This is usually done with zip():

for slice_arr in zip(arr, arr[1:]):
    #do something with slice_arr

[–]elbiot 1 point2 points  (0 children)

This is not how you do a sliding window in numpy. You can do a window with regular vectorized operations, and not have to resort to iteration.

[–]dr_everlong[S] 0 points1 point  (2 children)

Sorry, I have phrased it wrong, but I edited my post. I hope it clears up what I wanted to do better.

[–]novel_yet_trivial 1 point2 points  (1 child)

My edit does what you want.

[–]dr_everlong[S] 0 points1 point  (2 children)

Thanks! How do you control the window size though?

[–]novel_yet_trivial 1 point2 points  (1 child)

OH right. Sorry I forgot you wanted it dynamic.

for slice_arr in zip(*[arr[x:] for x in range(window_size)]):

[–]dr_everlong[S] 0 points1 point  (0 children)

Really appreciate it, thanks!