I have a question of manipulating numpy arrays. Say, given a 3-d array in the form np.array([[[1,2],[3,4]], [[5,6],[7,8]]])
which is a (2,2,2)
array. I want to manipulate it into a (2,4)
array such that a = np.array([[1,2,5,6],[3,4,7,8]])
. I want to know is there any built-in methods of numpy particularly dealing with problems like this and can be easily generalized.
EDITED:
Thank you all guys' answers. They all rock! I thought I should clarify what I mean by "easily generalized" in the original post. Suppose given a (6,3,2,3)
array (this is the actual challenge I am facing)
a = array([[[[ 10, 20, 30],
[ 40, 40, 20]],
[[ 22, 44, 66],
[ 88, 88, 44]],
[[ 33, 66, 99],
[132, 132, 66]]],
[[[ 22, 44, 66],
[ 88, 88, 44]],
[[ 54, 108, 162],
[216, 216, 108]],
[[ 23, 46, 69],
[ 92, 92, 46]]],
[[[ 14, 28, 42],
[ 56, 56, 28]],
[[ 25, 50, 75],
[100, 100, 50]],
[[ 33, 66, 99],
[132, 132, 66]]],
[[[ 20, 40, 60],
[ 80, 80, 40]],
[[ 44, 88, 132],
[176, 176, 88]],
[[ 66, 132, 198],
[264, 264, 132]]],
[[[ 44, 88, 132],
[176, 176, 88]],
[[108, 216, 324],
[432, 432, 216]],
[[ 46, 92, 138],
[184, 184, 92]]],
[[[ 28, 56, 84],
[112, 112, 56]],
[[ 50, 100, 150],
[200, 200, 100]],
[[ 66, 132, 198],
[264, 264, 132]]]])
I want to massage it into a (3,3,2,2,3)
array such that fora[0,:,:,:,:]
a[0,0,0,:,:] = np.array([[10,20,30],[40,40,20]]);
a[0,1,0,:,:] = np.array([[22,44,66],[88,88,44]]);
a[0,2,0,:,:] = np.array([[33,66,99],[132,132,66]]);
a[0,0,1,:,:] = np.array([[20,40,60],[80,80,40]]);
a[0,1,1,:,:] = np.array([[44,88,132],[176,176,88]]);
a[0,2,1,:,:] = np.array([[66,132,198],[264,264,132]]).
In short, the last 3 biggest blocks should "merge" with first 3 biggest blocks to form 3 (3,2)
blocks. The rest of 2 blocks i.e., (a[1,:,:,:,:]
, a[2,:,:,:,:]
) follow the same pattern.