Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsLinear Algebra

Span, independence, basis and rank

P1.B745 min●●○ IntermediateReviewed Nov 30, 2026

Why this matters for machine learning

Rank counts how many independent directions a matrix or a dataset really has, which decides what a layer can output, which features are redundant, and how much a low-rank model can store.

  • A feature that is a combination of other features (for example a total that equals the sum of two other columns) makes the weights of a linear regression ambiguous. (P3.A2: Linear regression, coming soon)
  • PCA looks for a few directions that carry most of a dataset's variation: the effective dimension of the data. (P3.C3: Principal Component Analysis, coming soon)
  • LoRA stores a weight update as a product of two thin matrices, so the update has rank at most rr. (P10.E3: Parameter-efficient fine-tuning, coming soon)
  • Two linear layers with a narrow middle of kk units can only produce a kk-dimensional set of outputs (rank at most kk), the idea behind the bottleneck of an autoencoder. (P8.2: Autoencoders, coming soon)
  • Whether a system Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} has a solution is a question about the column space of A\mathbf{A}. (P1.B9: Inverse and linear systems)

Before this: Vector operations, Matrices as data and as transformations, Matrix multiplication

After this lesson you can

  • Describe the span of one, two or three vectors (a point, a line, a plane or all of space) and decide whether a given point lies in it
  • Decide whether vectors are independent, by eye and by computation, and say what a dependent set wastes
  • Define a basis and the dimension, find coordinates in a basis, and tell a subspace from a set that is not one
  • Find the rank of a small matrix as the number of independent columns, use rank⁡≤min⁡(m,n)\operatorname{rank}\le\min(m,n), rank plus nullity, and the rank of a product, and read np.linalg.matrix_rank with its tolerance in mind

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, c1a+c2bc_1\mathbf{a}+c_2\mathbf{b}, and the weights c1,c2c_1,c_2 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 v\mathbf{v}. The combinations are the multiples c vc\,\mathbf{v}. Positive cc goes one way, negative cc goes the other way, and c=0c=0 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 a=(2,1)\mathbf{a}=(2,1) and b=(−1,2)\mathbf{b}=(-1,2). Can you reach (3,4)(3,4)? You need c1(2,1)+c2(−1,2)=(3,4)c_1(2,1)+c_2(-1,2)=(3,4), which is two equations:
2c1−c2=3c1+2c2=4\begin{aligned} 2c_1-c_2&=3\\ c_1+2c_2&=4 \end{aligned}

Doubling the first and adding the second gives 5c1=105c_1=10, so c1=2c_1=2 and then c2=1c_2=1. Indeed 2(2,1)+1(−1,2)=(3,4)2(2,1)+1(-1,2)=(3,4). The same steps work for any target, so the span is the whole plane.

  • Two vectors on one line, such as (2,1)(2,1) and (4,2)(4,2). Both are multiples of (2,1)(2,1), 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 a\mathbf{a} and b\mathbf{b}, so the span is a plane through the origin. For a=(1,0,1)\mathbf{a}=(1,0,1) and b=(0,1,1)\mathbf{b}=(0,1,1) this is the plane z=x+yz=x+y, the same sheet you met in P1.B5. The point (2,3,5)=2a+3b(2,3,5)=2\mathbf{a}+3\mathbf{b} is on it, and (1,1,1)(1,1,1) is not, because 1≠1+11\ne1+1.

The widget below draws all of these cases. It has two vectors a\mathbf{a} (blue) and b\mathbf{b} (pink) and a target t\mathbf{t}.

Everything a and b can make

−4−224−4−224abt
a = (2, 1), b = (−1, 2)
test: a₁×b₂ − a₂×b₁ = 2×2 − 1×(−1) = 5 (zero exactly when the pair is dependent)

Independent: neither vector is a multiple of the other, so a and b span the whole plane (dimension 2). They form a basis: every point has exactly one pair of weights (c₁, c₂).

t = (3, 4) is reached with c₁ = 2 and c₂ = 1: 2×(2, 1) + 1×(−1, 2) = (3, 4).

The blue and pink arrows are a and b; the dashed circle is the target t. Tinted plane with a slanted grid: every point is reachable, and the grid shows whole-number weights (c₁ along a, c₂ along b). Thick grey line: the span has collapsed to a line. Drag the tips, use the arrow keys, or use the sliders.

Span explorer. Drag the tips of a (blue) and b (pink) and the target t (dashed ring), or use the sliders or the arrow keys. When a and b are independent the whole plane is tinted, the slanted grid shows whole-number weights, and the faint path c₁a then c₂b ends at t. When they are dependent only a thick line (or only the origin) is left. The status line says which case it is and why, and the second line says how t is reached, or that it cannot be.

