Numpy swapaxes – Python NumPy swapaxes() Function

NumPy swapaxes() Function:

Numpy swapaxes: The swapaxes() function of the NumPy module is used to interchange two axes of an array.

Syntax:

numpy.swapaxes(a, axis1, axis2)

Parameters

a: This is required.  It is an input array.

axis1: This is required. It specifies the first axis.

axis2: This is required. It specifies the second axis.

Return value:

If “a” is an ndarray, it is returned as a view (a’s view); otherwise, a new array is created.

NumPy swapaxes() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Pass the other list(multi-dimensional) as an argument to the array() function to create another array.
  • Store it in another variable.
  • Print the above-given array1 (1D).
  • Swap the axis of the above-given array1 using the swapaxes() function of numpy module by giving random axis1, axis2 values as arguments to it and print the result.
  • Print the above-given array2(multi-dimensional).
  • Swap the axis of the above-given array2 using the swapaxes() function of numpy module by giving random axis1, axis2 values as arguments to it and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to create an array. 
# Store it in a variable.
gvn_ary1 = np.array([[8,7,6]])
# Pass the other list(multi-dimensional) as an argument to the array() function to
# create another array. 
# Store it in another variable.
gvn_ary2 = np.array([[[10,11,12]],
              [[13,15,16]]])

# Print the above given array1 (1D).
print("The above given array1(1D) is:")
print(gvn_ary1)

# Swap the axis of the above given array1 using the swapaxes()
# function of numpy module by giving random axis1, axis2 values as
# arguments to it and print the result.
print("The given array1 after swapping its axis:")
print(np.swapaxes(gvn_ary1, 0, 1))

# Print the above given array2(multi-dimensional).
print("The above given array2(multi-dimensional) is:")
print(gvn_ary2)
print()
# Swap the axis of the above given array2 using the swapaxes()
# function of numpy module by giving random axis1, axis2 values as
# arguments to it and print the result.
print("The given array2 after swapping its axis:")
print(np.swapaxes(gvn_ary2, 0, 2))

Output:

The above given array1(1D) is:
[[8 7 6]]
The given array1 after swapping its axis:
[[8]
 [7]
 [6]]
The above given array2(multi-dimensional) is:
[[[10 11 12]]

 [[13 15 16]]]

The given array2 after swapping its axis:
[[[10 13]]

 [[11 15]]

 [[12 16]]]

Example2

Approach:

  • Import numpy module using the import keyword.
  • Create an array of length some random number n using the arange() function (which creates an array of values 0 to n-1) and apply the reshape() function by passing the shape values to it.
  • Store it in a variable.
  • Print the given array.
  • Swap the axis of the above-given array using the swapaxes() function of numpy module by giving random axis1, axis2 values as arguments to it and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create an array of length some random number n using the arange() function
#(which creates an array of values 0 to n-1) and apply reshape() function
# by passing the shape values to it.
gvn_arry = np.arange(27).reshape(3,3,3)
# Print the given array
print("The given array is:")
print(gvn_arry)

# Swap the axis of the above given array using the swapaxes()
# function of numpy module by giving random axis1, axis2 values as
# arguments to it and print the result.
print("The given array after swapping its axis:")
print(np.swapaxes(gvn_arry, 0, 1))

Output:

The given array is:
[[[ 0 1 2]
 [ 3 4 5]
 [ 6 7 8]]

[[ 9 10 11]
 [12 13 14]
 [15 16 17]]

[[18 19 20]
 [21 22 23]
 [24 25 26]]]
The given array after swapping its axis:
[[[ 0 1 2]
 [ 9 10 11]
 [18 19 20]]

[[ 3 4 5]
 [12 13 14]
 [21 22 23]]

[[ 6 7 8]
 [15 16 17]
 [24 25 26]]]