How to transpose numpy array in python using transpose() function

Spread the love

In this article, you will learn how to transpose numpy array in python using transpose() function.

you need to learn syntax, parameters before using this function.

Syntax: ndarray.transpose(*axes)
Returns a view of the array with axes transposed.

For a 1-D array, this has no effect. (To change between column and row vectors, first cast the 1-D array into a matrix object.) For a 2-D array, this is the usual matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and a.shape = (i[0], i[1], … i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], … i[1], i[0]).

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 a’s i-th axis becomes a.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)
Returns:
out : ndarray
View of a, with axes suitably permuted.

import numpy as np a = np.array([[1, 2, 3], [4,5,6]]) b= a.transpose() print("Transpose Array",b) b= a.transpose(1,0) print("Transpose Array",b) b= a.transpose(0,1) print("Transpose Array",b)

Output:

Transpose Array [[1 4]
[2 5]
[3 6]]
Transpose Array [[1 4]
[2 5]
[3 6]]
Transpose Array [[1 2 3]
[4 5 6]]