This can be done with a reshape and transpose (and a final reshape)
In [195]: arr = np.arange(20).reshape(5,4)
In [196]: arr
Out[196]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
In [197]: arr.reshape(5,2,2)
Out[197]:
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15]],
[[16, 17],
[18, 19]]])
In [198]: arr.reshape(5,2,2).transpose(1,0,2)
Out[198]:
array([[[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13],
[16, 17]],
[[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15],
[18, 19]]])
Identifying the right transpose (or swapaxes
) may require a bit of trial and error.
In [199]: arr.reshape(5,2,2).transpose(1,0,2).reshape(-1,2)
Out[199]:
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13],
[16, 17],
[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15],
[18, 19]])
The equivalent with split and concatenate:
In [200]: np.concatenate(np.hsplit(arr,2), axis=0)
Out[200]:
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13],
[16, 17],
[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15],
[18, 19]])
The transpose route should be faster.