1.
Introduction
2.
NumPy (Numerical Python)
3.
Transpose of a Matrix
4.
How to Transpose a Matrix in Python?
4.1.
Code
4.2.
Output
5.
How to Transpose a Matrix Using Numpy.Transpose()
5.1.
Syntax
5.2.
Parameters
5.2.1.
a: array_like
5.2.2.
axes: tuple or list of ints ( optional )
5.3.
Returns
5.3.1.
p: ndarray
5.4.
Example 1
5.4.1.
Code
5.4.2.
Output
5.5.
Example 2
5.5.1.
Code
5.5.2.
Output
5.6.
6.
6.1.
What is a library in python?
6.2.
What is the use of the NumPy library in Python?
6.3.
Why is NumPy faster than lists?
6.4.
What are ndarrays in NumPy?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Numpy.Transpose() in Python

Basics of Machine Learning
Free guided path
9 chapters
29+ problems

## Introduction

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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.

## How to Transpose a Matrix Using Numpy.Transpose()

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)``

Now let's see the parameters of this function.

### Parameters

#### a: array_like

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.

### Returns

#### p: ndarray

It will return a ndarray. The resultant array is the given array with its axes permuted.

Now letâ€™s see some examples of this.

### Example 1

This example shows a simple transpose of an array using numpy.transpose() function.

#### Code

``````import numpy as np

input = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original array is: \n", input)

res = np.transpose(input);
print("\nResultant array is: \n", res)
``````

### Example 2

Letâ€™s see an example of transpose by providing a tuple as the second parameter.

#### Code

``````import numpy as np

input = np.array([[1, 2, 3,4], [5, 6, 7, 8], [9, 10, 11, 12]])

print("Original array is: \n", input)

res = np.transpose(input , (1,0));

print("\nResultant array is: \n", res)``````

### What is a library in python?

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 -

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questionsand the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow.

Happy Coding !!

Guided path
Free
Basics of Machine Learning
9 chapters
29+ Problems