Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为多维数据生成具有窗口大小和步长的子序列_Python_Performance_Numpy_Time Series - Fatal编程技术网

Python 为多维数据生成具有窗口大小和步长的子序列

Python 为多维数据生成具有窗口大小和步长的子序列,python,performance,numpy,time-series,Python,Performance,Numpy,Time Series,我有一个可变长度的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]等等 我

我有一个可变长度的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快速实现这一点。我尝试在这里实现解决方案:,但我不确定如何计算n维数据点的步长,我得到了奇怪的结果


对于可变的窗口大小,获得这些子序列的最佳方法是什么?

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

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))
(为后续问题编辑:现在大步添加!):


谢谢是否有任何方法也可以将stride合并到此代码中?例如,如果列表c中有100个列表,我希望窗口大小为10,步幅为3,如何实现这一点?
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))