Things to try:

  1. Press Independent and read the weights that reach t=(3,4)\mathbf{t}=(3,4): they are c1=2c_1=2 and c2=1c_2=1, as computed above. Drag t\mathbf{t} somewhere else and the weights change, but they always exist.
  2. Press Parallel. The tint disappears and only a line through the origin remains. Now t\mathbf{t} is either on that line (reachable, in infinitely many ways) or off it (not reachable by any weights).
  3. 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.
  4. Press One zero and then Both zero, and read the status each time.

Do you need every vector? Independence

Look at a=(1,2)\mathbf{a}=(1,2) and b=(2,4)\mathbf{b}=(2,4). The second is just twice the first, so it adds nothing: everything you can make with both, you can make with a\mathbf{a} 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.

  1. Dependent: some vector in the set is a linear combination of the others. (Here b=2a\mathbf{b}=2\mathbf{a}.)
  2. Dependent: some combination of the vectors with weights not all zero equals 0\mathbf{0}. (Here 2a−1⋅b=(2,4)−(2,4)=02\mathbf{a}-1\cdot\mathbf{b}=(2,4)-(2,4)=\mathbf{0}.)

Independent means not dependent: the only way to get 0\mathbf{0} is to use all weights zero.

Why are the two statements the same? Suppose v3=2v1+v2\mathbf{v}_3=2\mathbf{v}_1+\mathbf{v}_2. Move everything to one side: 2v1+v2−1⋅v3=02\mathbf{v}_1+\mathbf{v}_2-1\cdot\mathbf{v}_3=\mathbf{0}, a combination with a weight, −1-1, that is not zero. Going back: if c1v1+⋯+ckvk=0c_1\mathbf{v}_1+\dots+c_k\mathbf{v}_k=\mathbf{0} and, say, ck≠0c_k\ne0, divide by ckc_k and move the rest across, and vk\mathbf{v}_k 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:

a1b2−a2b1≠0 ⟺ independent\begin{aligned} a_1b_2-a_2b_1&\ne0\ \Longleftrightarrow\ \text{independent} \end{aligned}

