NumPy ndarray.transpose() Method | Find Transpose of the NumPy Array
Last Updated :
05 Feb, 2024
The ndarray.transpose() function returns a view of the array with axes transposed.
- For a 1-D array, this has no effect, as a transposed vector is simply the same vector.
- For a 2-D array, this is a standard matrix transpose.
- For an n-D array, if axes are given, their order indicates how the axes are permuted. If axes are not provided and arr.shape = (i[0], i[1], … i[n-2], i[n-1]), then arr.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).
Example
Python3
import numpy as np
arr = np.array([[ 5 , 6 ], [ 7 , 8 ]])
transposed_arr = arr.transpose()
print (transposed_arr)
|
Output:
[[5 7]
[6 8]]
Syntax
Syntax : numpy.ndarray.transpose(*axes)
Parameters :
- axes : [None, tuple of ints, or n ints] None or no argument: reverses the order of the axes. tuple of ints: i in the j-th place in the tuple means arr’s i-th axis becomes arr.transpose()’s j-th axis. n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)
Return : [ndarray] View of arr, with axes suitably permuted.
Examples
Let’s look at some examples to of transpose() method of the NumPy library to find transpose of a ndarray:
Example 1 :
Python3
import numpy as np
arr = np.array([[ 5 , 6 ], [ 7 , 8 ]])
gfg = arr.transpose()
print ( gfg )
|
Output :
[[5 7]
[6 8]]
Example 2 :
Python3
import numpy as np
arr = np.array([[ 5 , 6 ], [ 7 , 8 ]])
gfg = arr.transpose(( 1 , 0 ))
print ( gfg )
|
Output :
[[5 7]
[6 8]]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...