NumPy - Transposing Arrays

Transposing NumPy Array

By 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.

Transposing Arrays Using transpose() Function

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,

  • a − It is the array-like object to be transposed.
  • axes (Optional) − It specifies the new order of axes. If not provided, it defaults to reversing the dimensions of the array.

Example: Transposing a 2D Array

In the following example, we are transposing a 2D array “arr” using the numpy.transpose() function with default parameters −

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)

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]]

Example: Transposing a 3D Array

In here, we are transposing a 3D array “arr_3d” using the numpy.transpose() function with default parameters −

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)

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]]]

Example: Transposing with Specified Axes

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 −

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)

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]]]

Transposing Arrays Using “ndarray.T” Object

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 reverse the axes of multi-dimensional arrays without any additional arguments.

Example

In this example, we are using the .T attribute in NumPy to transpose the array “arr” −

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)

The result produced is as follows −

Original Array:
[[1 2 3]
 [4 5 6]]

Transposed Array using .T:
[[1 4]
 [2 5]
 [3 6]]
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team