For a=(2,1)\mathbf{a}=(2,1) and b=(−1,2)\mathbf{b}=(-1,2) it gives 2⋅2−1⋅(−1)=5≠02\cdot2-1\cdot(-1)=5\ne0: independent. For (1,2)(1,2) and (2,4)(2,4) it gives 1⋅4−2⋅2=01\cdot4-2\cdot2=0: dependent. (If b=ka\mathbf{b}=k\mathbf{a} then a1(ka2)−a2(ka1)=0a_1(ka_2)-a_2(ka_1)=0, 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 11 on 0\mathbf{0} and 00 on the others: 1⋅0+0⋅v=01\cdot\mathbf{0}+0\cdot\mathbf{v}=\mathbf{0}, 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 (1,2)(1,2) and (2,4)(2,4).
  • A single non-zero vector is independent. c v=0c\,\mathbf{v}=\mathbf{0} forces c=0c=0.
  • More than nn vectors in Rn\mathbb{R}^n are always dependent. In the plane, take (1,0)(1,0), (0,1)(0,1) and (5,7)(5,7): then 5(1,0)+7(0,1)−1⋅(5,7)=05(1,0)+7(0,1)-1\cdot(5,7)=\mathbf{0}. 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 nn vectors can never span Rn\mathbb{R}^n. 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.

Three vectors in 3-D: a line, a plane, or all of space

xyzabc
a = (1, 0, 1), b = (0, 1, 1), c = (1, 1, 1)
rank (the number of independent vectors among them) = 3
volume of the box = |a · (b × c)| = 1 (zero exactly when the three are dependent)

Independent: c sticks out of the plane of a and b, so the three vectors span all of 3-D space (dimension 3, rank 3). The box has a volume above 0.

Thin lines: the box with edges a, b and c. When the vectors are dependent the box is flat. The tinted patch is part of the plane they span, and the thick line is the line they span. The picture is a projection onto a flat screen, so turn the view to read depth.

Three vectors in 3-D. a = (1, 0, 1) is blue, b is pink and c (set by the sliders) is black. Switch b between (0, 1, 1) and a parallel vector. The thin lines are the box with edges a, b and c. The rank and the volume of the box are printed below. When the three vectors are dependent the box is flat, and the span is a plane (tinted) or a line (thick grey).

Things to try:

  1. With a and b independent and c=(1,1,1)\mathbf{c}=(1,1,1), the box has volume 1 and the three vectors span all of 3-D (rank 3).
  2. Move the third slider of c\mathbf{c} to 22, so c=(1,1,2)=a+b\mathbf{c}=(1,1,2)=\mathbf{a}+\mathbf{b}. The box goes flat (volume 0), the rank drops to 2 and the span is the plane z=x+yz=x+y. Any c\mathbf{c} of the form c1a+c2bc_1\mathbf{a}+c_2\mathbf{b} does this: try c=(2,1,3)\mathbf{c}=(2,1,3).
  3. Press a and b parallel. Now a\mathbf{a} and b\mathbf{b} alone make only a line, so three vectors can reach at most a plane. Set c\mathbf{c} along a\mathbf{a}, for instance (1,0,1)(1,0,1), 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

  1. independent (no waste), and
  2. 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: e1=(1,0)\mathbf{e}_1=(1,0) and e2=(0,1)\mathbf{e}_2=(0,1) in the plane (and e1,e2,e3\mathbf{e}_1,\mathbf{e}_2,\mathbf{e}_3 in 3-D). The entries of a vector are its weights on them: (3,1)=3e1+1e2(3,1)=3\mathbf{e}_1+1\mathbf{e}_2.

Coordinates in a basis. Any independent pair that spans the plane works. Take (1,1)(1,1) and (1,−1)(1,-1). Then

(3,1)=2 (1,1)+1 (1,−1)=(2,2)+(1,−1)\begin{aligned} (3,1)&=2\,(1,1)+1\,(1,-1)\\ &=(2,2)+(1,-1) \end{aligned}

so the coordinates of the point (3,1)(3,1) in this basis are (2,1)(2,1). It is the same point with a different recipe. Finding the coordinates means solving a small system, exactly as you did with a=(2,1)\mathbf{a}=(2,1) and b=(−1,2)\mathbf{b}=(-1,2) above: the “reach this point” line in the widget prints the recipe.

Why the recipe is unique. Suppose t\mathbf{t} had two recipes, c1a+c2bc_1\mathbf{a}+c_2\mathbf{b} and d1a+d2bd_1\mathbf{a}+d_2\mathbf{b}. Subtract them: (c1−d1)a+(c2−d2)b=0(c_1-d_1)\mathbf{a}+(c_2-d_2)\mathbf{b}=\mathbf{0}. Independence says the only way to get 0\mathbf{0} is with zero weights, so c1=d1c_1=d_1 and c2=d2c_2=d_2. 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 t\mathbf{t} 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: (1,0),(0,1)(1,0),(0,1) and (1,1),(1,−1)(1,1),(1,-1) and (2,1),(−1,2)(2,1),(-1,2), 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 Rn\mathbb{R}^n has dimension nn, a line through the origin has dimension 1 and a plane through the origin has dimension 2. The span of kk independent vectors has dimension kk. (The set {0}\{\mathbf{0}\} has dimension 0; its basis is the empty set.)

A basis does not have to be perpendicular, or made of unit vectors. (1,0)(1,0) and (1,1)(1,1) 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 (1,0)(1,0) and (1,0.001)(1,0.001). They are independent, so they are a basis, but they point almost the same way. To reach (3,1)(3,1) you need (3,1)=−997 (1,0)+1000 (1,0.001)(3,1)=-997\,(1,0)+1000\,(1,0.001): huge weights that nearly cancel. Press Nearly parallel in the span explorer (its preset uses a=(2,1)\mathbf{a}=(2,1) and b=(2,1.1)\mathbf{b}=(2,1.1), about 2.2° apart): the status line warns you, and the weights for the default t=(3,4)\mathbf{t}=(3,4) are −23.5-23.5 and 2525, even though t\mathbf{t} is a modest point. A tiny change in t\mathbf{t} (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: n+1n+1 vectors in Rn\mathbb{R}^n are always dependent. To see it, look for weights with c1v1+⋯+cn+1vn+1=0c_1\mathbf{v}_1+\dots+c_{n+1}\mathbf{v}_{n+1}=\mathbf{0}. Writing this entry by entry gives nn equations (one per entry), all with right-hand side 00, in n+1n+1 unknowns c1,…,cn+1c_1,\dots,c_{n+1}. 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 kk vectors (use the coordinates of the vectors in that span): more than kk vectors in it are dependent. Now suppose a space had a basis of size kk and another of size m>km>k. The mm vectors of the second basis lie in the span of the kk vectors of the first, so they would be dependent, which a basis is not allowed to be. Hence m≤km\le k, and by symmetry k≤mk\le m.

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:

  1. it contains the zero vector 0\mathbf{0};
  2. it is closed under adding: if u\mathbf{u} and v\mathbf{v} are in the set, so is u+v\mathbf{u}+\mathbf{v};
  3. it is closed under scaling: if v\mathbf{v} is in the set, so is c vc\,\mathbf{v} for every number cc.

In words: you cannot get out of the set by adding or scaling.

  • In R2\mathbb{R}^2 the subspaces are exactly {0}\{\mathbf{0}\}, the lines through the origin, and R2\mathbb{R}^2 itself. In R3\mathbb{R}^3 there are also the planes through the origin.
  • Every span is a subspace. A sum of two combinations of v1,…,vk\mathbf{v}_1,\dots,\mathbf{v}_k 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 v\mathbf{v}‘s.

Here are sets that are not subspaces, and the rule each one breaks:

SetWhy it is not a subspace
The line y=x+1y=x+1Misses the origin. It is also not closed under adding: (0,1)(0,1) and (1,2)(1,2) are on it, but their sum (1,3)(1,3) is not
The first quadrant (x≥0x\ge0, y≥0y\ge0)Contains 0\mathbf{0} and is closed under adding, but −1⋅(1,1)=(−1,−1)-1\cdot(1,1)=(-1,-1) is outside
The two axes together (xy=0xy=0)Closed under scaling, but (1,0)+(0,1)=(1,1)(1,0)+(0,1)=(1,1) is on neither axis
The unit disk (x2+y2≤1x^2+y^2\le1)Contains 0\mathbf{0}, but 2⋅(1,0)=(2,0)2\cdot(1,0)=(2,0) 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 Wx+b\mathbf{W}\mathbf{x}+\mathbf{b} with b≠0\mathbf{b}\ne\mathbf{0} 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 VV whose elements can be added and scaled by real numbers, so that the result stays in VV and these rules hold for all u,v,w\mathbf{u},\mathbf{v},\mathbf{w} in VV and all numbers c,dc,d:

RuleIn symbols
Closed under addingu+v\mathbf{u}+\mathbf{v} is in VV
Closed under scalingc vc\,\mathbf{v} is in VV
Adding in any orderu+v=v+u\mathbf{u}+\mathbf{v}=\mathbf{v}+\mathbf{u}
Grouping does not matter(u+v)+w=u+(v+w)(\mathbf{u}+\mathbf{v})+\mathbf{w}=\mathbf{u}+(\mathbf{v}+\mathbf{w})
A zero vector existssome 0\mathbf{0} in VV has v+0=v\mathbf{v}+\mathbf{0}=\mathbf{v}
Every vector has an oppositesome −v-\mathbf{v} has v+(−v)=0\mathbf{v}+(-\mathbf{v})=\mathbf{0}
Scaling twicec(d v)=(cd) vc(d\,\mathbf{v})=(cd)\,\mathbf{v}
Scaling by 1 does nothing1 v=v1\,\mathbf{v}=\mathbf{v}
Scaling spreads over a sum of vectorsc(u+v)=cu+cvc(\mathbf{u}+\mathbf{v})=c\mathbf{u}+c\mathbf{v}
Scaling spreads over a sum of numbers(c+d)v=cv+dv(c+d)\mathbf{v}=c\mathbf{v}+d\mathbf{v}

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: Rn\mathbb{R}^n, dimension nn.
  • Polynomials of degree at most 2, a+bx+cx2a+bx+cx^2: add and scale the coefficients. A basis is 1,x,x21,x,x^2, so the dimension is 3. The polynomial 2+3x2+3x has coordinates (2,3,0)(2,3,0).
  • 2×22\times2 matrices: add and scale entry by entry. A basis is the four matrices that have a single 11 and zeros elsewhere, so the dimension is 4. (In general, m×nm\times n matrices have dimension mnmn.)
  • Functions f:R→Rf:\mathbb{R}\to\mathbb{R}, with (f+g)(x)=f(x)+g(x)(f+g)(x)=f(x)+g(x) and (cf)(x)=c f(x)(cf)(x)=c\,f(x), 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 −1-1 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 Rn\mathbb{R}^n.

Columns, column space and rank

Now bring in matrices. In P1.B5 you saw that Ax\mathbf{A}\mathbf{x} is a linear combination of the columns of A\mathbf{A}, with the entries of x\mathbf{x} 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 A\mathbf{A} (also the range or the image), written col⁡(A)\operatorname{col}(\mathbf{A}).

The rank of A\mathbf{A} 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 A=(1224)\mathbf{A}=\begin{pmatrix}1&2\\2&4\end{pmatrix}. Column 2 is twice column 1, so the columns are dependent, the outputs all lie on the line y=2xy=2x and the rank is 11. Now take

M=(123456789)\mathbf{M}=\begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix}

Look at the columns: (3,6,9)=2 (2,5,8)−(1,4,7)(3,6,9)=2\,(2,5,8)-(1,4,7), 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: (7,8,9)=2 (4,5,6)−(1,2,3)(7,8,9)=2\,(4,5,6)-(1,2,3). 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 2×32\times3 matrix live in different spaces (columns in R2\mathbb{R}^2, rows in R3\mathbb{R}^3), yet they have the same number of independent members.

Rank is at most min⁡(m,n)\min(m,n). An m×nm\times n matrix has nn columns, so it has at most nn independent columns. It has mm rows, so at most mm independent rows. Since the two counts are equal, the rank is at most the smaller of mm and nn. A 2×1002\times100 matrix has rank at most 22, however many columns it has.

Some names:

  • Full rank: the rank is as large as possible, min⁡(m,n)\min(m,n).
  • Full column rank: the rank is nn (all columns independent). Different inputs then give different outputs (see the null space below).
  • Full row rank: the rank is mm. The columns then span all of Rm\mathbb{R}^m, so every output is reachable.
  • Rank deficient: the rank is below min⁡(m,n)\min(m,n). The identity matrix of size nn has rank nn; the 3×33\times3 matrix M\mathbf{M} above has rank 22 and is rank deficient.

What rank does to the picture. This is the P1.B5 picture, now with a number.

RankWhat the map doesExample
2 (a 2×22\times2 matrix)Plane to plane: the whole plane is reachedrotation, scaling, shear
1The plane is squashed onto a linethe projection (0.50.50.50.5)\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix} from P1.B5
0Everything goes to the originthe zero matrix

