通过重叠的窗口在数组上循环



我有一个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,
?]

省略了,因为缺少一个元素来完成窗口。

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

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 Image的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]]

相关内容

  • 没有找到相关文章

最新更新