Week 3 · Vectors, matrices and the best-fit line
Read the words of the video
- Imagine holding a pencil above a table, shining a flashlight straight down. The pencil's shadow on the table is its 'projection'. This shadow is the closest point on the table to your pencil's tip. The flashlight beam hits the table at a right angle, showing how we find the 'closest point equals perpendicular drop'. This is our main idea!
- In machine learning, we often have data points that don't exactly sit on a straight line. We can't find a perfect solution. Instead, we find the line that is 'least wrong'. This 'least wrong' line is like the shadow of our data, found by dropping a perpendicular onto the set of things a line can produce. It's about finding the closest fit.
- Let's try an example. Imagine a glowing point at (2,3) on a 2D graph and a glowing line passing through the origin at a 45-degree angle. We want to find the point's 'shadow' on this line. The shadow turns out to be at (2.5, 2.5). The difference between our original point and its shadow is exactly perpendicular to the line, just like our flashlight beam!
- For exams, remember that projection matrices are symmetric and orthogonally diagonalisable, but they are not always orthogonal or rank one. Always think of projection as finding the 'closest point' or the 'shadow' of your data onto a line or surface. You've mastered finding the 'least wrong' answer! Great job!
The one-minute story
You run a tiny shop and have recorded 3 days of sales: ads shown vs. items sold. The three dots almost sit on a line, but not quite. You want one straight line that is as close as possible to all three dots, so you can predict tomorrow.
No straight line passes through all three dots. So we cannot solve the problem exactly. What do we do? We find the line that is least wrong.
Here is the beautiful trick of this week: "least wrong" turns out to be the same thing as casting a shadow. The answer you want is the shadow of your real data on the set of things a line can produce. And shadows are found by dropping a perpendicular.
By the end of this week you can:
- read a matrix as a machine that mixes vectors, and multiply matrices by vectors;
- explain linear independence, span, basis, dimension and rank;
- name the four fundamental subspaces and state their dimensions;
- find the projection of a vector onto a line or a subspace;
- fit a line (or any linear model) by least squares using the normal equations.
Words you need
- Matrix - a rectangle of numbers. An matrix has rows and columns.
- Linear combination - "this much of vector A plus that much of vector B". Example: .
- Span - everything you can build from some vectors using linear combinations.
- Linearly independent - none of the vectors can be built from the others; each brings something new.
- Basis - a smallest set of vectors that spans the space, i.e. independent and spanning.
- Rank - how many independent columns a matrix has (the number of genuinely different directions it uses).
- Projection - the shadow of a vector on a line or flat surface.
- Residual - the leftover: the true value minus the fitted value.
Idea 1: A matrix mixes its columns
Picture a juice bar with three juices: orange, apple, mango. A recipe says how many glasses of each go into a drink: means 2 orange, 1 apple, 0 mango. The matrix has one column per juice (what is in each juice: sugar, vitamin C, ...). Multiplying the matrix by a recipe vector gives the total nutrition: you mix the columns in the amounts the recipe says.
For and :
Read as " times column 1, plus times column 2". Everything in this week follows from that single sentence.
The set of all possible (over all choices of ) is called the column space of . It is exactly the span of the columns.
Two things to remember about shape: is means it takes vectors with numbers and produces vectors with numbers. And is allowed only when the inside sizes match: .
Idea 2: Independence, span, basis, dimension, rank
- Vectors are linearly independent if the only way to mix them into the zero vector is to use zero of each. If one can be written from the others, they are dependent.
- A basis of a space is a set of independent vectors that spans it. Every basis of the same space has the same number of vectors, called its dimension.
- The rank of a matrix is the number of independent columns, which always equals the number of independent rows.
Example. and are dependent (the second is twice the first), so together they only span a line, not the whole plane. A matrix with these as columns has rank 1.
The four fundamental subspaces
For an matrix of rank :
| Space | What it is | Lives in | Dimension |
|---|---|---|---|
| Column space | all | ||
| Row space | span of the rows | ||
| Null space | all with | ||
| Left null space | all with |
Two perpendicularity facts: row space null space, and column space left null space. Also rank + nullity = number of columns: .
Question. is a matrix with rank 4. Which statements are true? (a) the columns are independent; (b) the null space contains only ; (c) the row space is all of ; (d) the left null space has dimension 2.
Solution. Rank 4 equals the number of columns, so all four columns are independent: (a) is true. Nullity , so only the zero vector solves : (b) true. The row space has dimension 4 and lives in , so it fills it: (c) true. Left null space has dimension : (d) true. All four are true.
The common slip is writing the left null space dimension as . It is (rows minus rank).
What a product does to row space and column space
Rows of are mixtures of rows of ; columns of are mixtures of columns of . So and , and . Papers love to swap and here.
Idea 3: Projection is a shadow
Hold a pencil above a table and shine a torch straight down. The pencil's shadow on the table is its projection. The shadow is the point on the table closest to the pencil's tip, and the torch beam hits the table at a right angle. Closest point = perpendicular drop. Remember that sentence; it is the heart of the week.
Projection onto a line
Projecting onto the line through the direction :
The matrix does the projecting for any : .
Why the formula looks like this: you want some multiple so that what is left over, , is perpendicular to . Perpendicular means dot product zero: , so .
Question. Project onto the line through . Then give the projection matrix onto the line and its trace.
Solution.
- and , so and the shadow is .
- The residual is . Check perpendicular: .
- The line has direction , so . Its trace is .
Notice : a projection onto a -dimensional subspace always has trace (its rank).
Four facts that every projection matrix obeys
- (projecting twice changes nothing: the shadow of a shadow is itself);
- (symmetric);
- for any already on the line (it is its own shadow);
- eigenvalues are only 0 and 1, and .
So a projection matrix is symmetric and orthogonally diagonalisable, but it is not an orthogonal matrix (it squashes things) and it is not always rank 1 (that depends on what you project onto). Those two sentences answer a real exam MSQ.
Projection onto a whole subspace
If the subspace is the column space of (columns independent), then
With one column this collapses to , the line formula. Same idea; you just solve for several amounts at once.
Idea 4: Least squares, or "closest I can get"
You want but is not in the column space of (too many equations, too few unknowns, noisy data), so no exact solution exists. The best compromise is the point in the column space closest to . That is the projection of onto the column space.
The shadow is closest exactly when the leftover is perpendicular to every column of :
These are the normal equations ("normal" meaning perpendicular). If the columns of are independent, is invertible and
Equivalently, minimises the sum of squared errors , the same squared loss you met in week 1. This is linear regression. For a line we use a column of ones for the intercept and a column of -values for the slope .
Question. Fit to the points .
Solution.
- Build (ones, then ) and .
- and .
- Solve . The determinant is , so .
- So and : the line is .
- Fitted values: . Residuals: .
Check: the residuals add to 0, and . The mistake vector is perpendicular to both columns, exactly as the theory says. The total squared error is .
Same problem, three languages
- Geometry: is the projection of onto the column space of .
- Algebra: solve .
- Calculus: set the gradient of to zero. You get the same equations. (We will use this in week 7.)
If has rows and independent columns with , then is and invertible. If the columns are dependent, is not invertible and the best is not unique. Later (MLT week 6) ridge regression fixes that.
Your turn
is a matrix with rank 3. Give the dimension of each of the four fundamental subspaces.
Need a hint?
Use , , .
Show the full solution
Column space: . Row space: . Null space: . Left null space: .
Project onto the direction . Give the shadow and the length of the shadow.
Need a hint?
.
Show the full solution
, , so and the shadow is itself. Its length is . Check the residual is perpendicular to : .
Fit a line through the points .
Need a hint?
Set , and form and .
Show the full solution
, . The determinant is , so . So and : . Predictions at : .
(a) symmetric (b) orthogonal (c) orthogonally diagonalisable (d) always rank 1
Need a hint?
Use the four facts listed in the lesson.
Show the full solution
(a) True, . (b) False: orthogonal matrices keep lengths, projections squash them. (c) True: symmetric matrices are orthogonally diagonalisable. (d) False: the rank equals the dimension of the subspace you project onto. Answer: (a) and (c).
and . What is the largest possible value of ?
Need a hint?
Recall Cauchy-Schwarz from week 1.
Show the full solution
, reached when they are parallel. Answer 15.
Exam radar
From the 53 MLF Quiz 1 papers: least squares method appears in 22, projection matrices in 13, row space in 11, basis in 11, rank in 9, matrix properties in 10, orthogonal matrix in 11.
Look for: rank/null-space MSQs (like Example 1), "find the projection matrix onto the line ...", "which statements about projection matrices are true", a small calculation for least squares, and the house-price least-squares story in the newest papers.
- Left null space has dimension , not .
- but . Do not swap.
- Projection matrices are not orthogonal matrices.
- In least squares, the residual is perpendicular to the columns of (the features), not to .
- exists only when the columns of are independent.
Quick quiz
A is 6 by 4 and has rank 4. What is the dimension of its left null space?
- 4
- 2
- 0
- 6
The projection matrix P onto the line through (1, 2) has trace...
- 5
- 2
- 1
- 0
In least squares, the residual vector y - A w is...
- parallel to y
- equal to zero
- perpendicular to every column of A
- perpendicular to w
Select all TRUE statements about a projection matrix P. (select all that apply)
- P squared equals P
- P is symmetric
- P is an orthogonal matrix
- P always has rank 1
Recap
- = a mixture of the columns of . Rank = number of independent columns.
- Four subspaces: column (, in ), row (, in ), null (), left null (). Row null; column left null.
- Closest point = perpendicular drop. Projection onto a line: ; onto a column space: . Always .
- Least squares: solve . The fitted values are the projection of ; the residual is perpendicular to the columns.
- rank + nullity ; left-null dimension
- Projection onto line: ; matrix
- Projection onto : ; ;
- Normal equations: ;
- MLT week 1 - PCA: finding the best view of your data: PCA's 'reconstruction' is a projection - exactly the shadow idea from MLF week 3.
- MLT week 5 - Regression: least squares and its Bayesian view: Least-squares regression is the same problem as MLF week 3's best-fit line.
- MLP week 3 - Linear regression and gradient descent in sklearn: The same least squares, now typed out in scikit-learn.
Practise with real exam questions
These come straight from past papers (with the real figures and the official answer key). Open 36 real questions on this week All Quiz 1 papers