The widget in P1.B5 said “squashed” for rank 1. A 3×23\times2 matrix with columns (1,0,1)(1,0,1) and (0,1,1)(0,1,1) has rank 2: it puts the plane into 3-D as a flat sheet (the plane z=x+yz=x+y), and a 2×32\times3 matrix with the same columns, (101011)\begin{pmatrix}1&0&1\\0&1&1\end{pmatrix}, has rank 2 too: it maps 3-D onto the whole plane. For a square matrix, rank nn (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 A\mathbf{A} be m×nm\times n with column rank rr. Pick rr independent columns and put them side by side in an m×rm\times r matrix C\mathbf{C}. Every column of A\mathbf{A} is a combination of those rr columns, so we can write A=CR\mathbf{A}=\mathbf{C}\mathbf{R}, where R\mathbf{R} is an r×nr\times n matrix that holds the weights (column jj of R\mathbf{R} is the recipe for column jj of A\mathbf{A}).

By the row view of a product (P1.B6), each row of CR\mathbf{C}\mathbf{R} is a combination of the rr rows of R\mathbf{R}. So all the rows of A\mathbf{A} are combinations of just rr vectors, which means A\mathbf{A} has at most rr independent rows: row rank ≤\le column rank. This holds for every matrix. Apply it to the transpose A⊤\mathbf{A}^\top (rows and columns swapped, P1.B10): the rows of A⊤\mathbf{A}^\top are the columns of A\mathbf{A}, so column rank of A\mathbf{A} ≤\le row rank of A\mathbf{A}. 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, (1,−1)(1,-1) was sent to (0,0)(0,0). The set of inputs that a matrix sends to 0\mathbf{0} is the null space (or kernel): all x\mathbf{x} with Ax=0\mathbf{A}\mathbf{x}=\mathbf{0}. It is a subspace (if Ax=0\mathbf{A}\mathbf{x}=\mathbf{0} and Ay=0\mathbf{A}\mathbf{y}=\mathbf{0} then A(x+y)=0\mathbf{A}(\mathbf{x}+\mathbf{y})=\mathbf{0} and A(cx)=0\mathbf{A}(c\mathbf{x})=\mathbf{0}). Its dimension is the nullity.

For A=(1224)\mathbf{A}=\begin{pmatrix}1&2\\2&4\end{pmatrix}: the vector (2,−1)(2,-1) gives (1⋅2+2⋅(−1), 2⋅2+4⋅(−1))=(0,0)(1\cdot2+2\cdot(-1),\ 2\cdot2+4\cdot(-1))=(0,0), so the null space is the line of multiples of (2,−1)(2,-1) and the nullity is 11.

There is a clean accounting rule, the rank–nullity theorem. For a matrix with nn columns,

n=rank⁡+nullity⁡\begin{aligned} n&=\operatorname{rank}+\operatorname{nullity} \end{aligned}

The nn directions of the input split into the ones that survive (they make the column space) and the ones that are crushed to 0\mathbf{0}. Check it on three matrices:

  • (1224)\begin{pmatrix}1&2\\2&4\end{pmatrix} has n=2n=2 columns and rank 11. The vector (2,−1)(2,-1) is in its null space, so the nullity is 11, and 1+1=21+1=2.
  • The projection (0.50.50.50.5)\begin{pmatrix}0.5&0.5\\0.5&0.5\end{pmatrix} has n=2n=2 and rank 11. The vector (1,−1)(1,-1) is in its null space, the nullity is 11, and 1+1=21+1=2.
  • (101011)\begin{pmatrix}1&0&1\\0&1&1\end{pmatrix} has n=3n=3 columns and rank 22. The vector (1,1,−1)(1,1,-1) is in its null space, the nullity is 11, and 2+1=32+1=3.

For the last matrix, (1,1,−1)(1,1,-1) gives (1+0−1, 0+1−1)=(0,0)(1+0-1,\ 0+1-1)=(0,0). 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 n\mathbf{n} is a non-zero vector in the null space, then A(x+n)=Ax+0=Ax\mathbf{A}(\mathbf{x}+\mathbf{n})=\mathbf{A}\mathbf{x}+\mathbf{0}=\mathbf{A}\mathbf{x}: 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 00 (full column rank) different inputs always give different outputs.

Which outputs are reachable? A system Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} has a solution exactly when b\mathbf{b} is in the column space of A\mathbf{A}, because the outputs are the column space. For (1224)\begin{pmatrix}1&2\\2&4\end{pmatrix}, b=(3,6)\mathbf{b}=(3,6) is reachable (try x=(1,1)\mathbf{x}=(1,1)) and so is every point of the line y=2xy=2x, but b=(1,0)\mathbf{b}=(1,0) is not. When a solution exists and the null space is not just 0\mathbf{0}, 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:

