Rolling or sliding window iterator?

后端 未结 23 1453
南方客
南方客 2020-11-21 05:23

I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. Default Python iteration can be considered a special case, where the window length

23条回答
  •  礼貌的吻别
    2020-11-21 05:51

    Optimized Function for sliding window data in Deep learning

    def SlidingWindow(X, window_length, stride):
        indexer = np.arange(window_length)[None, :] + stride*np.arange(int(len(X)/stride)-window_length+4)[:, None]
        return X.take(indexer)
    

    to apply on multidimensional array

    import numpy as np
    def SlidingWindow(X, window_length, stride1):
        stride=  X.shape[1]*stride1
        window_length = window_length*X.shape[1]
        indexer = np.arange(window_length)[None, :] + stride1*np.arange(int(len(X)/stride1)-window_length-1)[:, None]
        return X.take(indexer)
    

提交回复
热议问题