First, it is better to index your array like a[0,0]
or a[0,0,...]
or a[0,0,:,:]
.
Does a.reshape(2,2, 4,4)
do what you want? That retains all values, but reshapes the inner most (2,2)
arrays into (4,)
arrays.
Looking more closely it looks like you want to reorder values in the inner arrays. That is not entirely obvious from your description. I had to carefully match up values between the displays. In particular it's only 2 of the subarrays that are unabiguous:
[[ 0. 0.]
[ 0. 1.]]
[[ 0. 0.]
[ 1. 0.]]]
[0. 0. 0. 0.]
[0. 1. 1. 0.]]
What we will need to do is transpose the last 2 axes before reshaping.
This might do the trick:
a.transpose(0,1,2,4,3).reshape(2,2,4,4)
It works with np.ones((2,2,4,2,2))
, but I haven't tested it with numbers that duplicate your question - because you did not give a test case that can be cut-n-pasted.
oops - looks like Divakar
got it right. We need to break up the 4
dimension into 2,2
, and perform the transpose across dimensions.
In [290]: a=np.array([[[0,1],[0,0]],[[0,0],[0,1]],[[0,0],[0,1]],[[0,0],[1,0]]])
In [291]: a
Out[291]:
array([[[0, 1],
[0, 0]],
[[0, 0],
[0, 1]],
[[0, 0],
[0, 1]],
[[0, 0],
[1, 0]]])
In [292]: a.reshape(2,2,2,2).transpose(0,2,1,3).reshape(4,4)
Out[292]:
array([[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 1, 1, 0]])
Here's a clearer example:
In [303]: a=np.arange((4*2*2)).reshape(4,2,2)
In [304]: a.reshape(2,2,2,2).transpose(0,2,1,3).reshape(4,4)
Out[304]:
array([[ 0, 1, 4, 5],
[ 2, 3, 6, 7],
[ 8, 9, 12, 13],
[10, 11, 14, 15]])
It would be even better if each of the dimensions was different, so there'd even less ambiguity as to which ones are being combined.
In [308]: a=np.arange((6*5*7)).reshape(6,5,7)
In [309]: a.reshape(2,3,5,7).transpose(0,2,1,3).reshape(10,21)