Table of Contents
ToggleBy transposing an array in NumPy, we mean to rearrange the dimensions of an array to access its data along different axes.
For a 2-dimensional array (matrix), transposing means flipping the array along its diagonal. This swaps the rows and columns. If you have an array “A” with shape “(m, n)”, the transpose “A.T” will have shape “(n, m)”, where each element at position “(i, j)” in A will be at position “(j, i)” in A.T.
For arrays with more than two dimensions, transposing involves reordering the axes according to a specified order.
The transpose() function in NumPy is used to rearrange the dimensions of an array. It returns a view of the array with its axes rearranged in a specified order.
If the order is not specified, the shape of the returned array is the same as the original array’s shape, but with the dimensions permuted in reverse order. Following is the syntax −
numpy.transpose(a, axes=None)
Where,
In the following example, we are transposing a 2D array “arr” using the numpy.transpose() function with default parameters −
# Open Compiler
import numpy as np
# 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Transposing the array
transposed_arr = np.transpose(arr)
print("Original Array:")
print(arr)
print("\nTransposed Array:")
print(transposed_arr)
Output:
This swaps the rows and columns of the array as shown in the output below −
Original Array:
[[1 2 3]
[4 5 6]]
Transposed Array:
[[1 4]
[2 5]
[3 6]]
In here, we are transposing a 3D array “arr_3d” using the numpy.transpose() function with default parameters −
# Open Compiler
import numpy as np
# 3D array
arr_3d = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
# Transposing a 3D array
transposed_arr_3d = np.transpose(arr_3d)
print("Original 3D Array:")
print(arr_3d)
print("\nTransposed 3D Array:")
print(transposed_arr_3d)
Output:
This changes the order of dimensions, effectively rearranging the depth and height of the array as shown in the output below −
Original 3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Transposed 3D Array:
[[[1 5]
[3 7]]
[[2 6]
[4 8]]]
In the below example, we are rearranging the axes of a 3D array such that the first dimension (axis 0) remains unchanged, while axes “1” and “2” are swapped using the numpy.transpose() function −
# Open Compiler
import numpy as np
# 3D array
arr = np.array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
# Transposing
transposed_arr = np.transpose(arr, axes=(0, 2, 1))
print("Original 3D Array:")
print(arr)
print("\nTransposed 3D Array:", transposed_arr)
Output:
Following is the output obtained −
Original 3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Transposed 3D Array:
[[[1 3]
[2 4]]
[[5 7]
[6 8]]]
NumPy arrays have a convenient attribute .T that provides a quick way to transpose arrays without needing to call the transpose() function explicitly. In other words, it reverses the axes of multi-dimensional arrays without any additional arguments.
In this example, we are using the .T attribute in NumPy to transpose the array “arr” −
# Open Compiler
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Transpose the array
transposed_arr = arr.T
print("Original Array:")
print(arr)
print("\nTransposed Array using .T:")
print(transposed_arr)
Output:
The result produced is as follows −
Original Array:
[[1 2 3]
[4 5 6]]
Transposed Array using .T:
[[1 4]
[2 5]
[3 6]]
Key Takeaway: Master transposing NumPy arrays efficiently at Vista Academy!
