0

I have a 2d 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>

So if window size is 5, the shape of the 3d data will be <2,5,3> and will be:

[[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?

Cranjis
  • 1,590
  • 8
  • 31
  • 64

1 Answers1

0

You can use 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):

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.]]])
Stef
  • 28,728
  • 2
  • 24
  • 52