'Choose a sliding window size of an array and build a list of array

I have an array with 8 rows in total. At each time, I only work with four rows. I want to chose a sliding window of 4 rows with each others and finally build a list. The list I want contains different array. However, at the sliding window that I want, rows 3 and 4 are constant.

So, I want to put the first row, second row, **only one number of third row and only one number of fourth row**. I also want to use another sliding window with size lead and choose the next values of the first row with length lead.

For example, for a simple array A= [[1,2,3, -4, -5], [2, 3, 4,7,8],[ 5, 5, 5, 5,5], [1, 1,1,1,1]], with window size, lag=3 and lead=2 , I want to build Out = [[array([[1,2,3, 2, 3, 4, 5, 1]]),array([[-4, -5]])]]. You can see that I choose the 3 value of row1, and row 2, and since the row 3 and 4 are constant, I choose only one value of them. The second array is also the next two values (lead=2) of first row, i.e -4, -5.

The shape of the array which I finally want, is a little different. For the following array with lead =4 and lag =3:

data = np.array([[2, 1, 2, 1, 4, 3, 1, 5, 5, 1, 2, 0, 5, 3, 1, 2,3],
             [4, 4, 2, 4, 3, 2, 3, 1, 5, 0, 5, 4, 5, 3, 0, 1,4],
             [5, 5, 5, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 5, 5,5],
             [0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1,1], 
             
             [3, 3, 5, 5, 4, 1, 2, 2, 5, 1, 1, 3, 1, 2, 4, 2,3],
             [3, 4, 3, 3, 5, 2, 1, 2, 4, 3, 2, 1, 2, 5, 5,3,2],
             [3, 3, 3, 2, 2, 2, 2, 4, 4, 4, 5, 5, 5, 5, 6,6,6],
             [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1,1,1]])

I want to have an output like:

Out = [[array([[2., 1., 2., 4., 4., 2., 5, 0]]),
        array([[1, 4, 3, 1]])],
       [array([[5., 5., 1., 1., 5., 0., 1, 0]]),
        array([[2, 0, 5, 3]])],

       [array([[3., 3., 5., 3., 4., 3., 3, 1]]),
        array([[5, 4, 1, 2]])],
       [array([[2., 5., 1., 2., 4., 3., 4, 1]]),
       array([[1, 3, 1, 2]])]]

I am using the following code, however it provides all the row beside each others.

X_train        = []

lag  = 3
lead = 4
F    = 4 #number of rows

for i in range(2):

    eachrow    = []

    for col in range(0, data.shape[1] - lead - lag, lead + lag):
    
        X_row  = []
    
        XTMP   = np.array(np.zeros((F, lag)))
    
        XTMP[0 : F, :]  = data[ F * i : F * i + F,     col : col + lag]
    
        X_row.append(XTMP)
    
        ytmp   = data[F * i, col + lag : col + lag + lead]
    
        X_row.append(np.array([ytmp]))
    
        eachrow.append(X_row)
    
    X_train.append(eachrow)  

X_train = [X_train for arr in X_train for X_train in arr]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source