rank⁡(AB)≤min⁡(rank⁡A, rank⁡B)\begin{aligned} \operatorname{rank}(\mathbf{A}\mathbf{B})&\le\min\big(\operatorname{rank}\mathbf{A},\ \operatorname{rank}\mathbf{B}\big) \end{aligned}

Why. The columns of AB\mathbf{A}\mathbf{B} are A\mathbf{A} times the columns of B\mathbf{B} (P1.B6), so each is a combination of columns of A\mathbf{A}. They stay inside the column space of A\mathbf{A}, so the product cannot have more independent columns than A\mathbf{A} has. In the same way the rows of AB\mathbf{A}\mathbf{B} are combinations of the rows of B\mathbf{B}, so the product cannot have more independent rows than B\mathbf{B}. Since the number of independent rows equals the number of independent columns, both limits apply.

The inequality can be strict. (1000)(0001)=(0000)\begin{pmatrix}1&0\\0&0\end{pmatrix}\begin{pmatrix}0&0\\0&1\end{pmatrix}=\begin{pmatrix}0&0\\0&0\end{pmatrix}: both factors have rank 11 and the product has rank 00.

The outer product has rank 1. A column times a row, ab⊤\mathbf{a}\mathbf{b}^\top, is the smallest example:

(123)(45)=(458101215)\begin{aligned} \begin{pmatrix}1\\2\\3\end{pmatrix}\begin{pmatrix}4&5\end{pmatrix}&=\begin{pmatrix}4&5\\8&10\\12&15\end{pmatrix} \end{aligned}

