# Transpose Matrix using Python

**PyShark**, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)

Want to share your content on python-bloggers? click here.

In this article we will discuss the steps and intuition for calculating the transpose of a matrix using Python.

**Table of contents**

**Books I recommend:**

- Python Crash Course
- Automate the Boring Stuff with Python
- Beyond the Basic Stuff with Python
- Serious Python

## Introduction

Transpose of a matrix is one of the fundamental topics in linear algebra, and also one of the simplest ones to understand.

To continue following this tutorial we will need the following Python library: numpy.

If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:

pip install numpy

## Transpose of a matrix explained

The transpose of a matrix, in linear algebra, is an operator which rotates the matrix around its main diagonal.

In simple words, the rows become columns, and columns become rows of a matrix.

**Square matrix example**

Given some matrix \(A\):

$$A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}$$

Its transpose is:

$$A^T = \begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}$$

The steps of transposition are quite simple. First we find the main diagonal (highlighted in red below), and then we essentially flip the matrix around it, and that’s how we get the transpose:

**Non-square matrix example**

Given some matrix \(A\) of \(3 \times 2\) dimension (can be any other dimension):

$$A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}$$

Its transpose is a matrix of \(2 \times 3\) dimension (in this case):

$$A^T = \begin{bmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \end{bmatrix}$$

The steps of transposition are the same as for the square matrix. First we find the main diagonal (highlighted in red below), and then we essentially flip the matrix around it, and that’s how we get the transpose:

We can generalize the matrix transpose steps as:

Given matrix \(A\) of \(m \times n \) dimension, it’s transpose is matrix \(A^T\) of \(n \times m\) dimension.

We are essentially always rotating the matrix around the main diagonal.

Note that the transpose of a diagonal matrix will be the same matrix.

## Transpose a matrix using Python

In order to create an identity matrix in Python we will use the numpy library. And the first step will be to import it:

import numpy as np

Numpy has a lot of useful functions, and for this operation we will use the transpose() function which will transpose a given matrix.

Recall that in Python matrices are constructed as arrays. So the next step will be to define the input matrix:

A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(A)

The input matrix should be:

[[1 2 3] [4 5 6] [7 8 9]]

The final step is to transpose the matrix using Python:

A_T = np.transpose(A) print(A_T)

And you should get:

[[1 4 7] [2 5 8] [3 6 9]]

which is exactly the same answer as in the previous section.

## Conclusion

In this article we discussed the steps and intuition for calculating a transpose of a matrix using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.

The post Transpose Matrix using Python appeared first on PyShark.

**leave a comment**for the author, please follow the link and comment on their blog:

**PyShark**.

Want to share your content on python-bloggers? click here.