0

I have a list of n-dimensional sequences of varying length, but fixed n.
e.g. 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] ]

I want to create lists of all subsequences of window size w from these. E.g. for w = 2,
List 1: [ [1, 2, 3], [4, 5, 6] ]
List 2: [ [4, 5, 6] , [ 4, 4, 4] ]
List 3: [ [4, 6, 7], [6, 4, 3] ]
List 4: [ [7, 9, 1], [6, 2, 0] ] and so on...

I want a fast implementation of this in Python. I tried to implement the solution here: Split Python sequence (time series/array) into subsequences with overlap, but I am not sure how the strides are calculated for n-dimensional data points and I am getting weird results.

What is the best way to get such subsequences for variable window sizes?

ad123
  • 37
  • 6

1 Answers1

1

This is not necessarily a fast implementation, but this is a provisional implementation of what I think you want, taking your example question:

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))

(Edited for follow-up question: now adding in stride!):

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))
  • Thanks! Is there any way to incorporate stride in this code as well? For instance, if list c has 100 lists in it, and I want a window size of 10 with a stride of 3, how can this be done? – ad123 Jul 08 '20 at 09:06