# numpy.transpose() Function

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

## Syntax

The syntax of `numpy.transpose()` function is:

``numpy.transpose(arr, axes=None)``

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

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

The `numpy.transpose()` function transposes row elements into column elements and column elements into row elements and returns the transposed array as output.

Reference: NumPy Library

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 ## ValueError: invalid literal for int() with base 10

ValueError occurs when we pass an invalid argument type. The error is raised when we call int() function with string argument which Python cannot parse and throws ValueError: invalid literal for… ## TypeError: ‘str’ object does not support item assignment

Table of Contents Hide How to Reproduce the ErrorHow to Fix the ErrorSolution 1 – Create a new string by iterating the old stringSolution 2- Creating a new string using… ## How to Reverse a String in Python

Table of Contents Hide Reverse a string in PythonUsing extended slice operatorUsing for loopUsing while loopUsing recursionUsing reversed() method Python has many functions for string manipulation. However, the Python string… ## Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module… ## Python String isdecimal()

Table of Contents Hide SyntaxParametersReturn ValueExample 1: Working of isdecimal() method in PythonExample 2: String Containing digits and Numeric Characters The Python String isdecimal() method is a built-in function that… ## Python String endswith()

Table of Contents Hide SyntaxParameterReturn ValueExample 1: endswith() method without start and end ParametersExample 2: endswith() method with start and end ParametersPassing Tuple to endswith() Python String endswith() method is…