Hello Ninjas! You must have familiar with the matrix. A matrix is a 2-D array of numbers, symbols, or expressions in the form of rows and columns. This article is about NumPy transpose in Python. We will be learning about the transpose of a matrix and how you can transpose a matrix in Python both manually and by using Numpy.transpose() operation.
Let’s start by learning about Numpy first.
NumPy (Numerical Python)
NumPy or Numerical Python is a library in Python that is used to operate on arrays. It was developed by Travis Oliphant in 2005. It is an open-source library and is available for all. The NumPy includes functions from the domains like linear algebra, transform, matrices, etc. It provides a high-performance array object which is much faster than the traditional Python lists. It is widely used for scientific computing in Python.
Before moving on to the NumPy transpose in python, first, let's see the transpose operation in detail.
Transpose of a Matrix
The transpose of an input matrix is a new matrix obtained after interchanging the rows and columns of the original matrix. It can be found by replacing the A[i][j] element with A[j][i]. It is represented by AT or A'. An example of transpose is shown below.
It can be found by replacing the elements of the columns from the respective rows. For example, the elements of the first column are written in the first row, the second column in the second row, and so on. The order of the matrix is also reversed, like the new order for the 3x2 matrix is 2x3.
Let’s see the implementation of the same.
How to Transpose a Matrix in Python?
First, we will discuss the manual method of finding the transpose of a matrix in Python. To find the matrix transpose, we need to interchange the columns and rows of the matrix. The element that is placed at the A[i][j] position is interchanged with the A[j][i] position. To implement this, we will use nested loops. Below is the code for the same.
Code
M = [[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]]
R = [[0 for x in range(len(M))] for y in range(len(M[0]))]
for i in range(len(M)):
for j in range(len(M[0])):
R[j][i] = M[i][j]
for r in R:
print(r)
Output
We can also do this by using NumPy transpose in Python. Let’s see it in detail.
We can find the matrix transpose in a much simpler way by using numpy.transpose() method. It is used to transpose two-dimensional arrays. It returns the modified array after taking the transpose and preserves the dimension of the array.
Before discussing this further, let’s see the syntax of this function.
Syntax
numpy.transpose(a, axes=None)
You can also try this code with Online Python Compiler
This parameter takes the input array. It is mandatory. The transpose operation is performed on this.
axes: tuple or list of ints ( optional )
It takes a list or tuple and should contain a permutation of numbers from 0 to N-1. This field is optional; if not specified, the transpose operation will, by default, reverse the dimensions.
A library in python is a collection of built-in modules that can be used frequently in the programs. It contains classes, utility methods, and modules your code requires for specific tasks.
What is the use of the NumPy library in Python?
NumPy is used mostly for scientific computing in Python. It has a variety of matrices, masks, and multidimensional arrays that are used to perform various maths operations. It provides a much faster array object and supports a large number of functions.
Why is NumPy faster than lists?
NumPy arrays are stored in continuous memory locations, unlike lists, so that they can be accessed and manipulated easily by the processes. This principle is known as the locality of reference. Along with this, they are optimized with the latest CPU architectures.
What are ndarrays in NumPy?
The ndarray is the array object present in the core of this package. The n-dimensional arrays are present in this, and many operations are also present, which are used to improve performance and perform advanced operations in an efficient manner.
Conclusion
This article was about Numpy transpose in Python. We have discussed the transpose of a matrix and how we can take the transpose in python both manually and by using numpy.transpose().
You can check out our other articles if you want to dive deep into other concepts related to the python language -