Python | Numpy numpy.transpose ()

With Numpy numpy.transpose () we can perform a simple transpose function within one line using numpy.transpose() Numpy … It can transpose two-dimensional arrays, on the other hand, it does not affect one-dimensional arrays. This method transposes a two-dimensional NumPy array.

Parameters:
axes: [None, tuple of ints, or n ints] If anyone wants to pass the parameter then you can but it`s not all required. But if you want than remember only pass (0, 1) or (1, 0) . Like we have array of shape (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 3 and 0 as 2.

Returns: ndarray

Example # 1:
In this example, we see that it is really easy to wrap an array in just one line.

# import a python module named numpy

import numpy as np

 
# create a 3x3 array

gfg = np.array ([[ 1 , 2 , 3 ],

[ 4 , 5 , 6 ],

  [ 7 , 8 , 9 ]])

 
# before transposition

print (gfg, end = `` )

 
# after transposition

print (gfg.transpose ())

Exit:

 [[1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]] 

Example # 2:
In this example, we demonstrate the use of tuples in numpy.transpose () .

# import a python module named numpy

import numpy as np

 
# create a 3x3 array

gfg = np.array ([[ 1 , 2 ],

[ 4 , 5 ],

[ 7 , 8 ]])

 
# before transposition

print (gfg, end = `` )

 
# after transpose

print (gfg.transpose ( 1 , 0 ))

Exit:

 [[1 2] [4 5] [7 8]] [[1 4 7] [2 5 8]]