How to calculate the inverse of a matrix in python using numpy ?

How to Calculate the Inverse of a Matrix in Python

When working with data analysis, machine learning, or scientific computing, you’ll often encounter situations where you need to invert a matrix — especially when solving systems of linear equations or performing transformations.

Python’s NumPy library makes this process both fast and simple.

What Is a Matrix Inverse?

For a square matrix ( A ), the inverse matrix $( A^{-1} )$ is defined such that:
\begin{equation}
[
A \times A^{-1} = I
]
\end{equation}

where ( I ) is the identity matrix (a matrix with 1’s on the diagonal and 0’s elsewhere).

However, not every matrix has an inverse.
A matrix is invertible (non-singular) if and only if its determinant is not zero.

Step-by-Step: Calculate a Matrix Inverse in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Step 1 — Import NumPy
import numpy as np

# Step 2 — Define a Square Matrix
A = np.array([[4, 7],
              [2, 6]])

# Step 3 — Compute the Inverse
A_inv = np.linalg.inv(A)
print("Inverse of A:")
print(A_inv)

Output:

1
2
3
Inverse of A:
[[ 0.6 -0.7]
 [-0.2  0.4]]

Verify the Result

You can check your result by multiplying the original matrix by its inverse.
The product should be (approximately) the identity matrix.

1
2
3
I = A @ A_inv
print("\nA * A_inv =")
print(np.round(I, 3))

Result:

1
2
3
A * A_inv =
[[1. 0.]
 [0. 1.]]

The small floating-point differences (like 0.999 instead of 1.0) are due to numerical precision.

Check if a Matrix is Invertible

Before computing the inverse, it’s a good practice to check the determinant:

1
2
3
4
5
det = np.linalg.det(A)
print("Determinant of A:", det)

if det == 0:
    print("Matrix is singular — no inverse exists.")

If the determinant is zero, the matrix cannot be inverted.

Handling Non-Invertible Matrices

If your matrix might be singular or non-square, you can still compute a pseudo-inverse using:

1
A_pinv = np.linalg.pinv(A)

The pseudo-inverse works even when the true inverse does not exist and is widely used in machine learning and least-squares regression.

Summary

Task Function Description
Inverse np.linalg.inv(A) Computes inverse of a square, non-singular matrix
Determinant np.linalg.det(A) Checks if the matrix is invertible
Pseudo-inverse np.linalg.pinv(A) Works for any matrix (square or not)

Another Example

\begin{equation}
A = \left( \begin{array}{ccc}
1 & 3 & 3 \\
1 & 4 & 3 \\
1 & 3 & 4
\end{array}\right)
\end{equation}

inverse matrix A_inv

\begin{equation}
A^{-1} = \left( \begin{array}{ccc}
7 & -3 & -3 \\
-1 & 1 & 0 \\
-1 & 0 & 1
\end{array}\right)
\end{equation}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import numpy as np
>>> A = np.array([[1, 3, 3],
...                [1, 4, 3],
...                [1, 3, 4]])
>>> A
array([[1, 3, 3],
       [1, 4, 3],
       [1, 3, 4]])

>>> A_inv = np.linalg.inv(A)
>>> A_inv
array([[ 7., -3., -3.],
       [-1.,  1.,  0.],
       [-1.,  0.,  1.]])

Checking the Result

1
2
3
4
>>> A_inv.dot(A)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

As expected, multiplying ( A^{-1} ) by ( A ) gives the identity matrix, confirming the inversion is correct.

References