Reputation: 273
I have the follow 3d numpy array in shape (3, 2, 3).
[
[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]]
]
However, I need to reshape into (3, 3, 2) in the follow order:
[
[[ 0 3]
[ 6 9]
[12 15]]
[[ 1 4]
[ 7 10]
[13 16]]
[[ 2 5]
[ 8 11]
[14 17]]
]
I am currently using Jupyter with a lot of trial and error.
Thank you for any suggestions!
Upvotes: 1
Views: 1968
Reputation: 54213
To define the original array, you can use :
np.arange(18).reshape(3,2,3)
As mentioned by @Divakar in the comments, you can use :
np.arange(18).reshape(3,2,3).transpose(2,0,1)
to get the desired result.
From np.transpose
documentation:
axes: By default, reverse the dimensions, otherwise permute the axes according to the values given.
2,0,1
is the permutation needed to go from (3,2,3)
shape to (3,3,2)
. It would also convert a (3400, 7, 100)
shape into (100, 3400, 7)
.
Another method would be to use np.rollaxis
(another hint from @Divakar):
np.rollaxis(np.arange(18).reshape(3,2,3),2)
Upvotes: 3