Skip to content
Related Articles

Related Articles

Numpy ndarray.transpose() function | Python

Improve Article
Save Article
Like Article
  • Last Updated : 22 Apr, 2020

numpy.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]).

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.

Code #1 :




# Python program explaining
# numpy.ndarray.transpose() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[5, 6], [7, 8]])
  
gfg = arr.transpose()
  
print( gfg )

Output :

[[5 7]
 [6 8]]

 
Code #2 :




# Python program explaining
# numpy.ndarray.transpose() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[5, 6], [7, 8]])
  
gfg = arr.transpose((1, 0))
  
print( gfg )

Output :

[[5 7]
 [6 8]]
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!