1
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 answer should be:

answer = np.array([[1,2,7,8],
              [3,4, 9, 10],
              [5,6, 11, 12]])

I tried as

ans = a.reshape(3,-1)    
print ans

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

But answer is wrong. How to do it?

Roman
  • 3,007
  • 8
  • 26
  • 54

2 Answers2

2

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

L = 3 # Cutting length
out = 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)
Divakar
  • 218,885
  • 19
  • 262
  • 358
0

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]])
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87