How does numpy.transpose work for this example?
Asked Answered
M

3

13

I have difficulty understanding how numpy.transpose actually works. For example

a_value = array([[[0, 1],
                  [2, 3]],

                 [[4, 5],
                  [6, 7]]])

and when I do

np.transpose(a_value, (2, 1, 0))

I get

array([[[0, 4],
        [2, 6]],

       [[1, 5],
        [3, 7]]])

How can I derive this transpose manually? I need to understand the formula or the steps intuitively in the above case so I can generalize it for higher dimensions.

Myosin answered 13/10, 2015 at 10:47 Comment(1)
Did you read the documentation, i.e. help(numpy.transpose)? You just "permute" the dimensions. If you pass the permutation (2, 1, 0), then the element that was at position [x,y,z] will now be in position [z,y,x], or in position [z,x,y] for permutation (3, 0, 1)Commodious
H
26

As given in the documentation -

numpy.transpose(a, axes=None)

axes : list of ints, optional By default, reverse the dimensions, otherwise permute the axes according to the values given.

The second argument is the axes using which the values are permuted. That is for example if the index of initial element is (x,y,z) (where x is 0th axes, y is 1st axes, and z is 2nd axes) , the position of that element in the resulting array becomes (z,y,x) (that is 2nd axes first, then 1st axes, and last 0th axes) , based on the argument you provided for axes .

Since you are transposing an array of shape (2,2,2) , the transposed shape is also (2,2,2) , and the positions would change as -

(0,0,0) -> (0,0,0)
(1,0,0) -> (0,0,1)
(0,1,0) -> (0,1,0)
(1,1,0) -> (0,1,1)
...

Since the axes you choose are trivial, lets explain this for another axes. Example -

In [54]: A = np.arange(30).reshape((2, 3, 5))
In [55]: A
Out[55]:
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]]])

In [56]: np.transpose(A,(1,2,0))
Out[56]:
array([[[ 0, 15],
        [ 1, 16],
        [ 2, 17],
        [ 3, 18],
        [ 4, 19]],

       [[ 5, 20],
        [ 6, 21],
        [ 7, 22],
        [ 8, 23],
        [ 9, 24]],

       [[10, 25],
        [11, 26],
        [12, 27],
        [13, 28],
        [14, 29]]])

Here, the first element (0,0,0) becomes the (0,0,0) element in the result.

The second element (0,0,1) becomes the (0,1,0) element in the result. And so on -

(0,0,0) -> (0,0,0)
(0,0,1) -> (0,1,0)
(0,0,2) -> (0,2,0)
...
(2,3,4) -> (3,4,2)
...
Heteromorphic answered 13/10, 2015 at 10:59 Comment(1)
Thanks for the simple explanation.Myosin
O
3

Here's a little more clarification:

Don't confuse the parameters of np.reshape(z, y, x) with those of np.transpose(0, 1, 2).

np.reshape() uses the dimensions of our matrix, think (sheets, rows, columns), to specify its layout.

np.transpose() uses the integers 0, 1, and 2 to represent the axes we want to swap, and correspond to z, y, and x, respectively.

For example, if we have data in a matrix of 2 sheets, 3 rows, and 5 columns...

We can take the next step and think in terms of lists. So, the z, y, x or sheets, rows, columns representation of a 2x3x5 matrix is...

 [[[ 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]]]

...but the module we're feeding this data into requires a layout such that sheet 1 contains the first row of each of our sheets and sheet 2 contains the second row and so on. Then, we'll need to transpose our data with np.transpose(1, 0, 2). This swaps the z and the y axes and transposes the data.

 [[[ 0,  1,  2,  3,  4],
   [15, 16, 17, 18, 19]],

  [[ 5,  6,  7,  8,  9],
   [20, 21, 22, 23, 24]],

  [[10, 11, 12, 13, 14],
   [25, 26, 27, 28, 29]]]

Note the difference from using np.reshape(3, 2, 5) as this doesn't transpose the data--only re-arranges it.

[[[ 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]]]
Ommatophore answered 20/6, 2018 at 19:11 Comment(0)
K
0

The axes= parameter is how you want the starting axes to be arranged in the result.

In my case I have an array of shape (2, 1997, 4, 4), and I just want to transpose the (4, 4) sub-arrays.

axes=() needs 4 parameters here, one for each axis. axes=(0, 1, 2, 3) yields no change, as the requested result ordering is the same as the start ordering.

To transpose just the (4,4) arrays as I want, I use axes=(0, 1, 3, 2) -- just swap the last two axes, please.

Kaliope answered 17/5, 2021 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.