Skip to content Skip to sidebar Skip to footer

Rearranging Numpy Array

import numpy as np a = np.array([[1,2], [3,4], [5,6], [7,8], [9,10], [11,12]]) print np.shape(a) The expected a

Solution 1:

You could use some reshaping and swapping of axes, like so -

L = 3# Cutting lengthout = a.reshape(-1,L,a.shape[1]).swapaxes(0,1).reshape(L,-1)

Or use np.transpose to swap the axes, like so -

out = a.reshape(-1,L,a.shape[1]).transpose(1,0,2).reshape(L,-1)

Solution 2:

I would use split for this operation:

In [110]: np.hstack(np.split(a,2))
Out[110]:
array([[ 1,  2,  7,  8],
       [ 3,  4,  9, 10],
       [ 5,  6, 11, 12]])

Post a Comment for "Rearranging Numpy Array"