首页 > 解决方案 > 为多维数据生成具有窗口大小和步幅的子序列

问题描述

我有一个不同长度的 n 维序列列表,但 n 是固定的。
例如 n=3
1. [ [1, 2, 3], [4, 5, 6], [4, 4, 4] ]
2. [ [4, 6, 7], [6, 4, 3] ]
3. [ [7, 9, 1], [6, 2, 0], [0, 7, 3], [3, 3, 8] ]

我想从这些创建窗口大小 w 的所有子序列的列表。例如对于 w = 2,
列表 1:[ [1, 2, 3], [4, 5, 6] ]
列表 2: [ [4, 5, 6] , [ 4, 4, 4] ]
列表 3: [ [4, 6, 7], [6, 4, 3] ]
列表 4: [ [7, 9, 1], [6, 2, 0] ] 等等...

我想在 Python 中快速实现这一点。我试图在这里实现解决方案:Split Python sequence (time series/array) into subsequences with overlay,但我不确定如何计算 n 维数据点的步幅,我得到了奇怪的结果。

获取可变窗口大小的此类子序列的最佳方法是什么?

标签: pythonperformancenumpytime-series

解决方案


这不一定是快速实现,但这是我认为您想要的临时实现,以您的示例问题为例:

a= [ [1, 2, 3], [4, 5, 6], [4, 4, 4] ]
b= [ [4, 6, 7], [6, 4, 3] ]
c= [ [7, 9, 1], [6, 2, 0], [0, 7, 3], [3, 3, 8] ]

#put all lists into a list to loop through
uberset=[a,b,c]

#specific window size to work with
window=2

#rolling window function to loop through a given list of lists and window size
def rolling_window(ListOfLists, I_Window):
    ans=[]
    #for each List
    for i in ListOfLists:
      #slide your window across the list, stacking each window into the answer
      for j in range(0,len(i)-I_Window+1,1):
        ans.append(i[j:j+I_Window])
    return ans

print(rolling_window(uberset, window))

(针对后续问题进行了编辑:现在大步前进!):

a= [ [1, 2, 3], [4, 5, 6], [4, 4, 4] ]
b= [ [4, 6, 7], [6, 4, 3] ]
c= [ [7, 9, 1], [6, 2, 0], [0, 7, 3], [3, 3, 8] ]

#put all lists into a list to loop through
uberset=[a,b,c]

#specific window size to work with and stride to work with
window=2
stride=1

#rolling window function to loop through a given list of lists, window size, stride
def rolling_window(ListOfLists, I_Window,I_Stride):
    ans=[]
    #for each List
    for i in ListOfLists:
      #slide your window across the list, stacking each window into the answer
      for j in range(0,len(i)-I_Window+1,I_Stride):
        ans.append(i[j:j+I_Window])
    return ans

print(rolling_window(uberset, window, stride))

##and if you wanted to work with say a window size of 1 and a stride of 2:
print(rolling_window(uberset, 1, 2))

推荐阅读