venkysmarty
venkysmarty

Reputation: 11431

tranpose of multidimension with permute the axes

For higher dimensional arrays, transpose will accept a tuple of axis numbers to permute the axes (for extra mind bending):

In [115]: arr = np.arange(16).reshape((2, 2, 4))

In [116]: arr
Out[116]:

array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])

In [117]: arr.transpose((1, 0, 2))
Out[117]:

array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])

I am trying to understand how above output is generated for transpose((1, 0, 2). I am not able to understand what is meant is permute the axes? Request to explain in layman terms and how above output is generated.

Thanks

Upvotes: 0

Views: 664

Answers (3)

Nyps
Nyps

Reputation: 886

Transposing a matrix is essentially switching the order of your dimensions, e.g.: arr.transpose() is in this case equal to arr.transpose((2,1,0)).

On the other hand, if you wish to select the order of your dimensions by hand, you will preserve the original order (i.e. don't change anything) by transposing with arr.transpose((0,1,2)).

In your example you leave the last dimension (2) of your array, e.g. [0,1,2,3], unchanged. However, you switch the first two (0 and 1), thus elements at arr[0,0,0:4] will still be there, but the contents of arr[1,0,0:4] now appear at arr[0,1,0:4] after transposing:

In[]: t_arr = arr.transpose((1,0,2))
In[]: arr[0,0,0:4] == t_arr[0,0,0:4]
Out[]: 
array([ True,  True,  True,  True], dtype=bool)

In[]: arr[0,1,0:4] == t_arr[1,0,0:4]   # indices #0,1,0-3 and #1,0,0-3 respectively
Out[]: 
array([ True,  True,  True,  True], dtype=bool)

This is also what you expect when transposing a multidimensional matrix. The axes are swapped in a way, so that the elements are located at almost the same indices as before, just with the swapped order, i.e.:

arr[x,y,z] == arr.transpose()[z,y,x]           # True
arr[x,y,z] == arr.transpose((1,0,2))[y,x,z]    # True (your case)

Upvotes: 0

MB-F
MB-F

Reputation: 23637

Consider an array and its transpose:

arr = np.arange(24).reshape((2, 3, 4))
arrt = arr.transpose((1, 0, 2))

By default transpose just reverses the order of dimensions. The particular transpose command above swaps the first two dimensions but leaves the last dimension untouched. Let's verify in a few examples:

print(arr.shape)
# (2, 3, 4)

print(arrt.shape)
# (3, 2, 4)

# the last dimension is the same in both arrays
print(arr[0, 0, :])
# [0 1 2 3]
print(arrt[0, 0, :])
# [0 1 2 3]

print(arr[:, 0, 0])  # what was before the first dimension
# [ 0 12]
print(arrt[0, :, 0])  # is now the second dimension
# [ 0 12]

# the first two dimensions are swapped - the submatrix is transposed
print(arr[:, :, 0])
# [[ 0  4  8]
#  [12 16 20]]
print(arrt[:, :, 0])
# [[ 0 12]
#  [ 4 16]
#  [ 8 20]]

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

The default transpose is to reverse the axes, so an A x B matrix becomes B x A. For 3D, the default would be to transpose A x B x C to C x B x A.

In your example, transpose(1, 0, 2), it will transpose A x B x C to B x A x C. That's because the default 3D transpose is (2, 1, 0), but you have (1, 0, 2) which simply swaps the first two axes.

When you are experimenting, it may be more clear if you use an example array of shape 2 x 3 x 4 or some other combination which has no duplicates.

Upvotes: 1

Related Questions