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?

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.