0

I have a [? 5 5] array: (?=3 in this case)

[[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]
 [40 41 42 43 44]
 [45 46 47 48 49]]
[[50 51 52 53 54]
 [55 56 57 58 59]
 [60 61 62 63 64]
 [65 66 67 68 69]
 [70 71 72 73 74]]]

I want to have 5 (the # of row) separate array have (? 5) like this:

[array([[ 0,  1,  2,  3,  4],
   [25, 26, 27, 28, 29],
   [50, 51, 52, 53, 54]]), 
 array([[ 5,  6,  7,  8,  9],
   [30, 31, 32, 33, 34],
   [55, 56, 57, 58, 59]]), 
 array([[10, 11, 12, 13, 14],
   [35, 36, 37, 38, 39],
   [60, 61, 62, 63, 64]]), 
 array([[15, 16, 17, 18, 19],
   [40, 41, 42, 43, 44],
   [65, 66, 67, 68, 69]]), 
 array([[20, 21, 22, 23, 24],
   [45, 46, 47, 48, 49],
   [70, 71, 72, 73, 74]])]

Is this a simple preferably one/two numpy operation way to do this?

3
  • I'd propose renaming the question into Numpy array [3 5 5] to [5 3 5] or ...to 5x [3 5] to make it more useful to others. Apparently the word transpose is not obvious to everybody looking at multidimensional data for the first time, so this is where the value in keeping this question would be. Otherwise I'm sure this would be a duplicate to other existing transpose questions. Commented Apr 11, 2016 at 17:48
  • @roadrunner66 I use ?, since it could be anything. Do you think changing it to 5 is better for others? Thanks! Commented Apr 11, 2016 at 20:13
  • Yeah, I'm not sure, I just wanted to find away that others with a similar problem (who don't know the word transpose) will find your question. Maybe reorder dimensions of numpy array ? Commented Apr 11, 2016 at 22:10

2 Answers 2

2

Yes, there is : numpy.transpose. You get to chose any sequence in the axes of your 3D structure.

import numpy as np

aaa=np.array([[[ 0 , 1,  2,  3,  4],
 [ 5,  6,  7,  8,  9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]],
[[25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34],
 [35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44],
 [45, 46, 47, 48, 49]],
[[50, 51, 52, 53, 54],
 [55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64],
 [65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]])

bb= np.transpose(aaa,axes=[1,0,2])
print bb

output:

[[[ 0  1  2  3  4]
  [25 26 27 28 29]
  [50 51 52 53 54]]

 [[ 5  6  7  8  9]
  [30 31 32 33 34]
  [55 56 57 58 59]]

 [[10 11 12 13 14]
  [35 36 37 38 39]
  [60 61 62 63 64]]

 [[15 16 17 18 19]
  [40 41 42 43 44]
  [65 66 67 68 69]]

 [[20 21 22 23 24]
  [45 46 47 48 49]
  [70 71 72 73 74]]].

To access the subarrays, just use indexing like so:

 c= b[0]
 print c

Output:

 [[ 0  1  2  3  4]
 [25 26 27 28 29]
 [50 51 52 53 54]]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Then, should I apply split to get five arrays? Can I do it in one operation?
No split needed. To access the five subarrays, just use indexing : print b[0] print b[1] etc. or reassign to whatever you like : c1=b[0] etc.
@SungKim Additionally, you could just write a, b, c, d, e = np.transpose(aaa, axes=(1, 0, 2)).
2

Just to propose an alternative solution, np.swapaxes is another alternative to np.transpose when only a pair of axes is involved.

a, b, c, d, e = arr.swapaxes(0, 1) # swap axes 0, 1

swapaxes will always return a view of the array, with the same result as the np.transpose proposed by @roadrunner66. It is usually just sightly faster than transpose and heavily used inside numpy's code to put the important dimension up-front.

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.