The transpose()
method swaps the axes of the given array similar to the transpose of a matrix in mathematics. In the case of arrays with more than two dimensions, transpose()
permutes the axes based on the given argument.
import numpy as np
originalArray = np.array([[1, 2], [3, 4]])
# swaps the rows and columns of an array
transposedArray = np.transpose(originalArray)
print(transposedArray)
'''
Output:
[[1 3]
[2 4]]
'''
transpose() Syntax
The syntax of transpose()
is:
numpy.transpose(array, axes = None)
transpose() Arguments
The transpose()
method takes two arguments:
array
- the array to be transposedaxes
(optional) - the axes of the transposed matrix (tuple
orlist
of integers )
transpose() Return Value
The transpose()
method returns an array
with axes permuted.
Example 1: Transposing an Array Without Axes Argument
import numpy as np
originalArray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# if axes argument is skipped, the shape of array is reversed
transposedArray = np.transpose(originalArray)
print(transposedArray)
Output
[[1 4 7] [2 5 8] [3 6 9]]
Transposing a two-dimensional NumPy array is the same as transposing a matrix.
Example 2: Transposing a 1D Array
If we use the transpose()
method on a one-dimensional array, the method returns the original array.
import numpy as np
originalArray = np.array([1, 2, 3, 4])
# try to swap the rows and columns of the given array
transposedArray = np.transpose(originalArray)
print(transposedArray)
Output
[1 2 3 4]
Note: The transpose()
method cannot increase the dimension of an array. Hence, the method cannot convert a unidirectional row array to a column array.
Example 3: Transposing a 3D Array
The axes argument (second argument) defines which axes are swapped.
import numpy as np
originalArray = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
# swap the rows and columns of the array based on given axes
print('When axes are (2, 0, 1):')
transposedArray = np.transpose(originalArray, (2, 0, 1))
print(transposedArray)
# swap the rows and columns of the array based on given axes
print('When axes are (0, 2, 1):')
transposedArray = np.transpose(originalArray, (0, 2, 1))
print(transposedArray)
Output
When axes are (2, 0, 1): [[[0 2] [4 6]] [[1 3] [5 7]]] When axes are (0, 2, 1): [[[0 2] [1 3]] [[4 6] [5 7]]]
Note: NumPy uses indices to represent dimensions, unlike traditional concepts like x, y, and z. The dimensions are represented as dimension 0, dimension 1, and so on.
Thus, the axes argument cannot have repeated elements since a dimension can only appear once.
1. Using (2, 0, 1) as the second argument to transpose()
In the original array, (x, y, z) are represented by (0, 1, 2).
If we use (2, 0, 1) as the second argument to the transpose()
method, it means
- z-axis (index 2) becomes the new x-axis
- x-axis (index 0) becomes the new y-axis
- y-axis (index 1) becomes the new z-axis
2. Using (0, 2, 1) as the second argument to transpose()
If we use (0, 2, 1) as the second argument to the transpose()
method, it means
- x-axis (index 0) will remain unchanged
- z-axis (index 2) becomes the new y-axis
- y-axis (index 1) becomes the new z-axis
Note: It is hard to visualize the working of transpose()
for arrays having more than two dimensions. To learn more, visit working of transpose() on a 3D array.