Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 2 · INTERMEDIATEModule 2 · Matrices

Transpose, identity, inverse and determinant

Flip a matrix, do nothing, undo a matrix, and measure how much it stretches area. When that last number is zero, there is nothing to undo with.

10 min Prerequisites: Lesson 11 (multiplying matrices)

1. The idea

Four operations come up all the time. The transpose Aᵀ flips a matrix over its diagonal: rows become columns, so an m × n matrix becomes n × m. It is the usual fix for a shape mismatch, and it is why attention writes QKᵀ: every query dotted with every key.

The identity matrix I has 1s on the diagonal and 0s everywhere else; multiplying by it changes nothing, like multiplying a number by 1. The inverse A⁻¹ undoes A, so A⁻¹A = I. In the widget, apply A and then A⁻¹ and the grid comes back where it started.

The determinant says how much A scales area (volume, in more dimensions). det A = 2 doubles every area; a negative determinant also flips the plane over. det A = 0 means A squashes the plane onto a line, and nothing can un-squash it: A has no inverse. Code rarely computes an inverse itself. To find 𝐱 with A𝐱 = 𝐛, np.linalg.solve(A, b) is faster and more accurate than inv(A) @ b.

2. Try it: undo a matrix

Set a matrix, then press “Apply A, then A⁻¹”. Now try Squash flat, whose determinant is 0, and press it again.

The same grid transformation as in lesson 10, with a button that applies A and then its inverse. For a matrix with a non-zero determinant the grid returns to the plain grid; for one with determinant 0 the text explains that no inverse exists.

3. The math

Transpose
(A⊤)ij=Aji(AB)⊤=B⊤A⊤(A^\top)_{ij} = A_{ji} \qquad (AB)^\top = B^\top A^\top
Rows become columns. A transposed product reverses its order.
Identity
Ix=xAI=IA=AI\mathbf{x} = \mathbf{x} \qquad AI = IA = A
The matrix version of the number 1.
Inverse
A−1A=A A−1=IA^{-1}A = A\,A^{-1} = I
Only square matrices can have one, and only if det A ≠ 0.
Determinant and inverse of a 2 × 2
det⁡(abcd)=ad−bcA−1=1ad−bc(d−b−ca)\det\begin{pmatrix} a & b \\ c & d \end{pmatrix} = ad - bc \qquad A^{-1} = \frac{1}{ad-bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}
Dividing by ad − bc is exactly what fails when the determinant is 0.
No inverse
det⁡A=0  ⟺  A squashes space flat\det A = 0 \iff A \text{ squashes space flat}
Its columns are dependent: they span less than the whole space.

4. The code (python)

mm2_inverse.py
import numpy as np

A = np.array([[3.0, 1.0],
              [2.0, 1.0]])
print(A.T.tolist())                       # [[3.0, 2.0], [1.0, 1.0]]
print(round(np.linalg.det(A), 6))         # 1.0
A_inv = np.linalg.inv(A)
print(A_inv.round(6).tolist())            # [[1.0, -1.0], [-2.0, 3.0]]
print((A_inv @ A).round(6).tolist())      # [[1.0, 0.0], [0.0, 1.0]]

b = np.array([4.0, 3.0])
print(np.linalg.solve(A, b))              # [1. 1.]

S = np.array([[1.0, 2.0],
              [0.5, 1.0]])                # det = 1*1 - 2*0.5 = 0
print(round(abs(np.linalg.det(S)), 6))    # 0.0

5. You’ll see this in

6. Check yourself

Answer all 4 questions correctly to complete the chapter · 0 / 4 done
Q1/4 A is 3 × 5. What shape is Aᵀ?
Q2/4 det A = 0. What follows?
Q3/4 What is det [[2, 0], [0, 3]]?
Q4/4 (AB)ᵀ equals…

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.