• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing How to Transpose Matrix in NumPy

You can use either numpy.matrix.transpose() or numpy.transpose() function to get the permute or reserve the dimension of the input matrix. The transpose of a matrix is obtained by moving the columns data to the rows and rows data to the column.  These transpose() functions are mainly used to transpose the 2-dimension arrays. This does not show any effect on the one-D array, When you try transposing a 1-D array, it returns an unmodified view of the original array.

Advertisements

In this article, I will explain the concept of the Python NumPy matrix.transpose() function and use this how to reverse the dimensions of the given matrix. If you want to transpose an array refer NumPy transpose() function.

1. Quick Examples of NumPy Transpose Matrix

If you are in a hurry, below are some quick examples of how to transpose the NumPy matrix.


# Quick examples of numpy transpose matrix

# Example 1: Use matrix.transpose() method
# Get the transpose of matrix
arr = np.matrix('[4, 8; 1, 12]')
arr2 = arr.transpose()

# Example 2:  Use numpy.transpose() function 
# Get the transpose of array
arr = np.array
arr2 = arr.transpose([[1, 2, 4, 3],[1, 3, 5, 6]])

# Example 3: Use numpy.mutiply() function 
arr2 = np.multiply(arr, arr1)

2. Syntax of NumPy matrix.transpose()

Following is the syntax of matrix.transpose() function


# Syntax of numpy.matrix.transpose() 
matrix.transpose(a, axes)

2.1 Parameters of NumPy matrix.transpose()

It takes two parameters

  • a – This is the input array that you want to transpose.
  • axes – This parameter specifies the new order of the axes. It is an optional parameter, and if not specified, the default behavior is to reverse the dimensions of the array. If specified, it should be a tuple or list of integers representing the desired order of axes. But if you want then remember to only pass (0, 1) or (1, 0). Like you have matrix of shapes (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 2 and 0 as 3.

2.2 Return Value

It returns a view of the array with axes transposed, the resultant array will have transposed array shape.

3. Usage of NumPy matrix.transpose()

This matrix.transpose() returns a NumPy array by interchanging (transposing) each row and the corresponding column. The new array is called the transpose of the given matrix. If you have a matrix of shape (X, Y) then the transpose of the matrix will have the shape(Y, X).

The transpose of the matrix is obtained using the transpose() method in NumPy. For instance, arr is the original matrix created using np.matrix('[4, 8; 1, 12]'). arr.transpose() is used to obtain the transpose of the matrix. The resulting transposed matrix is stored in the variable arr2. Finally, the transposed matrix is printed.


# Import numpy
import numpy as np

# Create matrix with numpy
arr = np.matrix('[4, 8; 1, 12]')
print("Original matrix:\n", arr)

# Get the transpose of matrix
arr2 = arr.transpose()
print("Transposed Matrix:\n",arr2)

Yields below output.

numpy transpose matrix

4. Use NumPy transpose() Function

Alternatively, you can reverse the dimensions of a given array using numpy.transpose(). Let’s create NumPy array using numpy.array() function and run the transpose function to transform.

Using the np.transpose() function with a NumPy array. This code creates a 2×4 array, prints the original array, then uses np.transpose() to obtain its transpose. Finally, it prints the transposed matrix. As mentioned earlier, the np.transpose() function reverses the dimensions of the array, effectively transposing the rows and columns.


# Create a numpy array
arr = np.array([[1, 2, 4, 3],[1, 3, 5, 6]])
print("Original array:\n", arr)

# Use numpy.transpose() function 
arr2 = arr.transpose()
print("Transposed Matrix:\n",arr2)

# Output :
# Original array:
#  [[1 2 4 3]
#  [1 3 5 6]]
# Transposed Matrix:
#  [[1 1]
#  [2 3]
#  [4 5]
#  [3 6]]

5. Use NumPy.multiply() to Matrix Multiplication

The np.multiply() function in NumPy performs element-wise multiplication, not matrix multiplication. If you want to perform element-wise multiplication of two arrays. However, if you want to perform matrix multiplication, you should use np.dot(), @ operator, or np.matmul().


import numpy as np
 
# Create a numpy two dimensional arrays
arr = np.array([[1, 2, 4, 3],[1, 3, 5, 6]])
arr1 = np.array([[2, 3, 6, 5],[4, 6, 2, 1]])
                 
# Use numpy.mutiply() function 
arr2 = np.multiply(arr, arr1)
print("Matrix Multiplication:\n",arr2)

Yields below output.


# Output:
Matrix Multiplication:
 [[ 2  6 24 15]
 [ 4 18 10  6]]

Frequently Asked Questions

What does it mean to transpose a matrix?

Transposing a matrix is an operation in linear algebra that involves flipping the matrix over its diagonal. This operation switches the row and column indices of the matrix. If you have a matrix A with dimensions m x n (m rows and n columns), the transpose of A, denoted as A^T, will have dimensions n x m (n rows and m columns).

How can I transpose a matrix in NumPy?

You can use the np.transpose() function or the .T attribute of a NumPy array to transpose a matrix.

How does matrix transposition relate to matrix multiplication?

Matrix transposition is closely related to matrix multiplication, particularly in the context of the dot product. When dealing with matrix multiplication, the rule is that for two matrices A and B to be multiplied (AB), the number of columns in A must be equal to the number of rows in B. If A is of shape (m x n) and B is of shape (n x p), then the resulting matrix C (AB) will be of shape (m x p).

Can I use the .T attribute to transpose a matrix?

You can use the .T attribute to transpose a matrix in NumPy. The .T attribute is a convenient shorthand for the np.transpose() function and provides a more concise way to obtain the transposed matrix.

Can I transpose a non-square matrix?

You can absolutely transpose a non-square matrix in NumPy. The transposition operation is defined for matrices of any shape, not just square matrices.

What happens if I transpose a 1D array?

When you transpose a 1D array in NumPy, the result will be the same as the original array. The transposition of a 1D array doesn’t change its shape.

Conclusion

In this article, I have explained how to transpose the matrix using the matrix.transpose() function with examples.

Happy Learning!!

References