# How to Transpose Matrix in NumPy

• Post author:
• Post category:NumPy / Python

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.

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.

``````
# Below are the quick examples

# 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` – Input matrix
• `axes` – List of ints, corresponding to the dimensions. By default, the dimensions are reversed. If anyone wants to pass the parameter then you can but it’s not all required. 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 of matrix.transpose()

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

``````
import numpy as np
# Create matrix with numpy
arr = np.matrix('[4, 8; 1, 12]')
# Get the transpose of matrix
arr2 = arr.transpose()
print(arr2)

# Output
# [[ 4  1]
# [ 8 12]]
``````

## 4. Use NumPy transpose() Function

Alternatively, we 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.

``````
# Create a numpy array
arr = np.array([[1, 2, 4, 3],[1, 3, 5, 6]])
# Use numpy.transpose() function
arr2 = arr.transpose()
print(arr2)

# Output :
# [[1 1]
# [2 3]
# [4 5]
# [3 6]]
``````

## 5. Use NumPy.multiply() to Matrix Multiplication

If you want element-wise matrix multiplication, you can use numpy.multiply() function or numpy.dot() function.

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

Yields below output.

``````
[[ 2  6 24 15]
[ 4 18 10  6]]
``````

## 6. Conclusion

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

Happy Learning!!

## References 