How to inverse a matrix using NumPy?

Learn, how to inverse a matrix using NumPy in Python? By Pranit Sharma Last updated : October 10, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

Inverse of matrix

Mathematically, the inverse of matrix is another matrix, which on multiplication with the given matrix gives the multiplicative identity. For a matrix A, its inverse is A-1, and A · A-1 = A-1· A = I, where I is the identity matrix. The matrix whose determinant is non-zero and for which the inverse matrix can be calculated is called an invertible matrix.

Problem statement

Suppose, we are given with a numpy matrix and we need to create another matrix which is its inverse.

Inversing a matrix using NumPy

For this purpose, we will first create a numpy matrix and then we will use the I attribute which is used to generate an inverse of that matrix along which it is used.

Note: The I attribute only works with matrix.

Let us understand with the help of an example,

Python program to inverse a matrix using NumPy

# Import numpy
import numpy as np

# Import pandas
import pandas as pd

# Creating a numpy matrix
mat = np.matrix([[2,3],[4,5]])

# Display original matrix
print("Original matrix:\n",mat,"\n")

# Finding matrix inverse
res = mat.I

# Display result


The output of the above program is:

Example: How to inverse a matrix using NumPy?

Python NumPy Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.