numpy.swapaxes() in Python
The numpy.swapaxes() function interchanges the two specified axes of the given array.
Syntax
numpy.swapaxes(a, axis1, axis2)
Parameter
The numpy.swapaxes() method consists of three parameters, which are as follows:
a : This parameter represents the Input array.
axis1 : It represents the first axis.
axis2 : It signifies the second axis.
Return
This function returns the view of parameter ‘a’ if ‘a’ is a ndarray, else a new array is created.
Example 1
# Python Program explaining
# numpy.swapaxes() function
import numpy as np
array = np.arange(8).reshape(2,2,2)
print ('Original array:')
print (array,"\n")
# swaping the numbers between axis 1 and axis 2 (along width)
print ('The array after applying the swapaxes() function:')
print (np.swapaxes(array, 2, 1))
Output
Original array:
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
The array after applying the swapaxes() function:
[[[0 2]
[1 3]]
[[4 6]
[5 7]]]