Skip to content Skip to sidebar Skip to footer

Numpy Group Reshaping / Indexing

The situation is I'd like to take the following Python / NumPy code: # Procure some data: z = np.zeros((32,32)) chunks = [] for i in range(0,32,step): for j in range(0,32,step

Solution 1:

You may need transpose:

a = np.arange(1024).reshape(32,32)

a.reshape(16,2,16,2).transpose((0,2,1,3)).reshape(-1,2,2)

Output:

array([[[   0,    1],
        [  32,   33]],

       [[   2,    3],
        [  34,   35]],

       [[   4,    5],
        [  36,   37]],

       ...,

       [[ 986,  987],
        [1018, 1019]],

       [[ 988,  989],
        [1020, 1021]],

       [[ 990,  991],
        [1022, 1023]]])

Post a Comment for "Numpy Group Reshaping / Indexing"