NumPy transpose() function
In this tutorial, we will cover the
numpy.transpose() function in the NumPy library of Python.
transpose() function in the numpy library is mainly used to reverse or permute the axes of an array and then it will return the modified array.
The main task of this function is to change the column elements into the row elements and the column elements into the row elements.
This function has no effect on 1-D arrays and thus it is used for 2-D arrays.
The syntax required to use this function is as follows:
Let us take a look at the parameters of this function:
This parameter indicates the input array
It is basically an optional parameter, but if you will pass this paramter then remember you should pass (0, 1) or (1, 0). Like we have array of shape (4, 3) to change it (3, 4) you should pass (1, 0) where 1 as 4 and 0 as 3.
transpose() function will return the input array with its permuted axes.
Let us have a look at the basic example of this function and the code snippet for the same is as follows:
import numpy as np a = np.array([[6, 2, 4], [8, 5, 6], [7, 1, 9]]) print("Before Transpose array is :\n",a) x=np.transpose(a) print("After Transpose the array is:\n",x)
Now we will use
transpose() function along with the axis parameter:
import numpy as np a = np.array([[6, 28], [8, 56], [7, 19]]) print("Before Transpose :\n",a) x=np.transpose(a,(1,0)) print("After Transpose :\n",x)
This tutorial was all about
transpose() function in the Numpy Library. We have covered how it is used with its syntax, its parameters and values returned by this function.