[英]Transform sequential 2d array to time-windowed dataset
I have a 2d dataframe:我有一个二维 dataframe:
C1. C2. C3
0. 2. 3. 6
1. 8. 2. 1
2. 8. 6. 2
3. 4. 9. 0
4. 6. 7. 1
5. 2. 3. 0
I want it to be a 3d data with <num_windows, window_size, num_features>我希望它是带有 <num_windows, window_size, num_features> 的 3d 数据
So if window size is 5, the shape of the 3d data will be <2,5,3> and will be:因此,如果 window 大小为 5,则 3d 数据的形状将为 <2,5,3> 并且将是:
[[2,3,4],[8,2,1],[8,6,2],[4,9,0],[6,7,1]] , [[8,2,1],[8,6,2],[4,9,0],[6,7,1],[2,3,0]]
What is the best way to do it?最好的方法是什么?
You can use sliding_window_view
:您可以使用
sliding_window_view
:
num_windows = 2
window_size = 5
num_features = 3
np.lib.stride_tricks.sliding_window_view(df, (window_size, num_features))[:num_windows,0,:,:]
gives a 3D array of shape (num_windows, window_size, num_features)
:给出一个形状为
(num_windows, window_size, num_features)
的 3D 数组:
array([[[2., 3., 6.],
[8., 2., 1.],
[8., 6., 2.],
[4., 9., 0.],
[6., 7., 1.]],
[[8., 2., 1.],
[8., 6., 2.],
[4., 9., 0.],
[6., 7., 1.],
[2., 3., 0.]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.