如何为numpy数组制作非方形滚动窗口?

时间:2018-12-01 01:28:25

标签: python numpy

如何将滚动窗口功能的大小更改为2x3窗口,而不是现在的3x3窗口?我不够熟练,也不知道如何对函数进行反向工程以了解window_size的工作原理:(

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)

1 个答案:

答案 0 :(得分:2)

如果您知道要使用2D窗口,请在定义$ ./bin/fopen_file_from_user Enter a filename: dat/file.txt file opened: dat/file.txt line[ 1]: larry snedden 123 mocking bird lane line[ 2]: sponge bob 321 bikini bottom beach line[ 3]: mary fleece 978 pasture road line[ 4]: hairy whodunit 456 get out of here now lane 时指定window_size[0]array.shape[0]一起使用,并且window_size[1]array.shape[1]一起使用:

shape