In P1.B5 the projection matrix squashed the whole plane onto a line, even though it had four numbers and two columns. A spreadsheet can do something similar: if one column is the sum of two other columns, it tells a model nothing new. Both cases ask the same question: how many genuinely different directions are there? This lesson gives the question a precise form (span, independence, basis, rank) and shows what a matrix can and cannot do because of the answer.
What can you make? The span
P1.B2 defined a linear combination of vectors: scale each one, then add. With two vectors, , and the weights can be any numbers. The question “which points can I reach?” has a name. The span of some vectors is the set of all their linear combinations.
Here is what spans look like.
- One non-zero vector . The combinations are the multiples . Positive goes one way, negative goes the other way, and stays at the origin. The span is a line through the origin.
- The zero vector alone. Every multiple of it is itself, so the span is a single point: the origin.
- Two vectors in the plane that do not lie on one line. Take and . Can you reach ? You need , which is two equations:
Doubling the first and adding the second gives , so and then . Indeed . The same steps work for any target, so the span is the whole plane.
- Two vectors on one line, such as and . Both are multiples of , so every combination is too. The span is only that line.
- Two vectors in 3-D that do not lie on one line. Every combination starts at the origin and stays in the flat sheet through and , so the span is a plane through the origin. For and this is the plane , the same sheet you met in P1.B5. The point is on it, and is not, because .
The widget below draws all of these cases. It has two vectors (blue) and (pink) and a target .
Things to try:
- Press Independent and read the weights that reach : they are and , as computed above. Drag somewhere else and the weights change, but they always exist.
- Press Parallel. The tint disappears and only a line through the origin remains. Now is either on that line (reachable, in infinitely many ways) or off it (not reachable by any weights).
- Press Opposite. The vectors point opposite ways, yet the span is still only one line. Pointing the opposite way does not give a new direction.
- Press One zero and then Both zero, and read the status each time.
Do you need every vector? Independence
Look at and . The second is just twice the first, so it adds nothing: everything you can make with both, you can make with alone. Vectors like these are dependent. Vectors that each add something new are independent.
There are two ways to say this, and they are equivalent.
- Dependent: some vector in the set is a linear combination of the others. (Here .)
- Dependent: some combination of the vectors with weights not all zero equals . (Here .)
Independent means not dependent: the only way to get is to use all weights zero.
Why are the two statements the same? Suppose . Move everything to one side: , a combination with a weight, , that is not zero. Going back: if and, say, , divide by and move the rest across, and is a combination of the others. The second form is the one used for testing, because it treats all the vectors alike and needs no choice of “which one is the extra”.
Testing by eye. In the plane, two vectors are independent exactly when they do not lie on one line through the origin (the widget shows this as the tint). In 3-D, three vectors are independent exactly when they do not all lie in one plane through the origin.
Testing by computation. For two vectors in the plane there is a one-line test:
For and it gives : independent. For and it gives : dependent. (If then , which is why parallel vectors give zero. This number is the determinant of the matrix with these two columns, and P1.B8 explains it. The widget prints it under the plot.) For more vectors you put them side by side as the columns of a matrix and compute the rank, which is the subject of this lesson’s second half.
A few facts follow at once:
- A set containing the zero vector is always dependent. Take weight on and on the others: , a combination that is not all zeros. (A single zero vector on its own is dependent too.)
- A list with a repeated vector is dependent, for the same reason as and .
- A single non-zero vector is independent. forces .
- More than vectors in are always dependent. In the plane, take , and : then . Two directions are all a plane has, so a third vector cannot add a new one. (The reason is in a box below.)
- The reverse is also true: fewer than vectors can never span . One vector only reaches a line in the plane, two vectors only reach a plane in 3-D.
Three vectors in 3-D is the first case you cannot see at a glance. This widget draws the box that three vectors make.
Things to try:
- With a and b independent and , the box has volume 1 and the three vectors span all of 3-D (rank 3).
- Move the third slider of to , so . The box goes flat (volume 0), the rank drops to 2 and the span is the plane . Any of the form does this: try .
- Press a and b parallel. Now and alone make only a line, so three vectors can reach at most a plane. Set along , for instance , and even that plane collapses to a line (rank 1).
Basis and dimension
A basis of a space is a set of vectors that is
- independent (no waste), and
- spanning (it reaches every vector of the space).
It is the smallest set of arrows that still lets you reach everything. The one you already use is the standard basis: and in the plane (and in 3-D). The entries of a vector are its weights on them: .
Coordinates in a basis. Any independent pair that spans the plane works. Take and . Then
so the coordinates of the point in this basis are . It is the same point with a different recipe. Finding the coordinates means solving a small system, exactly as you did with and above: the “reach this point” line in the widget prints the recipe.
Why the recipe is unique. Suppose had two recipes, and . Subtract them: . Independence says the only way to get is with zero weights, so and . Spanning gives that a recipe exists, independence gives that there is only one. When the vectors are dependent, a recipe, if there is one, is not unique (the widget says “in infinitely many ways”); and if is off their line there is no recipe at all (the widget says it cannot be reached).
A basis is not unique, but its size is. The plane has many bases: and and , and so on. But every basis of the plane has exactly two vectors. The number of vectors in a basis is the dimension of the space. So has dimension , a line through the origin has dimension 1 and a plane through the origin has dimension 2. The span of independent vectors has dimension . (The set has dimension 0; its basis is the empty set.)
A basis does not have to be perpendicular, or made of unit vectors. and are a perfectly good basis of the plane. Perpendicular bases are nicer, because the coordinates then come straight from dot products (P1.B10 and P1.B11), but they are not required.
A basis can be a bad one. Take and . They are independent, so they are a basis, but they point almost the same way. To reach you need : huge weights that nearly cancel. Press Nearly parallel in the span explorer (its preset uses and , about 2.2° apart): the status line warns you, and the weights for the default are and , even though is a modest point. A tiny change in (or in the data) changes the weights a lot. This is the first sign of an unstable (badly conditioned) problem, and why “nearly dependent” matters as much as “dependent”.
Go deeper Why every basis of a space has the same size
Everything rests on one fact: vectors in are always dependent. To see it, look for weights with . Writing this entry by entry gives equations (one per entry), all with right-hand side , in unknowns . Solving by elimination, each equation can remove one unknown, and one unknown is left over. That one can be anything, so there is a solution that is not all zeros.
The same argument works inside any span of vectors (use the coordinates of the vectors in that span): more than vectors in it are dependent. Now suppose a space had a basis of size and another of size . The vectors of the second basis lie in the span of the vectors of the first, so they would be dependent, which a basis is not allowed to be. Hence , and by symmetry .
Subspaces
A subspace is a set of vectors that is a vector space on its own, sitting inside a bigger one. It has three requirements:
- it contains the zero vector ;
- it is closed under adding: if and are in the set, so is ;
- it is closed under scaling: if is in the set, so is for every number .
In words: you cannot get out of the set by adding or scaling.
- In the subspaces are exactly , the lines through the origin, and itself. In there are also the planes through the origin.
- Every span is a subspace. A sum of two combinations of is again a combination, and so is a multiple of one. This is why spans are always flat and always pass through the origin. The dimension of that subspace is the number of independent vectors among the ‘s.
Here are sets that are not subspaces, and the rule each one breaks:
| Set | Why it is not a subspace |
|---|---|
| The line | Misses the origin. It is also not closed under adding: and are on it, but their sum is not |
| The first quadrant (, ) | Contains and is closed under adding, but is outside |
| The two axes together () | Closed under scaling, but is on neither axis |
| The unit disk () | Contains , but is outside |
A shifted flat piece, such as a line that misses the origin, is not a subspace. Keep this in mind for later: a layer with produces a shifted copy of a subspace, because of the bias.
What is a vector space, formally?
P1.B1 and P1.B2 promised a precise definition. A vector space is any set whose elements can be added and scaled by real numbers, so that the result stays in and these rules hold for all in and all numbers :
| Rule | In symbols |
|---|---|
| Closed under adding | is in |
| Closed under scaling | is in |
| Adding in any order | |
| Grouping does not matter | |
| A zero vector exists | some in has |
| Every vector has an opposite | some has |
| Scaling twice | |
| Scaling by 1 does nothing | |
| Scaling spreads over a sum of vectors | |
| Scaling spreads over a sum of numbers |
That is eight rules, or ten if you list the two “closed” rules separately, which is why P1.B2 said “about eight to ten, depending on how a textbook counts”. Nothing here is surprising for lists of numbers: these are the rules you have used since P1.B2. What is new is that any set with adding and scaling that obeys them is a vector space, and everything in this lesson (span, independence, basis, dimension) works in it.
- Lists of numbers: , dimension .
- Polynomials of degree at most 2, : add and scale the coefficients. A basis is , so the dimension is 3. The polynomial has coordinates .
- matrices: add and scale entry by entry. A basis is the four matrices that have a single and zeros elsewhere, so the dimension is 4. (In general, matrices have dimension .)
- Functions , with and , also form a vector space. It has no finite basis: it is infinite-dimensional.
- Not a vector space: the positive numbers with ordinary adding and scaling. There is no zero vector among them, and times a positive number is not positive.
A subspace is then simply a subset that is a vector space with the same adding and scaling. Because the rules are inherited from the big space, you only need to check the three conditions above. In this course the numbers you scale by are real numbers; for machine learning the one space you need almost all the time is .
Columns, column space and rank
Now bring in matrices. In P1.B5 you saw that is a linear combination of the columns of , with the entries of as the weights. So the set of all possible outputs is exactly the span of the columns. That set is called the column space of (also the range or the image), written .
The rank of is the dimension of its column space: the number of independent columns, in other words the number of genuinely different directions the output uses.
Take . Column 2 is twice column 1, so the columns are dependent, the outputs all lie on the line and the rank is . Now take
Look at the columns: , so column 3 is a combination of columns 1 and 2. Columns 1 and 2 are not multiples of each other, so they are independent. The rank is 2, and the three columns together reach only a plane in 3-D, not all of 3-D.
Now look at the rows: . Row 3 is also a combination of the other two, and the first two rows are independent. The rows give rank 2 as well. That is not a coincidence:
This is surprising, because the columns and the rows of a matrix live in different spaces (columns in , rows in ), yet they have the same number of independent members.
Rank is at most . An matrix has columns, so it has at most independent columns. It has rows, so at most independent rows. Since the two counts are equal, the rank is at most the smaller of and . A matrix has rank at most , however many columns it has.
Some names:
- Full rank: the rank is as large as possible, .
- Full column rank: the rank is (all columns independent). Different inputs then give different outputs (see the null space below).
- Full row rank: the rank is . The columns then span all of , so every output is reachable.
- Rank deficient: the rank is below . The identity matrix of size has rank ; the matrix above has rank and is rank deficient.
What rank does to the picture. This is the P1.B5 picture, now with a number.
| Rank | What the map does | Example |
|---|---|---|
| 2 (a matrix) | Plane to plane: the whole plane is reached | rotation, scaling, shear |
| 1 | The plane is squashed onto a line | the projection from P1.B5 |
| 0 | Everything goes to the origin | the zero matrix |
The widget in P1.B5 said “squashed” for rank 1. A matrix with columns and has rank 2: it puts the plane into 3-D as a flat sheet (the plane ), and a matrix with the same columns, , has rank 2 too: it maps 3-D onto the whole plane. For a square matrix, rank (full rank) is the same as “the determinant is not zero” (P1.B8) and “the matrix can be undone” (P1.B9).
Go deeper Why the number of independent rows equals the number of independent columns
Let be with column rank . Pick independent columns and put them side by side in an matrix . Every column of is a combination of those columns, so we can write , where is an matrix that holds the weights (column of is the recipe for column of ).
By the row view of a product (P1.B6), each row of is a combination of the rows of . So all the rows of are combinations of just vectors, which means has at most independent rows: row rank column rank. This holds for every matrix. Apply it to the transpose (rows and columns swapped, P1.B10): the rows of are the columns of , so column rank of row rank of . Together, they are equal.
The null space and rank plus nullity
The projection squashes the whole plane onto a line, so something must be lost. What is lost is a direction. In P1.B5, was sent to . The set of inputs that a matrix sends to is the null space (or kernel): all with . It is a subspace (if and then and ). Its dimension is the nullity.
For : the vector gives , so the null space is the line of multiples of and the nullity is .
There is a clean accounting rule, the rank–nullity theorem. For a matrix with columns,
The directions of the input split into the ones that survive (they make the column space) and the ones that are crushed to . Check it on three matrices:
- has columns and rank . The vector is in its null space, so the nullity is , and .
- The projection has and rank . The vector is in its null space, the nullity is , and .
- has columns and rank . The vector is in its null space, the nullity is , and .
For the last matrix, gives . Three input directions, two survive, one is crushed. (The theorem is standard and proved in any linear algebra text; here it is stated and checked.)
Why this matters. If is a non-zero vector in the null space, then : two different inputs give the same output, so the map cannot be undone. That is the exact reason the projection in P1.B5 had no way back. When the nullity is (full column rank) different inputs always give different outputs.
Which outputs are reachable? A system has a solution exactly when is in the column space of , because the outputs are the column space. For , is reachable (try ) and so is every point of the line , but is not. When a solution exists and the null space is not just , there are infinitely many: add any null vector. P1.B9 solves such systems in general.
The rank of a product
P1.B6 left a promise: a product of thin matrices has small rank. Here is the statement:
Why. The columns of are times the columns of (P1.B6), so each is a combination of columns of . They stay inside the column space of , so the product cannot have more independent columns than has. In the same way the rows of are combinations of the rows of , so the product cannot have more independent rows than . Since the number of independent rows equals the number of independent columns, both limits apply.
The inequality can be strict. : both factors have rank and the product has rank .
The outer product has rank 1. A column times a row, , is the smallest example:
Every column is a multiple of and every row is a multiple of , so the rank is however large the matrix is. (If neither vector is zero; with a zero vector the product is the zero matrix.)
A thin product is low rank. If is and is , then has rank at most , even if and are huge. This delivers the promise from P1.B6 about LoRA. Fine-tuning a weight matrix with a LoRA update of inner size stores numbers instead of , and the update has rank at most . The LoRA paper’s bet is that a useful update is close to a low-rank one; that is its hypothesis and an empirical observation, not a theorem (P10.E3).
Two more places where the product rule shows up:
- A bottleneck. Two linear layers, with an inner size and no activation function between them, have rank at most : no matter how big the input and output are, the outputs lie in a -dimensional subspace (shifted by any bias). Real networks have nonlinearities, so this describes only the linear part, but it is the picture behind a narrow layer such as the middle of an autoencoder (P8.2).
- Attention scores. The score matrix is , but and have columns, so before the softmax it has rank at most (P7.3). That bound says something only when , and it stops holding after the mask and softmax, which are not linear: the attention weights themselves can have higher rank.
Go deeper The rank of a sum, and sums of outer products
The rank of a sum satisfies : every column of the sum is a column of plus a column of , so it lies in the span of the two column spaces together, whose dimension is at most the two ranks added. In P1.B6 a product was shown to be a sum of outer products: with the columns of and the rows of . Each outer product has rank at most , so the sum has rank at most . This is a second proof of the thin-product bound. P1.B13 returns to this: a matrix can be written as a sum of rank-1 pieces, and keeping only the biggest pieces gives a low-rank approximation.
Redundant features in a dataset
A data matrix with examples (rows) and features (columns) is just a matrix, so all of the above applies. If one feature is a combination of the others, the columns are dependent and the rank is below . A typical case: a total that is the sum of two other columns. Then there is a non-zero vector with (here for the columns weekday, weekend and total), a null direction, and a linear model cannot tell from : they give identical predictions, so the best weights are not unique. This is the redundancy (multicollinearity) problem of linear regression (P3.A2). The rank also gives a quick bound: with fewer examples than features () the rank is at most , so the columns are necessarily dependent.
Nearly dependent features are the more common, and often worse, case: the weights exist and are unique but swing wildly, just like the huge weights in the nearly parallel basis above.
The same idea gives PCA its starting point (P3.C3). If centred data lie exactly in a plane inside 3-D, the data matrix has rank 2, and two directions describe everything. Real data are noisy and usually have full rank, so what PCA reports is how much of the variation a few directions carry: an effective dimension. Whether a set of embeddings uses all of its directions is likewise something to measure, not assume.
Rank in NumPy
np.linalg.matrix_rank returns the rank. The first cell goes through the lesson: independence, coordinates, the nearly parallel basis, the rank of products, and the tolerance. Run it and read each line against the text.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Numerical rank. Computers use floating-point numbers, so a column that is exactly a combination of the others on paper is only almost one inside the machine. matrix_rank therefore does not look for exact zeros. It computes the singular values of the matrix and counts how many are larger than a tolerance. (Singular values are a list of non-negative numbers, one per direction, that say how much the matrix stretches that direction. A value of means the direction is flattened. P1.B13 explains them.) NumPy’s default tolerance is the largest singular value times times the machine epsilon, about for 64-bit floats. For the singular values are about , and a number around (the exact tiny value can differ between machines), far below the tolerance: rank 2.
Rank on a computer is therefore a decision: “which singular values are big enough to count?” For noisy real data the interesting question is not “what is the rank” but “how many directions matter”. The second cell shows both the exact and the practical side.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Read the output like this:
Xhas 3 columns but rank 2, so the weights and give the same predictions, because they differ by and .- For the rank is and the null space has one dimension: columns.
- With noise of size added to the dependent column, the columns are no longer exactly dependent. The default tolerance is tiny (about here), so NumPy reports rank 3. The smallest singular value is about , far below the others but above that tolerance. Telling
matrix_rankto treat anything below as zero gives rank 2. Which one is “right” depends on whether is noise to ignore or signal, and no formula decides that for you.
Common mistakes
Check your understanding
What is the span of the two vectors and in the plane?
Since , the second vector is a multiple of the first, so both lie on the line through the origin with direction , which is . Two vectors span the whole plane only if they do not lie on one line.
Which of these sets is a subspace of ?
The line passes through the origin and stays inside itself when you add or scale. The line misses the origin, the quadrant is not closed under scaling by , and the disk is not closed under scaling by 2 (or under adding).
What is the rank of ?
Row 2 is twice row 1, so it adds nothing. Rows 1 and 3 are not multiples of each other, so two rows are independent and the rank is 2. (Not 3, because the rows are dependent; not 1, because row 3 points somewhere new.)
A matrix has rank 3. What is the dimension of its null space?
Rank plus nullity equals the number of columns: , so the nullity is 3. (The number of rows, 4, does not enter. It only limits the rank, which is at most 4.)
is and is . What can you say about the rank of , which is ?
The rank of a product is at most the smaller of the two ranks, and each factor has rank at most 4 (it has 4 columns, or 4 rows). It can be lower than 4, for instance if the columns of are dependent, so "exactly 4" and "at least 4" are not guaranteed.
Exercises
- Are and independent? What is their span?
Show answer
, so they are independent. Two independent vectors in the plane span the whole plane, and they are a basis. - Find the coordinates of in the basis .
Show answer
Solve : and , so and . The coordinates are . Check: . - Show that , and are dependent, by finding weights.
Show answer
. The weights are not all zero, so the set is dependent. The rank of the matrix with these columns is 2. - Say whether each set is a subspace of or , and why: (a) ; (b) ; (c) ; (d) .
Show answer
(a) Yes: it is the line through the origin with direction . (b) No: it does not contain . (c) Yes: it is the horizontal plane through the origin (the -plane), closed under adding and scaling. (d) No: and are in it, but their sum is not. - Find the rank of .
Show answer
Row 2 is twice row 1. Row 3, , is not a multiple of row 1, so two rows are independent: the rank is 2. (By columns: column 2 is twice column 1, and column 3 is independent of column 1, again rank 2.) - What is the largest possible rank of a matrix, and of a matrix? When does a matrix have full rank, and what does its column space look like then?
Show answer
The ranks are at most and . A matrix has full rank when its rank is , so three of its columns are independent; its columns then span all of , and every output in is reachable. - A matrix has rank 3. How many dimensions does the null space have, and what kind of set is the column space inside ?
Show answer
Rank plus nullity is the number of columns, 7, so the nullity is . The column space has dimension 3: a flat 3-dimensional subspace of (it is not all of , which has dimension 4). - Compute the outer product and find its rank.
Show answer
It is . Row 1 is twice row 2, so the rank is 1. (Any outer product of two non-zero vectors has rank 1.) - A data matrix with 100 examples and 4 features has rank 3. What does this tell you about the features, and about a linear model on them?
Show answer
The 4 columns are dependent: at least one feature is a combination of the others (for example a total of two others). There is a non-zero vector with , so adding any multiple of to the weights does not change the predictions: the best weights are not unique. - Find the coordinates of in the basis and , and say what they warn you about.
Show answer
gives , and gives . The weights are huge for a modest point, because the basis vectors are almost parallel: small changes in the data produce large changes in the weights. - Code: predict the rank of
np.ones((4, 4))and ofnp.eye(4)[:, :3], then check withmatrix_rank.Show answer
The first is : every column is the same vector, so there is only one independent direction. The second is : the matrix has the first three columns of the identity, which are independent (and a matrix cannot have rank above 3).