通过重叠 Windows 在数组上循环

Looping on an array by Overlapping Windows

我有一个 numpy 数组:

seq = np.array(range(1,31))

In [24]: seq
Out[24]: array([ 1,  2,  3, ..., 28, 29, 30])

我想将它分成长度为 n 的重叠(或非重叠)段。

n=7
overlap = 3
windowed_seq = windowing(seq,n,overlap)

In [24]: windowed_seq
Out[24]: [ 1,  5,   ...,    17, 21, 
           2,  6,   ...,    18, 22, 
           3,  7,   ...,    19, 23, 
           4,  8,   ...,    20, 24, 
           5,  9,   ...,    21, 25, 
           6,  10,  ...,    22, 26, 
           7,  11,  ...,    23, 27] 

最后一栏:

25,
26,
27,
28,
29,
30,
31,
?]

被省略,因为缺少一个元素来完成 window..

我在网上找到了这个解决方案,但它不适用于数组...

https://www.safaribooksonline.com/library/view/python-cookbook-2nd/0596007973/ch19s08.html

import itertools
def windows(iterable, length=2, overlap=0):
    it = iter(iterable)
    results = list(itertools.islice(it, length))
    while len(results) == length:
        yield results
        results = results[length-overlap:]
        results.extend(itertools.islice(it, length-overlap))
    if results:
        yield results
if _ _name_ _ == '_ _main_ _':
    seq = 'foobarbazer'
    for length in (3, 4):
        for overlap in (0, 1):
            print '%d %d: %s' % (length, overlap,
                    map(''.join, windows(seq, length, overlap)))

你能帮我让它为我的阵列工作吗?

你可以使用 scikit 图片的 view_as_windows:

import skimage
import numpy

data = numpy.arange(1, 31)
print skimage.util.view_as_windows(data, 7, step=3).T

产量

[[ 1  4  7 10 13 16 19 22]
 [ 2  5  8 11 14 17 20 23]
 [ 3  6  9 12 15 18 21 24]
 [ 4  7 10 13 16 19 22 25]
 [ 5  8 11 14 17 20 23 26]
 [ 6  9 12 15 18 21 24 27]
 [ 7 10 13 16 19 22 25 28]]

如果你想确保你的数组恰好是窗口长度的倍数,你可以使用

data = numpy.append(data, numpy.repeat(numpy.nan, len(data) % 7))

之后你的输出变成

[[  1.   4.   7.  10.  13.  16.  19.  22.  25.]
 [  2.   5.   8.  11.  14.  17.  20.  23.  26.]
 [  3.   6.   9.  12.  15.  18.  21.  24.  27.]
 [  4.   7.  10.  13.  16.  19.  22.  25.  28.]
 [  5.   8.  11.  14.  17.  20.  23.  26.  29.]
 [  6.   9.  12.  15.  18.  21.  24.  27.  30.]
 [  7.  10.  13.  16.  19.  22.  25.  28.  nan]]