Every column is a multiple of (1,2,3)(1,2,3) and every row is a multiple of (4,5)(4,5), so the rank is 11 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 B\mathbf{B} is m×rm\times r and A\mathbf{A} is r×pr\times p, then BA\mathbf{B}\mathbf{A} has rank at most rr, even if mm and pp are huge. This delivers the promise from P1.B6 about LoRA. Fine-tuning a 1000×10001000\times1000 weight matrix with a LoRA update BA\mathbf{B}\mathbf{A} of inner size r=8r=8 stores 1000⋅8+8⋅1000=16,0001000\cdot8+8\cdot1000=16{,}000 numbers instead of 1,000,0001{,}000{,}000, and the update has rank at most 88. 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, W2W1\mathbf{W}_2\mathbf{W}_1 with an inner size kk and no activation function between them, have rank at most kk: no matter how big the input and output are, the outputs lie in a kk-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 QK⊤\mathbf{Q}\mathbf{K}^\top is T×TT\times T, but Q\mathbf{Q} and K\mathbf{K} have dkd_k columns, so before the softmax it has rank at most dkd_k (P7.3). That bound says something only when T>dkT>d_k, 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 rank⁡(A+B)≤rank⁡A+rank⁡B\operatorname{rank}(\mathbf{A}+\mathbf{B})\le\operatorname{rank}\mathbf{A}+\operatorname{rank}\mathbf{B}: every column of the sum is a column of A\mathbf{A} plus a column of B\mathbf{B}, 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: BA=∑k=1rbkrk⊤\mathbf{B}\mathbf{A}=\sum_{k=1}^{r}\mathbf{b}_k\mathbf{r}_k^\top with bk\mathbf{b}_k the columns of B\mathbf{B} and rk⊤\mathbf{r}_k^\top the rows of A\mathbf{A}. Each outer product has rank at most 11, so the sum has rank at most rr. 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 X\mathbf{X} with NN examples (rows) and dd 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 dd. A typical case: a total that is the sum of two other columns. Then there is a non-zero vector n\mathbf{n} with Xn=0\mathbf{X}\mathbf{n}=\mathbf{0} (here n=(1,1,−1)\mathbf{n}=(1,1,-1) for the columns weekday, weekend and total), a null direction, and a linear model y^=Xw\hat{y}=\mathbf{X}\mathbf{w} cannot tell w\mathbf{w} from w+t n\mathbf{w}+t\,\mathbf{n}: 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 (N<dN<d) the rank is at most N<dN<d, 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 dd 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.

