Transform 2D array to a 3D array with overlapping strides

前端 未结 2 1133
感情败类
感情败类 2021-01-13 14:45

I would convert the 2d array into 3d with previous rows by using NumPy or native functions.

Input:

[[1,2,3],
 [4,5,6],
 [7,8,9],
 [10,11,12],
 [13,1         


        
2条回答
  •  粉色の甜心
    2021-01-13 15:23

    How about a list comprehension?

    In [144]: np.array([l[i:i + 3][::-1] for i in range(0, len(l) - 2)])
    Out[144]: 
    array([[[ 7,  8,  9],
            [ 4,  5,  6],
            [ 1,  2,  3]],
    
           [[10, 11, 12],
            [ 7,  8,  9],
            [ 4,  5,  6]],
    
           [[13, 14, 15],
            [10, 11, 12],
            [ 7,  8,  9]]])
    

提交回复
热议问题