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 rows and columns is "". Maths writes matrices as capital letters, and is the number in row , column .
You've used matrices since Stage 0: the penguins' X is a 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:
In words: entry of the answer is row of , dotted with . 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 by and you get its first column; by , its second:
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:
In words: entry is row of dotted with column of . The shapes must fit in the middle: gives , 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:
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 : the x-axis arrow lands at , so everything doubles in width.
- Rotate 90° : goes to and to .
- Shear : the y-axis arrow tips over to , and squares lean into parallelograms.
Doing one transformation and then another is the matrix product. Order matters: "stretch, then rotate" is (the first one done sits next to the vector), and it's a different matrix from :
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 swaps rows and columns: . In NumPy:
A.T. A matrix equal to its own transpose is symmetric; the next lesson needs those. - The identity has 1s down the diagonal and 0s elsewhere. It leaves every vector where it is: . In NumPy:
np.eye(2). - The inverse undoes : . In NumPy:
np.linalg.inv(A).
Whether the inverse exists depends on the determinant, . For a 2×2 matrix:
In words: it's how much the matrix scales areas. The plane warper shows it as you edit. For here it's : every shape comes out 5 times the area.
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 . Both columns point along the same line, so the whole plane lands on that line. Its determinant is : 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:
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 , so one direction was lost; 2 for the healthy .
This turns up for real in Stage 3. Solving for linear regression's weights needs the inverse of , 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
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.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 2: Linear Algebra: matrices, the transpose, the identity and the inverse (2.1–2.3), and the determinant (2.11)
- 3Blue1Brown, Essence of Linear Algebra: the picture of a matrix as a transformation of the plane, which the plane warper follows
- NumPy: numpy.linalg.inv: the inverse, and LinAlgError for a singular matrix
- NumPy: numpy.linalg.det: the determinant
- NumPy: numpy.linalg.matrix_rank: the rank of a matrix