3

I have an array that looks like below:

array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7]])

How can I use reshape to divide it into 4 chucks, such that it looks like

array([[[0, 0, 0, 0],  
       [1, 1, 1, 1],  
       [2, 2, 2, 2],  
       [3, 3, 3, 3]],  
       [[0, 0, 0, 0],  
       [1, 1, 1, 1],  
       [2, 2, 2, 2],  
       [3, 3, 3, 3]], 
       [[4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]],
       [[4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]]])

I tried different integer combinations of m, n, l in reshape(m,n,l), but none works.

1
  • and did you intend to get a 3D array or do you want 2D array out? Commented Oct 20, 2013 at 21:33

2 Answers 2

8

Edit: Sorry, I didn't realize it was a 3-d result, not a 4-d result. To get the 3-d one, you would have to reshape once more. And that extra reshape will copy the data.

You can't, you need to tranpose as well:

In [1]: a = np.arange(8)[:,None].repeat(8,axis=1)

In [2]: a
Out[2]: 
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7]])

In [3]: b = a.reshape(2,4,2,4)

In [4]: b
Out[4]: 
array([[[[0, 0, 0, 0],
         [0, 0, 0, 0]],
         ...
        [[7, 7, 7, 7],
         [7, 7, 7, 7]]]])

In [5]: b.transpose(0,2,1,3)
Out[5]: 
array([[[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2],
         [3, 3, 3, 3]],

        [[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2],
         [3, 3, 3, 3]]],


       [[[4, 4, 4, 4],
         [5, 5, 5, 5],
         [6, 6, 6, 6],
         [7, 7, 7, 7]],

        [[4, 4, 4, 4],
         [5, 5, 5, 5],
         [6, 6, 6, 6],
         [7, 7, 7, 7]]]])
Sign up to request clarification or add additional context in comments.

Comments

5

Underneath all numpy arrays (and really, all arrays (not linked lists)) are linear chunks of memory, the higher dimensional nature is put on it by your interpretation. The way to think about it is element [i, j] is really element [i * num_cols + j] in the underlying array.

Numpy takes care of all the striding details for you to let you easily index into the memory using what ever dimentionality you want, however you have the constraint that you can only re-shape the data into arrays where you can write a rule like the above for converting (i,j) -> a single index, which what you want does not.

There are a bunch of ways you can do what you want, but they all involve copying data

In [6]: array([[0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7]]).reshape(-1, 4)[np.r_[range(0, 8, 2), range(1, 8, 2), range(8, 16, 2), range(9, 16, 2)]].reshape(4, 4, 4)
Out[6]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]])

or assuming your array is in a

In [10]: np.vstack([a[:4, :4], a[:4, 4:], a[4:, :4], a[4:, 4:]])
Out[10]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7]]).reshape(4, 4, 4)

or just

np.array([a[:4, :4], a[:4, 4:], a[4:, :4], a[4:, 4:]])

5 Comments

@seberg My understanding of the guts of numpy is apparently limited, can you point me at some details? I assumed that when you are creating the new array (particularly during vstack) you end up with copies.
Basically numpy uses a strided layout, which means you can for example slice/transpose arbitrarily without copying. Reshape will also never copy when you split up dimensions (it may need to copy the other way around). Here, you are right, since it is a reshape + transpose + reshape, you need a copy.
@seberg That is what I thought I was trying to say.
Yes, sorry. The thing is, you wanted a 2-D output, and I wanted a 4-d output :)...
and it looks like the OP really wanted 3D :-p

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.