Inverting Matrices With NumPy

Resumen

Reversing a matrix transformation is possible thanks to the inverse matrix, a core concept in linear algebra that powers algorithms like linear regression in machine learning. To understand how to undo what a matrix does to your data, you need two building blocks: the identity matrix and the inverse matrix, both easy to compute with NumPy.

What is the identity matrix and why does it matter?

Before undoing a transformation, you need a reference for the neutral state. That role belongs to the identity matrix, written as a capital I. It is a square matrix with ones on its main diagonal and zeros everywhere else, and it behaves like the number one of matrix algebra: any matrix A multiplied by I returns A unchanged [0:30].

What is the identity matrix? It is a square matrix with ones on the diagonal and zeros elsewhere. Multiplying any matrix by it gives back the same matrix, so it acts as the neutral element of multiplication.

How do you create an identity matrix in NumPy?

In Google Colab you can build one in a single line using np.eye, where the argument defines the size of the square matrix [1:20]. The method is called eye because it sounds like the letter I, avoiding the bad practice of naming a function with a single uppercase letter.

python import numpy as np

I = np.eye(3) print("3x3 identity matrix") print(I)

If you then define a matrix A and compute A @ I, the result is exactly A, which confirms the identity matrix is the one of matrix algebra [2:10].

How does the inverse matrix reverse a transformation?

The inverse matrix, written as A to the power of minus one, is the transformation that cancels the effect of A. Its defining property is simple: any matrix multiplied by its inverse equals the identity. That makes sense, because applying a transformation and then undoing it leaves you in the neutral state [2:50].

Not every matrix has an inverse, though. For a matrix to be invertible it must meet two conditions:

  • It must be square, meaning the same number of rows and columns.
  • Its determinant must be different from zero.

The determinant works as your quick test to know whether a transformation can be undone.

When is a matrix invertible? When it is square and its determinant is not zero. If either condition fails, no inverse exists and the transformation cannot be reversed.

How do you compute the determinant and the inverse in NumPy?

NumPy ships with the linalg module, which contains both det for the determinant and inv for the inverse [3:40]. Here is the full workflow with a 2x2 matrix:

python M = np.array([[3, 1], [2, 2]])

determinant = np.linalg.det(M) print(f"Determinant of M: {determinant}")

M_inv = np.linalg.inv(M) print(f"Inverse of M:\n{M_inv}")

identity = M @ M_inv print(np.round(identity, 2))

The determinant of M is 4, so the matrix passes the invertibility test. After computing M_inv, multiplying M by its inverse should return the identity matrix. Floating point arithmetic can leave tiny residuals, so wrapping the result in np.round(..., 2) cleans it up and shows a perfect identity matrix [5:30].

Why do the inverse and identity matrices matter in machine learning?

These two matrices are not just theory. They sit at the heart of practical algorithms you will use often.

  • The inverse matrix is the theoretical foundation of the normal equation in linear regression, a popular machine learning algorithm that gives you a direct formula for the optimal weights of a model.
  • The identity matrix is essential in regularization techniques such as Ridge regression, where a small portion of the identity is added to stabilize the system of equations and guarantee that the matrix remains invertible [6:40].

Even when production code relies on more numerically stable methods, understanding the inverse tells you where those solutions come from and why they work.

What is your practice exercise?

Put the concepts to work with this challenge:

  1. Create a 2x2 or 4x4 matrix of your choice.
  2. Use np.linalg.det to check that the determinant is different from zero.
  3. If it is invertible, compute its inverse with np.linalg.inv.
  4. Multiply the matrix by its inverse and confirm you get the identity matrix.

Share your result in the comments and tell us which matrix you picked. Next up, you will leave pure theory behind and build a linear regression model to predict housing prices, where these ideas finally click into place.