python - 为多维数据生成具有窗口大小和步幅的子序列
问题描述
我有一个不同长度的 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 维数据点的步幅,我得到了奇怪的结果。
获取可变窗口大小的此类子序列的最佳方法是什么?
解决方案
这不一定是快速实现,但这是我认为您想要的临时实现,以您的示例问题为例:
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))
推荐阅读
- markdown - 将 Markdown 转换为 RTF 并保留标题?
- node.js - 使用 Cloud Functions 将复杂的 Json 写入 Cloud Firestore 的语法?
- asp.net - getexternallogininfo 在没有 MVC 的简单 asp.net 中的方法上返回 null
- video - 在运动补偿技术中,发送方是否可以只发送有关运动的信息?
- node.js - React 和 axios 的 ERR_CONNECTION_REFUSED
- node.js - 计算所有匹配文档中的字段总和 - MongoDB - Mongoose
- socket.io - 构建失败并出现解决错误
- javascript - 在程序编译期间雪花去除反斜杠?
- html - 带有 SVG 背景的 CSS 网格
- orocommerce - 某些关系无故添加到产品实体中