NumPy Transpose

The numpy.transpose() method takes the array as an input, reverses its dimension, and returns the transposed array. In simple terms, the NumPy transpose swaps the rows and columns and returns the modified array as output.

NumPy Transpose Syntax

The syntax of numpy.transpose() function is:

numpy.transpose(arraxes=None)

NumPy Transpose Parameters

The numpy.transpose() method takes 2 parameters:

  • arr – An array-like object as an input argument
  • axes(optional) – If specified, it must be a tuple or list which contains a permutation of [0,1,..,N-1] where N is the number of axes of arr. If not specified, defaults to range(a.ndim)[::-1], which reverses the order of the axes.

NumPy Transpose Return Value

The numpy.transpose() method takes an array as input. It reverses its dimension by moving rows data to columns and columns data to rows and returns the modified array as an output.

Example 1: How to transpose NumPy array in Python?

In the below example, we transpose the NumPy array using the numpy.transpose() method.

import numpy as np

# NumPy Array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Prints original Array
print('Original Array\n', arr)

# Transposes rows to columns and columns to rows 
transposed_arr = np.transpose(arr)

# print the Transposed NumPy Array
print('Transposed Array\n',transposed_arr)

Output

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

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

Example 2: Transpose the NumPy array with an axes

In the below example, we are specifying the axis parameter to transpose the array. This will permute the axes or interchange the rows and columns and returns the transposed array.

from array import array
import numpy as np

# NumPy Array
arr = np.array([[1, 2, 3], [4, 5, 6]])

# Prints original Array
print('Original Array\n', arr)

arr.strides

# Transposes rows to columns and columns to rows along axis
transposed_arr = np.transpose(arr, (1,0))

# print the Transposed NumPy Array
print('Transposed Array\n',transposed_arr)

Output

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

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

Example 3 – Reposition NumPy Array elements using numpy.transpose()

In this example, we are creating a NumPy array using the numpy.ones() method and reposition the array elements using the numpy.transpose() method along with the shape.

from array import array
import numpy as np

# NumPy Array
arr = np.ones((3,4,6,5))  

# Transposes rows to columns and columns to rows 
transposed_arr1 = np.transpose(arr, (1,0,2,3)).shape
transposed_arr2 = np.transpose(arr, (0,1,3,2)).shape

# print the Transposed NumPy Array Shape
print('Transposed Array Shape 1 \n',transposed_arr1)
print('Transposed Array Shape 2 \n',transposed_arr2)

Output

Transposed Array Shape 1 
 (4, 3, 6, 5)
Transposed Array Shape 2
 (3, 4, 5, 6)

Conclusion

NumPy array library has an inbuilt method, numpy.transpose() that will permutes or convert row elements into column elements and column elements into row elements and returns the transposed array as output.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy Repeat

NumPy Repeat

Table of Contents Hide NumPy Repeat SyntaxNumPy Repeat ParametersNumPy Repeat Return ValueExample 1: Repeating the Single Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array…
View Post