Try it: rank, independence, coordinates and products

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 00 means the direction is flattened. P1.B13 explains them.) NumPy’s default tolerance is the largest singular value times max⁡(m,n)\max(m,n) times the machine epsilon, about 2.2×10−162.2\times10^{-16} for 64-bit floats. For M\mathbf{M} the singular values are about 16.84816.848, 1.0681.068 and a number around 10−1610^{-16} (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.

Try it: redundant features, rank plus nullity, and the tolerance

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Read the output like this:

  • X has 3 columns but rank 2, so the weights (1,2,0)(1,2,0) and (4,5,−3)(4,5,-3) give the same predictions, because they differ by 3 (1,1,−1)3\,(1,1,-1) and X(1,1,−1)=0\mathbf{X}(1,1,-1)=\mathbf{0}.
  • For A\mathbf{A} the rank is 22 and the null space has one dimension: 2+1=32+1=3 columns.
  • With noise of size 10−910^{-9} added to the dependent column, the columns are no longer exactly dependent. The default tolerance is tiny (about 10−1410^{-14} here), so NumPy reports rank 3. The smallest singular value is about 5×10−105\times10^{-10}, far below the others but above that tolerance. Telling matrix_rank to treat anything below 10−610^{-6} as zero gives rank 2. Which one is “right” depends on whether 10−910^{-9} is noise to ignore or signal, and no formula decides that for you.

Common mistakes

Check your understanding

  1. What is the span of the two vectors (2,4)(2,4) and (−1,−2)(-1,-2) in the plane?

  2. Which of these sets is a subspace of R2\mathbb{R}^2?

  3. What is the rank of (123246001)\begin{pmatrix}1&2&3\\2&4&6\\0&0&1\end{pmatrix}?

  4. A 4×64\times6 matrix has rank 3. What is the dimension of its null space?

  5. B\mathbf{B} is 100×4100\times4 and A\mathbf{A} is 4×1004\times100. What can you say about the rank of BA\mathbf{B}\mathbf{A}, which is 100×100100\times100?

Exercises

  1. Are (1,2)(1,2) and (3,5)(3,5) independent? What is their span?
    Show answer1⋅5−2⋅3=−1≠01\cdot5-2\cdot3=-1\ne0, so they are independent. Two independent vectors in the plane span the whole plane, and they are a basis.
  2. Find the coordinates of (5,1)(5,1) in the basis (1,1),(1,−1)(1,1),(1,-1).
    Show answerSolve c1(1,1)+c2(1,−1)=(5,1)c_1(1,1)+c_2(1,-1)=(5,1): c1+c2=5c_1+c_2=5 and c1−c2=1c_1-c_2=1, so c1=3c_1=3 and c2=2c_2=2. The coordinates are (3,2)(3,2). Check: 3(1,1)+2(1,−1)=(5,1)3(1,1)+2(1,-1)=(5,1).
  3. Show that (1,2,3)(1,2,3), (4,5,6)(4,5,6) and (7,8,9)(7,8,9) are dependent, by finding weights.
    Show answer−1⋅(1,2,3)+2⋅(4,5,6)−1⋅(7,8,9)=(−1+8−7, −2+10−8, −3+12−9)=(0,0,0)-1\cdot(1,2,3)+2\cdot(4,5,6)-1\cdot(7,8,9)=(-1+8-7,\ -2+10-8,\ -3+12-9)=(0,0,0). The weights −1,2,−1-1,2,-1 are not all zero, so the set is dependent. The rank of the matrix with these columns is 2.
  4. Say whether each set is a subspace of R2\mathbb{R}^2 or R3\mathbb{R}^3, and why: (a) {(x,y):x+y=0}\{(x,y): x+y=0\}; (b) {(x,y):x+y=1}\{(x,y): x+y=1\}; (c) {(x,y,z):z=0}\{(x,y,z): z=0\}; (d) {(x,y):xy=0}\{(x,y): xy=0\}.
    Show answer(a) Yes: it is the line through the origin with direction (1,−1)(1,-1). (b) No: it does not contain (0,0)(0,0). (c) Yes: it is the horizontal plane through the origin (the xyxy-plane), closed under adding and scaling. (d) No: (1,0)(1,0) and (0,1)(0,1) are in it, but their sum (1,1)(1,1) is not.
  5. Find the rank of (120240361)\begin{pmatrix}1&2&0\\2&4&0\\3&6&1\end{pmatrix}.
    Show answerRow 2 is twice row 1. Row 3, (3,6,1)(3,6,1), 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.)
  6. What is the largest possible rank of a 5×35\times3 matrix, and of a 2×1002\times100 matrix? When does a 3×53\times5 matrix have full rank, and what does its column space look like then?
    Show answerThe ranks are at most min⁡(5,3)=3\min(5,3)=3 and min⁡(2,100)=2\min(2,100)=2. A 3×53\times5 matrix has full rank when its rank is 33, so three of its columns are independent; its columns then span all of R3\mathbb{R}^3, and every output in R3\mathbb{R}^3 is reachable.
  7. A 4×74\times7 matrix has rank 3. How many dimensions does the null space have, and what kind of set is the column space inside R4\mathbb{R}^4?
    Show answerRank plus nullity is the number of columns, 7, so the nullity is 7−3=47-3=4. The column space has dimension 3: a flat 3-dimensional subspace of R4\mathbb{R}^4 (it is not all of R4\mathbb{R}^4, which has dimension 4).
  8. Compute the outer product (21)(34)\begin{pmatrix}2\\1\end{pmatrix}\begin{pmatrix}3&4\end{pmatrix} and find its rank.
    Show answerIt is (6834)\begin{pmatrix}6&8\\3&4\end{pmatrix}. Row 1 is twice row 2, so the rank is 1. (Any outer product of two non-zero vectors has rank 1.)
  9. 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 answerThe 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 n\mathbf{n} with Xn=0\mathbf{X}\mathbf{n}=\mathbf{0}, so adding any multiple of n\mathbf{n} to the weights does not change the predictions: the best weights are not unique.
  10. Find the coordinates of (3,1)(3,1) in the basis (1,0)(1,0) and (1,0.001)(1,0.001), and say what they warn you about.
    Show answerc2⋅0.001=1c_2\cdot0.001=1 gives c2=1000c_2=1000, and c1+c2=3c_1+c_2=3 gives c1=−997c_1=-997. 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.
  11. Code: predict the rank of np.ones((4, 4)) and of np.eye(4)[:, :3], then check with matrix_rank.
    Show answerThe first is 11: every column is the same vector, so there is only one independent direction. The second is 33: the matrix has the first three columns of the identity, which are independent (and a 4×34\times3 matrix cannot have rank above 3).

