6

I have a very big array with the shape = (32, 3, 1e6) I need to reshape it to this shape = (3, 32e6)

On a snippet, how to go from this::

>>> m3_3_5
array([[[8, 4, 1, 0, 0],
        [6, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[8, 7, 1, 0, 3],
        [2, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[2, 4, 0, 2, 3],
        [2, 5, 5, 3, 2],
        [1, 1, 1, 1, 1]]])

to this::

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

I did try various combinations with reshape with no success::

>>> dd.T.reshape(3, 15)
array([[8, 8, 2, 6, 2, 2, 1, 1, 1, 4, 7, 4, 8, 8, 5],
       [1, 1, 1, 1, 1, 0, 5, 5, 5, 1, 1, 1, 0, 0, 2],
       [5, 5, 3, 1, 1, 1, 0, 3, 3, 2, 2, 2, 1, 1, 1]])

>>> dd.reshape(15, 3).T.reshape(3, 15)
array([[8, 0, 8, 2, 1, 8, 0, 8, 2, 1, 2, 2, 5, 2, 1],
       [4, 0, 5, 1, 1, 7, 3, 5, 1, 1, 4, 3, 5, 1, 1],
       [1, 6, 5, 1, 1, 1, 2, 5, 1, 1, 0, 2, 3, 1, 1]])
3
  • 3
    You probably need a transpose in addition to a reshape. I'm sure you'll get answers but you'll probably learn more by experimenting yourself. Commented Feb 18, 2016 at 0:41
  • 1
    Indeed. And for what it's worth, I just found the appropriate transpose by trying a few things until one worked. :-) Commented Feb 18, 2016 at 0:42
  • Try transpose with an argument, something like transpose([2,0,1]) (just a guess). Commented Feb 18, 2016 at 1:02

2 Answers 2

3

a.transpose([1,0,2]).reshape(3,15) will do what you want. (I am basically following comments by @hpaulj).

In [14]: a = np.array([[[8, 4, 1, 0, 0],
        [6, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[8, 7, 1, 0, 3],
        [2, 8, 5, 5, 2],
        [1, 1, 1, 1, 1]],

       [[2, 4, 0, 2, 3],
        [2, 5, 5, 3, 2],
        [1, 1, 1, 1, 1]]])

In [15]: a.transpose([1,0,2]).reshape(3,15)
Out[15]: 
array([[8, 4, 1, 0, 0, 8, 7, 1, 0, 3, 2, 4, 0, 2, 3],
       [6, 8, 5, 5, 2, 2, 8, 5, 5, 2, 2, 5, 5, 3, 2],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
Sign up to request clarification or add additional context in comments.

1 Comment

ok it clear now, I was missing the transpose *axes argument for nD array n>2
3

You can get the desired behavior with np.hstack

# g is your (3,3,5) array from above
reshaped = np.hstack(g[i,:,:] for i in range(3))  #uses a generator exp
reshaped_simpler = np.hstack(g) # this produces equivalent output to the above statmement
print reshaped # (3,30)

Output

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

2 Comments

yes, the trivial m3_3_5.reshape(3, 15) does not provide the expected result
You can simplify this to np.hstack(g) or np.concatenate(g, axis=-1). It iterates of the input, effectively splitting it on the 1st axis, and then reassembles it on the last axis. This reorders the values as desired (in this case).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.