Numpy Transpose Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the numpy.transpose() function in Python NumPy library. We will learn how this function is used to permute or reverse the axes of an array. By the end of the lab, you will be able to use numpy.transpose() to modify arrays with ease.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the NumPy library

The first step is to import the NumPy library, which will allow us to use the numpy.transpose() function. You can do this by running the following code:

import numpy as np

Create a NumPy array

The next step is to create a NumPy array that we will use to demonstrate how the numpy.transpose() function works. You can create a NumPy array by running the following code:

a = np.array([[1,2,3],[4,5,6]])
print("The original array is:\n",a)

The output will be:

The original array is:
 [[1 2 3]
 [4 5 6]]

Using the numpy.transpose() function

The transpose() function in the NumPy library is mainly used to reverse or permute the axes of an array. Now we will see how to use this function. You can use this function by running the following code:

b = np.transpose(a)
print("The modified array after transpose is:\n",b)

The output will be:

The modified array after transpose is:
 [[1 4]
  [2 5]
  [3 6]]

Using numpy.transpose() function with optional parameter.

The numpy.transpose() function can also take an optional parameter called axes. This parameter is used to change the orientation of the array according to a given list of axes. You can use this parameter by running the following code:

c = np.transpose(a, axes=(1,0))
print("The modified array after transpose with axes is:\n",c)

The output will be:

The modified array after transpose with axes is:
 [[1 4]
  [2 5]
  [3 6]]

Using 1-D arrays:

Note thattranspose() function of NumPy library does not affect 1-D arrays. Therefore, this function is used only for 2D arrays.

You can try this with the below code snippet:

d = np.array([1,2,3,4,5,6])
print("Array before transpose:\n",d)
e = np.transpose(d)
print("The modified array after transpose:\n",e)

The output will be:

Array before transpose:
 [1 2 3 4 5 6]
The modified array after transpose:
 [1 2 3 4 5 6]

Summary

In this lab, we have learned how to use the numpy.transpose() function in the NumPy library to permute or reverse the axes of an array. We learned how to use this function with and without optional parameters. We also learned that this function does not affect 1-D arrays. You can try out different variations and combinations of this function on arrays to make the most of it.

Summary

Congratulations! You have completed the Numpy Transpose Function lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like