Key takeaways

  • The span of some vectors is every linear combination of them: all the points you can reach by scaling and adding. One non-zero vector spans a line, two vectors that do not lie on one line span the plane, and every span passes through the origin.
  • Vectors are dependent if one of them is a combination of the others, or equivalently if some combination with weights that are not all zero gives 0\mathbf{0}. A set with the zero vector, or with more than nn vectors in Rn\mathbb{R}^n, is always dependent.
  • A basis is an independent set that spans the space. Every vector has exactly one set of coordinates in a basis, a space has many bases, and they all have the same size: the dimension.
  • A subspace is a set that contains 0\mathbf{0} and is closed under adding and scaling: a line or plane through the origin, not one that misses it. A vector space is any set with adding and scaling that obey the usual rules.
  • The column space of a matrix is the span of its columns, which is the set of all outputs Ax\mathbf{A}\mathbf{x}. The rank is its dimension: the number of independent columns, which equals the number of independent rows, and is at most min⁡(m,n)\min(m,n).
  • Rank plus nullity equals the number of columns: of the nn input directions, the rank survives and the nullity is squashed to 0\mathbf{0}.
  • rank⁡(AB)≤min⁡(rank⁡A,rank⁡B)\operatorname{rank}(\mathbf{A}\mathbf{B})\le\min(\operatorname{rank}\mathbf{A},\operatorname{rank}\mathbf{B}), so a product of an m×rm\times r and an r×pr\times p matrix has rank at most rr. This is why LoRA updates are low rank. Separately, a column that repeats information gives a null direction, which makes regression weights ambiguous.
Found a mistake? Tell us