close

How does NumPy’s transpose() method permute the axes of an array?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How does NumPy’s transpose() method permute the axes of an array in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How does NumPy’s transpose() method permute the axes of an array?

  1. How does NumPy's transpose() method permute the axes of an array?

    In this case your new array dimensions are again [2][2][4], only because axes 0 and 1 had the same size (2).

  2. NumPy's transpose() method permute the axes of an array

    In this case your new array dimensions are again [2][2][4], only because axes 0 and 1 had the same size (2).

Method 1

As explained in the documentation:

By default, reverse the dimensions, otherwise permute the axes according to the values given.

So you can pass an optional parameter axes defining the new order of dimensions.

E.g. transposing the first two dimensions of an RGB VGA pixel array:

 >>> x = np.ones((480, 640, 3))
 >>> np.transpose(x, (1, 0, 2)).shape
 (640, 480, 3)

Method 2

In C notation, your array would be:

int arr[2][2][4]

which is an 3D array having 2 2D arrays. Each of those 2D arrays has 2 1D array, each of those 1D arrays has 4 elements.

So you have three dimensions. The axes are 0, 1, 2, with sizes 2, 2, 4. This is exactly how numpy treats the axes of an N-dimensional array.

So, arr.transpose((1, 0, 2)) would take axis 1 and put it in position 0, axis 0 and put it in position 1, and axis 2 and leave it in position 2. You are effectively permuting the axes:

0 -\/-> 0
1 -/\-> 1
2 ----> 2

In other words, 1 -> 0, 0 -> 1, 2 -> 2. The destination axes are always in order, so all you need is to specify the source axes. Read off the tuple in that order: (1, 0, 2).

In this case your new array dimensions are again [2][2][4], only because axes 0 and 1 had the same size (2).

More interesting is a transpose by (2, 1, 0) which gives you an array of [4][2][2].

0 -\ /--> 0
1 --X---> 1
2 -/ \--> 2

In other words, 2 -> 0, 1 -> 1, 0 -> 2. Read off the tuple in that order: (2, 1, 0).

>>> arr.transpose((2,1,0))
array([[[ 0,  8],
        [ 4, 12]],

       [[ 1,  9],
        [ 5, 13]],

       [[ 2, 10],
        [ 6, 14]],

       [[ 3, 11],
        [ 7, 15]]])

You ended up with an int[4][2][2].

You’ll probably get better understanding if all dimensions were of different size, so you could see where each axis went.

Why is the first inner element [0, 8]? Because if you visualize your 3D array as two sheets of paper, 0 and 8 are lined up, one on one paper and one on the other paper, both in the upper left. By transposing (2, 1, 0) you’re saying that you want the direction of paper-to-paper to now march along the paper from left to right, and the direction of left to right to now go from paper to paper. You had 4 elements going from left to right, so now you have four pieces of paper instead. And you had 2 papers, so now you have 2 elements going from left to right.

Sorry for the terrible ASCII art. ¯\_(ツ)_/¯

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read