# 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 ## Python slice()

Table of Contents Hide slice() Syntax slice() Parametersslice() Return ValueExample 1: Python slice string Get substring using slice objectExample 2: Get substring using negative indexExample 3: Python slice list or Python slice arrayExample 4: Python… ## TypeError: only integer scalar arrays can be converted to a scalar index

Python numpy throws typeerror: only integer scalar arrays can be converted to a scalar index when you try to convert the ordinary array into a scalar index. The other cause… ## TypeError: string indices must be integers

In Python, the iterable objects are indexed using numbers. If you try to access the iterable objects using string, you will get typeerror: string indices must be integers. Python TypeError:… ## Python String find()

Table of Contents Hide SyntaxParametersReturn ValueDifference between find() method and index() methodExample 1: find() method without any argumentsExample 2: find() method with start and end Arguments The Python String find()… ## Python ValueError: could not convert string to float

Table of Contents Hide ValueError: could not convert string to floatException could not convert string to float Fix ValueError: could not convert string to floatSolution 1: Ensure the string has… ## Python bytes()

Table of Contents Hide bytes() Syntax bytes() Parametersbytes() Return ValueExample 1: Create a byte of a given integer sizeExample 2: Convert string to bytesExample 3: Convert iterable list to bytesExample 4:…