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

## [Solved] AttributeError: ‘float’ object has no attribute ‘get’

Table of Contents Hide What is AttributeError: ‘float’ object has no attribute ‘get’?How to fix AttributeError: ‘float’ object has no attribute ‘get’?Solution 1 – Call the get() method on valid…

## IndexError: too many indices for array

The indexerror: too many indices for an array means that you have a declared an array in a different dimension and trying to index it in another dimension. For example,…

## Convert Letters to Numbers in Python

Table of Contents Hide Convert Letters to Numbers in PythonUsing ord() MethodUsing list comprehensionConclusion Using the ord() method, we can convert letters to numbers in Python. The ord() method returns…

## Python – List Files in a Directory

Table of Contents Hide Method 1: Using os.listdir() methodMethod 2: Using os.walk() methodMethod 3: Using os.scan() methodMethod 4: Using glob module There are several modules available in Python to list…

## nxnxn matrix python

Table of Contents Hide What is NxNxN?How to Create NxNxN Matrix in Python?Create NxN Matrix in Python 3 with Non Duplicating numbersCreate NxNxN matrix in Python using numpy In this…

## How to Create a Directory in Python?

Table of Contents Hide Method 1 – Using os.mkdir() functionExample 1 – Create a directory in Python using os.mkdir()Example 2 – Exception if the directory already existsMethod 2 – Using…