This tutorial shows you how to transpose a matrix using the NumPy matrix transpose function in Python. In addition to being able to transpose a matrix, the transpose() function also lets you transpose other array-like objects, like a list of lists. We’ll show you some Python transpose examples on arrays and list of lists, but first let’s make sure you understand what the transpose of a matrix is and why you might need to use them.

What is the Transpose of a Matrix?

Transposing a matrix produces a new matrix where the rows and columns of the original matrix are switched. For instance if the original matrix has the shape [3,4], the shape of the transposed matrix will be [4,3].

Transposing matrices has real-world applications in all areas where linear algebra is used to solve complex problems. Some of these areas are:

  1. Image Processing
  2. Social Network Analysis
  3. Machine Learning and Data Science
  4. Signal Modulation and Demodulation
  5. Statistical Programming

Although transposing a matrix isn’t necessary for many modern computer algorithms, transposing a matrix becomes important in computer science when performing certain computations, like fast Fourier transforms. The way array data is stored in memory (by rows or by columns) can drastically change the speed at which these computations are performed. By swapping rows and columns, you can achieve much faster calculation performance in some instances.

NumPy Transpose Matrix Function

With Python’s NumPy library, finding the transpose of a matrix requires one line of code. To do so, you need to pass your matrix in the form of a list of lists, to the transpose() function of the NumPy library. If you have a NumPy array, you can directly call the transpose() method on the NumPy array to get the transpose of the array. Let’s look at a couple examples.

Note If you have not already installed Python’s NumPy library, you can do so by executing the following command at your terminal.

$ pip install numpy

This example uses NumPy to transpose a matrix created via a two dimensional NumPy array.

import numpy as np

M1 = np.array([[1, 10, 51, 26],
               [4,  5, 25, 62],
               [4,  5, 25, 62]])

print("Shape of the original matrix ", M1.shape)

In the script above, we create a NumPy array, M1, of 3 rows and 4 columns. Next, we printed the shape of the M1 array on console. Here is the output:

Shape of original matrix  (3, 4)

Let’s now take transpose of the M1 array and print the resultant matrix along with its shape:

M1T = M1.transpose()
print(f'Transposed Matrix:\n{M1T}')
print("Shape of the transposed matrix ", M1T.shape)

Here is the output:

Transposed Matrix:
[[ 1  4  4]
 [10  5  5]
 [51 25 25]
 [26 62 62]]
Shape of the transposed matrix  (4, 3)

From the output, you can see that the transposed matrix now contains 4 rows and 3 columns.

In addition to calling the transpose() function on NumPy array objects, you can pass a Python list of lists to the transpose() method. Look at the following example.

import numpy as np

M2 = [[5, 24, 76, 19],
      [6, 45, 19, 56],
      [9, 62, 25, 82]]
print("Shape of the original matrix ", np.array(M2).shape)

M2T = np.transpose(M2)
print(f'Transposed Matrix:\n{M2T}')
print("Shape of the transposed matrix ", np.array(M2T).shape)

When transposing a list of lists with NumPy, you must pass the list to the tranpose() function. This is a key difference between transposing a NumPy array, like we did above, and transposing a Python list of lists. Here is the output from our script:

Shape of the original matrix  (3, 4)
Transposed Matrix:
[[ 5  6  9]
 [24 45 62]
 [76 19 25]
 [19 56 82]]
Shape of the transposed matrix  (4, 3)

Conclusion

This article briefly explains matrix transpose along with its applications and uses. As a practical examples, the Python’s Numpy transpose matrix function is used to transpose matrices created via a NumPy array and a list of lists.

If you liked this NumPy tutorial, please subscribe using the form below. We’ll show you more Python tips and shortcuts to help you master this programming language.


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit