簡體   English   中英

如何切片具有重疊百分比的數組

[英]How to slice arrays with a percantage of overlapping

我有一組這樣的數據:

numpy.array([[3, 7],[5, 8],[6, 19],[8, 59],[10, 42],[12, 54], [13, 32], [14, 19], [99, 19]])

我想將其拆分為具有重疊百分比的 chunkcs 數量,對於每列單獨...例如對於第 1 列,拆分為 3 個具有 %50 重疊的 chunkcs(導致二維數組):

[[3, 5, 6, 8,],
        [6, 8, 10, 12,],
              [10, 12, 13, 14,]]

(忽略最后一行,這將導致 [13, 14, 99] 與其他行的大小不同)。 我正在嘗試創建一個函數,該函數接受數組、chunkc 的數量和重疊百分比並返回結果。

這是一個窗口函數,所以使用skimage.util.view_as_windows

from skimage.util import view_as_windows

out = view_as_windows(in_arr[:, 0], window_shape = 4, step = 2)

如果你只需要numpy ,你可以使用這個食譜

僅對於 numpy,相當快的方法是:

def rolling(a, window, step):
    shape = ((a.size - window)//step + 1, window)
    strides = (step*a.itemsize, a.itemsize)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

你可以這樣稱呼它:

rolling(arr[:,0].copy(), 4, 2)

備注:我有rolling(arr[:,0], 4, 2)意外輸出,所以只需復制一份。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM