Facebook Page Twitter Page LinkedIn Page
× NumPy Tutorial NumPy Resources

The NumPy transpose() function is used to reverse or permute the axes of an array and returns the modified array. For a 2-D array, the function returns matrix transpose.


numpy.transpose(a, axes=None)


a Required. Specify the input array.
axes Optional. Specify a tuple or list which contains a permutation of [0,1,..,N-1] where N is the number of axes of a. The ith axis of the returned array will correspond to the axis numbered axes[i] of the input. If not specified, defaults to range(a.ndim)[::-1], which reverses the order of the axes.

Return Value

Returns a with its axes permuted.

Example: matrix transpose

In the below example, the function is used to return matrix transpose.

import numpy as np
Arr = np.array([[10,20,30],[100, 200,300]])

print("Array is:")

#creating matrix transpose
print("\nMatrix transpose is:")

The output of the above code will be:

Array is:
[[ 10  20  30]
 [100 200 300]]

Matrix transpose is:
[[ 10 100]
 [ 20 200]
 [ 30 300]]

Example: transpose() with axes parameter

The axes parameter can be used to permute the axes of the array. Consider the following example.

import numpy as np
#creating a 3-D array of ones
Arr1 = np.ones((10,5,2))

#permute axes
Arr2 = np.transpose(Arr1, (2,0,1))

#checking the shape of permuted array

The output of the above code will be:

(2, 10, 5)

❮ NumPy - Functions