numpy.transpose() in Python

The Python numpy.transpose() function permutes dimensions of the array.

Basic Syntax

Following is the basic syntax for numpy.transpose() function in Python:

numpy.transpose(arr, axis=None)

And the parameters are:

Parameter Description
arr The input array
axis [default = None, OPTIONAL] Along the axis like 1 or 0.

Return Value

This function returns array with its axis permuted. Always a view is returned if possible.

Example’s

Following are the examples for numpy.transpose() function:

Example 1

# Python Program illustration of numpy.transpose() function 
import numpy as np 

# input array 
input_array = np.array([[1, 2, 3, 4], 
				[5, 6, 7, 8], 
				[9, 10, 11, 12]]) 

print('array before transpose\n') 
print(input_array) 
print('\n\n')

print('array after transpose\n') 
print(input_array.transpose())   

The output for the above program is as given below:

array before transpose

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

array after transpose

[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]

Example 2

# Python Program illustration of numpy.transpose() function 
import numpy as np 

# input array 
input_array = np.array([[1, 2, 3], 
				[4, 5, 6]]) 

print('array before transpose\n') 
print(input_array) 
print('\n\n')

print('array after transpose\n') 
print(input_array.transpose()) 

The output for the above program is as given below:

array before transpose

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

array after transpose

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