. Advertisement .

..3..

. Advertisement .

..4..

This ITtutoria guide is for people who struggle to** inverse a matrix in Python**. Before we delve further into specific steps and examples, let’s find out exactly what a matrix’s inverse entails.

**What Is Matrix Inversion?**

First, let’s tackle this primary question: What is Matrix inversion? It is a matrix reciprocal, similar to how we fare in standard arithmetic for single numbers. This function aims to debunk equations to identify the value of unknown factors.

The basic rule here is that a matrix will send back identity matrices once multiplied with the initial matrix. It exists only when that matrix is non-singular (for instance, the determinant cannot be 0.).

When we use adjoint and determinant, it is easy to locate the square matrix’s inverse with our given formula below:

```
if det(X) != 0
X-1 = adj(X)/det(X)
else
"Inverse Matrix does not exist"
```

Here:

**X-1:** The X’s inversion

**How to Inverse A Matrix in Python?**

An effective method is to turn to the Numpy.linalg. inv(). It features different functions for array creation and manipulation in Python. This submodule employs other functions and algorithms for linear algebra.

Hence, it is possible to use this function for computing the given matrix’s inversion. Such operations might produce errors if the matrix’s inverse is not possible, which stems from its singular values.

Thus, we suggest you turn to that function in trial blocks. If that matrix has singular values, there will be errors, and the system will execute the codes in your except block.

**Example 1: **

```
import numpy as np
try:
ex_matrix = np.array([[22,28],[19,29]])
print(np.linalg.inv(ex_matrix))
except:
print("Singular Matrix, It is not possible to invert the Matrix.")
```

**Output:**

```
[[ 0.27358491 -0.26415094]
[-0.17924528 0.20754717]]
```

A matrix’s inversion is also called a “reciprocal matrix”. Let’s pick another example:

```
[1 3 2 1]
[0 1 -1 -1]
[0 0 1 3]
[0 0 0 1]
```

Let’s use the Numpy.linalg.inv() one more time to identify its inverse. Our NumPy codes will be as follows:

```
import numpy as np
ex_matrix1 = np.array([[1,3,2,1], [0,1,-1,-1], [0,0,1,3],[0,0,0,1]])
invert_matrix = np.linalg.inv(ex_matrix1)
print(invert_matrix)
```

After the script execution, we have this matrix:

```
[[ 1. -3. -5. 11.]
[ 0. 1. 1. -2.]
[ 0. 0. 1. -3.]
[ 0. 0. 0. 1.]]
```

That is the matrix’s inverse. And how to verify this result? An easy approach is to turn to the Numpy.allclose() operation. We can see that the inverse matrix is type 4 x 4.

Hence, let’s employ the numpy.eye() to make one identity matrix. Should the produced inverse matrix serve correct values, the line’s output will be True. Please use this command line to check:

`print(np.allclose(np.dot(invert_matrix, ex_matrix1), np.eye(4)))`

```
[[ 1. -3. -5. 11.]
[ 0. 1. 1. -2.]
[ 0. 0. 1. -3.]
[ 0. 0. 0. 1.]]
```

**Conclusion**

This article has shown you how to **inverse a matrix in Python**. Two examples are presented, making sure you can grasp the fundamental concept of this approach! It would be better to use updated Python for these processes, so please refer to this article for detailed upgrade instructions.

Good luck with your programming, and feel free to leave some comments or questions for the ITtutoria team if you still struggle to comprehend certain steps.

## Leave a comment