Skip to content Skip to sidebar Skip to footer

Reorganizing A 2d Numpy Array Into 3d

I have a 12X2 array I want to reorganize into a 3x4x2 array. Specifically, I want to change a = np.array(([1,13],[2,14],[3,15],[4,16],[5,17],[6,18],[7,19],[8,20],[9,21],[10,22],[11

Solution 1:

You can use a combination of np.reshape and np.transpose, like so -

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

So, for your actual case (if I understood it correctly) would be -

a.reshape(64,128,-1).transpose(2,0,1)

Please note that the output shape in this case would be (3, 64, 128) though. So, I am assuming the sample case doesn't properly correspond to your actual case.

Solution 2:

There are many ways to do it, how about a.T.reshape(2,3,4):

n [14]: a.T.reshape(2,3,4)
Out[14]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]]])

Post a Comment for "Reorganizing A 2d Numpy Array Into 3d"