AI Hints
Statistics & Math for ML · Lesson 84 of 138

Identity and Inverse

Undoing a matrix.

⏱ 2 min read▶ Video at 1:39:06🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

The number one does nothing when you multiply by it. Matrices have their own version, called the identity. And just as multiplying by a half undoes multiplying by two, many matrices have an inverse that undoes them.

Identity and Inverse
From the video: Identity and Inverse
  • Multiplying by 1 does nothing
  • The identity matrix
  • The inverse undoes

np.eye and np.linalg.inv

NP dot eye makes the identity, with ones on the diagonal, and zeros everywhere else. Multiplying by it changes nothing. Lin alg dot inv finds the inverse. A times its inverse gives the identity. Run it.

inverse.pypython
import numpy as np

I = np.eye(2)
A = np.array([[2.0, 1.0], [1.0, 3.0]])

print(I)
print("A @ I equals A:", np.allclose(A @ I, A))
A_inv = np.linalg.inv(A)
print((A @ A_inv).round(10) + 0)
Output
[[1. 0.]
 [0. 1.]]
A @ I equals A: True
[[1. 0.]
 [0. 1.]]

Not every matrix has one

A matrix that squashes space flat, like squashing a square into a line, can't be undone, because information was lost. Those matrices are called singular, and they have no inverse. In data, this happens when one feature is an exact copy, or combination, of others. Like having price in dollars and price in cents.

keyvalue
Identityones on the diagonal
A @ A⁻¹= I
Singularsquashes space, no inverse
In dataduplicate features

Summary

The identity changes nothing, and the inverse undoes a matrix. Singular matrices have no inverse. Next, solving systems of equations.

Key takeaways
  • np.eye(n)
  • np.linalg.inv(A)
  • Singular: no inverse
Next up: Solving Linear Systems — np.linalg.solve