The maths you need · lesson 4 of 12
Parts in this lesson
AA @ vmatrix times vector
BA.T, np.eye(2)the transpose and the identity
Cnp.linalg.inv(A), np.linalg.det(A)the inverse and the determinant
Dnp.linalg.matrix_rank(M)how many independent directions a matrix keeps

Matrices

Last updated

A matrix is a table of numbers, and you've already used one: the feature matrix X, a row per penguin. This lesson adds the second way to see it, as a machine that moves every point of the plane at once. Drag the numbers and watch the grid stretch, turn and shear. Neural networks are built from matrices doing exactly this, one layer after another.

By the end of this lesson you can

  • Read a matrix’s shape, rows and columns, and its transpose
  • Multiply a matrix by a vector, and see the columns as where the axes land
  • Describe a 2×2 matrix as a stretch, rotation, shear or flip of the plane
  • Use the identity, the inverse and the determinant, and spot a matrix with no inverse

Rows and columns

A matrix is a grid of numbers. Its shape is rows by columns: a matrix with n rows and d columns is "n×d". Maths writes matrices as capital letters, and Aij is the number in row i, column j.

A=(2113)A12=1,A22=3

You've used matrices since Stage 0: the penguins' X is a 342×4 matrix, one row per penguin and one column per measurement. Each row is a vector (lesson 1.2); so is each column.

Matrix times vector

To multiply a matrix by a vector, take the dot product of each row with the vector:

(Av)i=∑jAijvj

In words: entry i of the answer is row i of A, dotted with v. In NumPy: A @ v. The vector needs as many components as the matrix has columns, and the answer has as many as it has rows.

There's a second way to read it that's worth more. Multiply A by (1,0) and you get its first column; by (0,1), its second:

matrices.ipynb

So the columns of a matrix say where the two axis arrows land. Every other vector is some mix of the axis arrows, so it lands at the same mix of the columns. That's the whole transformation, read off the matrix at a glance.

The same rule, done for every column of a second matrix, is the matrix product:

(AB)ij=∑kAikBkj

In words: entry (i,j) is row i of A dotted with column j of B. The shapes must fit in the middle: (n×d)(d×k) gives n×k, as in lesson 0.6.

A matrix moves the plane

Seen this way, a 2×2 matrix is a machine that moves every point of the plane. Edit the four numbers, or try the presets, and watch the grid and the flag:

Plane warperdet = 1
column 1column 2

The matrix [[1, 0], [0, 1]] sends (1, 0) to (1, 0), its first column, and (0, 1) to (0, 1), its second. Its determinant is 1: areas are multiplied by 1.

  • Stretch (2001): the x-axis arrow lands at (2,0), so everything doubles in width.
  • Rotate 90° (0−110): (1,0) goes to (0,1) and (0,1) to (−1,0).
  • Shear (1101): the y-axis arrow tips over to (1,1), and squares lean into parallelograms.
matrices.ipynb

Doing one transformation and then another is the matrix product. Order matters: "stretch, then rotate" is RA (the first one done sits next to the vector), and it's a different matrix from AR:

compose.ipynb

A neural network layer (Stage 6) is a matrix like this followed by a bend, so a deep network is many transformations of space, one after another. The picture of matrices as moving the plane follows 3Blue1Brown's Essence of Linear Algebra, which is worth watching alongside this stage.

Transpose, identity and inverse

Three special matrices you'll need:

  • The transpose A⊤ swaps rows and columns: (A⊤)ij=Aji. In NumPy: A.T. A matrix equal to its own transpose is symmetric; the next lesson needs those.
  • The identity I has 1s down the diagonal and 0s elsewhere. It leaves every vector where it is: Iv=v. In NumPy: np.eye(2).
  • The inverse A−1 undoes A: A−1A=I. In NumPy: np.linalg.inv(A).

Whether the inverse exists depends on the determinant, det⁡A. For a 2×2 matrix:

det⁡(abcd)=ad−bc

In words: it's how much the matrix scales areas. The plane warper shows it as you edit. For A here it's 2×3−1×1=5: every shape comes out 5 times the area.

inverse.ipynb

The -0. in the last answer is a rounding speck, far below the sixth decimal place, so small it rounds to zero but keeps its sign. Treat it as 0.

Wrong way: a matrix that squashes the plane flat

Click "Squash flat" on the plane warper, or set the numbers to (1224). Both columns point along the same line, so the whole plane lands on that line. Its determinant is 1×4−2×2=0: every area becomes 0.

A flattened plane can't be un-flattened. Many points landed on the same spot, and no matrix can tell them apart again, so there is no inverse. Such a matrix is called singular. Ask for its inverse anyway:

singular.ipynb

On your own computer, np.linalg.inv(M) stops with LinAlgError: Singular matrix. The NumPy in this page is built differently and gives back an array of nan instead, with no error. That's worse, because nan flows quietly into everything that uses it. Either way, check before you invert:

  • np.linalg.det(M) near 0 means trouble.
  • np.linalg.matrix_rank(M) counts the independent directions the matrix keeps: 1 for M, so one direction was lost; 2 for the healthy A.

This turns up for real in Stage 3. Solving for linear regression's weights needs the inverse of X⊤X, and if two features are copies of each other (mass in grams and mass in kilograms, say), that matrix is singular.

Check what you learned

X has shape 100×4 and W has shape 4×3. What shape is XW?
Predict, then check with the plane warper: where does the matrix (3003) send the point (1,2)?
What is the determinant of (2412), and does it have an inverse?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning