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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) numpy(("`NumPy`")) -.-> numpy/ArrayManipulationGroup(["`Array Manipulation`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") numpy/ArrayManipulationGroup -.-> numpy/transpose("`Transpose and Axis Swap`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/with_statement -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/transpose -.-> lab-86512{{"`Numpy Transpose Function`"}} python/lists -.-> lab-86512{{"`Numpy Transpose Function`"}} python/tuples -.-> lab-86512{{"`Numpy Transpose Function`"}} python/importing_modules -.-> lab-86512{{"`Numpy Transpose Function`"}} python/numerical_computing -.-> lab-86512{{"`Numpy Transpose Function`"}} python/build_in_functions -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/1d_array -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/multi_array -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/data_array -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/bool_idx -.-> lab-86512{{"`Numpy Transpose Function`"}} numpy/fancy_idx -.-> lab-86512{{"`Numpy Transpose Function`"}} end

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