Chapter 5 of 10 · ~22 min read & lab+55 XP
Chapter 5 · Linear Algebra

Span, Linear Independence, Basis, Rank & The Four Subspaces

Understand true data dimensionality, why redundant features break regression, and Gilbert Strang's Four Fundamental Subspaces

Why ML Engineers & Data Scientists Actually Need This

Rank & Linear Independence explain Multicollinearity, the One-Hot Dummy Variable Trap, and why LoRA fine-tuning works!

ML / AI Engineer

Modern AI breakthroughs like LoRA prove that giant weight updates ΔW actually have tiny effective RANK (r = 8 instead of 4096) — slashing GPU VRAM by 98%!

Data Scientist

Adding a redundant feature (e.g., Temp_F alongside Temp_C, or keeping all K one-hot columns with an intercept) drops Rank(X) < d and breaks Linear Regression coefficient interpretability!

Interview & Debug Edge

'What happens to OLS Linear Regression when two features are perfectly collinear, and how does Ridge fix it?' — a guaranteed Data Science interview classic!

Step 0 · Test Your Gut Before Any Math!

Your housing dataset has 5 columns: [SqFt, Bedrooms, Temp_C, Temp_F, Price], where Temp_F = 1.8·Temp_C + 32. What is the maximum possible RANK of the 4 feature columns?

What You Will Master in This Chapter
  • Visualize the Span of a set of vectors and test whether vectors are Linearly Independent or Dependent (collinear).
  • Define a Basis and explain how changing basis (like PCA!) rotates coordinates without losing information.
  • Identify all Four Fundamental Subspaces (Column Space, Row Space, Null Space, Left Null Space) and their exact dimensions.
  • Apply the Rank–Nullity Theorem to diagnose multicollinearity, dummy-variable traps, and low-rank structure in real ML datasets.
Plain-English Symbol Decoder · Click Any Symbol Before We Begin
No mysterious math notation — translate everything into English first
How to Say It Aloud
“Span of v₁ and v₂”
Plain-English Meaning
EVERY single destination you can reach by combining arrows v₁ and v₂ with any weights!
Everyday Example
East + North span the entire 2D city map; East + West only span a 1D road!
Story-First Intuition, Visual Diagrams & Interactive Walkthroughs

Just because a dataset has 50 columns does not mean it carries 50 independent dimensions of information! Span, Linear Independence, Basis, and Rank give you X-ray vision into the true geometry of your feature matrix XX and explain why redundant columns create fatal multicollinearity.

1. Span & Linear Independence — How Much Space Can Your Vectors Reach?

Everyday Story First (Zero Math Yet!)

Imagine a video-game character with two joystick moves:

  • If Move 1 walks East and Move 2 walks North, you can reach ANY spot on the entire 2D map! (Span = whole 2D plane, Rank = 2!)
  • Now imagine Move 1 walks 1 step East and Move 2 walks 2 steps East — you are trapped forever on one 1D road! Move 2 was completely redundant (Linearly Dependent, Rank = 1)!
Span of 1 Vector1D Line through origin (rank 1)2 Independent VectorsFills ALL of ℝ² (rank 2)2 Dependent VectorsCollinear → trapped on 1D line (rank 1)
Figure 5.1 — Span & Linear Independence: two independent vectors span all of ℝ², whereas two collinear vectors are trapped on a single 1D line.

What Is a Span?

The span of vectors v1,…,vk\mathbf{v}_1, \dots, \mathbf{v}_k is the set of every single vector you can build using linear combinations: span(v1,…,vk)={c1v1+c2v2+⋯+ckvk∣ci∈R}\text{span}(\mathbf{v}_1, \dots, \mathbf{v}_k) = \{ c_1 \mathbf{v}_1 + c_2 \mathbf{v}_2 + \dots + c_k \mathbf{v}_k \mid c_i \in \mathbb{R} \}

  • Span of 1 non-zero vector in R2\mathbb{R}^2: An infinite 1D line passing through the origin.
  • Span of 2 non-parallel vectors in R2\mathbb{R}^2: The entire 2D plane R2\mathbb{R}^2!
  • Span of 2 parallel (collinear) vectors in R2\mathbb{R}^2: Still trapped on the same 1D line — the second vector added zero new reach!

Linear Independence vs. Dependence

Vectors v1,…,vk\mathbf{v}_1, \dots, \mathbf{v}_k are linearly independent if no vector is redundant (none can be written as a linear combination of the others). Formally, the only way to combine them to get zero is the trivial all-zeros solution: c1v1+c2v2+⋯+ckvk=0  ⟹  c1=c2=⋯=ck=0c_1 \mathbf{v}_1 + c_2 \mathbf{v}_2 + \dots + c_k \mathbf{v}_k = \mathbf{0} \implies c_1 = c_2 = \dots = c_k = 0 If you can find any non-zero weights giving 0\mathbf{0} (e.g. 2v1−v2=02\mathbf{v}_1 - \mathbf{v}_2 = \mathbf{0}), they are linearly dependent!

Worked Example 1 — Testing Linear Independence by Hand

  1. Are v1=[1,2]T\mathbf{v}_1 = [1, 2]^T and v2=[3,6]T\mathbf{v}_2 = [3, 6]^T independent?
    • No! Notice v2=3v1\mathbf{v}_2 = 3\mathbf{v}_1, so 3v1−1v2=03\mathbf{v}_1 - 1\mathbf{v}_2 = \mathbf{0} (non-zero weights!). They are linearly dependent and span only a 1D line.
  2. Are u1=[1,0,1]T\mathbf{u}_1 = [1, 0, 1]^T, u2=[0,1,1]T\mathbf{u}_2 = [0, 1, 1]^T, u3=[1,1,2]T\mathbf{u}_3 = [1, 1, 2]^T independent?
    • No! Look closely: u1+u2=[1,1,2]T=u3\mathbf{u}_1 + \mathbf{u}_2 = [1, 1, 2]^T = \mathbf{u}_3! So 1u1+1u2−1u3=01\mathbf{u}_1 + 1\mathbf{u}_2 - 1\mathbf{u}_3 = \mathbf{0}. These 3 vectors only span a 2D plane inside R3\mathbb{R}^3!
Formula Anatomy · Click Any Colored Piece to Decode It
Rank-Nullity Theorem: rank(A) + nullity(A) = n (total columns)

Every input feature column either adds NEW information (rank) OR collapses into redundancy (nullspace)!

Dimensions That SURVIVE

Number of truly independent signal directions kept by matrix A.

In our example: 3 unique features out of 5 columns
Put it together with real numbers: A compression autoencoder mapping 784 image pixels down to a 32D bottleneck MUST crush at least 784 − 32 = 752 dimensions into its nullspace!
Try It Yourself

Are the three 2D vectors [1, 0]ᵀ, [0, 1]ᵀ, and [2, 3]ᵀ linearly independent or dependent? Why?

2. Basis & Coordinates — Choosing the Best Axes for Your Data

Everyday Story First (Zero Math Yet!)

Here is a wild miracle of Linear Algebra that surprises everyone: for ANY rectangular matrix (even 10,000 rows × 5 columns!), the number of independent rows ALWAYS equals the number of independent columns — so Row Rank = Column Rank = rank(A)!

A basis of a vector space VV is a set of vectors that is just right:

  1. They span all of VV (enough vectors to reach every point), AND
  2. They are linearly independent (no redundant vectors!).

Every vector x∈V\mathbf{x} \in V has a unique coordinate representation in a given basis!

  • The standard basis of R2\mathbb{R}^2 is i^=[1,0]T,j^=[0,1]T\hat{i} = [1, 0]^T, \hat{j} = [0, 1]^T.
  • Why ML engineers love changing basis: The standard feature axes (height, weight, waist size) are heavily correlated. PCA simply finds a new orthonormal rotated basis where the first axis captures maximum variance and all axes are uncorrelated!

Worked Example 2 — Expressing a Point in a New Basis

Suppose our basis is b1=[1,1]T\mathbf{b}_1 = [1, 1]^T and b2=[1,−1]T\mathbf{b}_2 = [1, -1]^T. What are the coordinates of x=[5,1]T\mathbf{x} = [5, 1]^T in this new basis? Solve c1[1,1]T+c2[1,−1]T=[5,1]Tc_1[1, 1]^T + c_2[1, -1]^T = [5, 1]^T:

  • c1+c2=5c_1 + c_2 = 5 and c1−c2=1c_1 - c_2 = 1.
  • Adding gives 2c1=6  ⟹  c1=32c_1 = 6 \implies \mathbf{c_1 = 3}, and c2=2\mathbf{c_2 = 2}. Check: 3[1,1]T+2[1,−1]T=[5,1]T3[1, 1]^T + 2[1, -1]^T = [5, 1]^T!
Pause & Check · Quick Intuition Pulse

Pause & Check: A matrix A has shape (1000, 4) — 1,000 rows and 4 columns. What is the LARGEST possible value of rank(A)?

3. Matrix Rank, The Four Fundamental Subspaces & Rank–Nullity

Everyday Story First (Zero Math Yet!)

Every Data Scientist hits the Dummy Variable Trap at least once: you one-hot encode is_Day and is_Night, keep an intercept column of all 1s, and suddenly is_Day + is_Night = 1 creates exact linear dependence! Toggle redundant ML columns below to watch Rank(X) drop and OLS crash live!

Input Space ℝⁿ (n columns)Row Space C(Aᵀ)dimension = r (rank)Null Space N(A)dim = n − r (Ax = 0)A maps 1-to-1 onto C(A)Crushed to 0Output Space ℝᵐ (m rows)Column Space C(A)dimension = r (all Ax)Left Null Space N(Aᵀ)dim = m − r (residuals!)
Figure 5.2 — The Four Fundamental Subspaces & Rank–Nullity: Row Space (dim r) + Null Space (dim n − r) = ℝⁿ; Column Space (dim r) + Left Null Space (dim m − r) = ℝᵐ.

For any matrix A∈Rm×nA \in \mathbb{R}^{m \times n}, its rank r=rank(A)r = \text{rank}(A) is the number of linearly independent columns — which astonishingly ALWAYS equals the number of linearly independent rows (and the number of pivots after Gaussian elimination, and the number of non-zero singular values!).

Every m×nm \times n matrix naturally carves input space Rn\mathbb{R}^n and output space Rm\mathbb{R}^m into 4 Fundamental Subspaces:

  1. Column Space C(A)⊆Rm\mathcal{C}(A) \subseteq \mathbb{R}^m (dim =r= r): Span of AA's columns — all possible model predictions AxA\mathbf{x}.
  2. Row Space C(AT)⊆Rn\mathcal{C}(A^T) \subseteq \mathbb{R}^n (dim =r= r): Span of AA's rows — the active input directions that AA preserves.
  3. Null Space N(A)⊆Rn\mathcal{N}(A) \subseteq \mathbb{R}^n (dim =n−r= n - r): All input vectors killed by AA (Ax=0A\mathbf{x} = \mathbf{0}).
  4. Left Null Space N(AT)⊆Rm\mathcal{N}(A^T) \subseteq \mathbb{R}^m (dim =m−r= m - r): All vectors orthogonal to every column of AA (ATr=0A^T\mathbf{r} = \mathbf{0}) — in Linear Regression, the OLS residual vector r=y−Xw^\mathbf{r} = \mathbf{y} - X\hat{\mathbf{w}} lives right here!

The Rank–Nullity Theorem

rank(A)⏟surviving dims+dim⁡(N(A))⏟crushed dims (nullity)=n(number of columns)\underbrace{\text{rank}(A)}_{\text{surviving dims}} + \underbrace{\dim(\mathcal{N}(A))}_{\text{crushed dims (nullity)}} = n \quad (\text{number of columns})

Hands-On Mini-Lab · Real ML Rank & Multicollinearity Detector!

Toggle redundant columns in a Data Science feature table below and watch Rank(X) vs. number of columns — see why redundant features make (XᵀX)⁻¹ crash in Linear Regression!

Total Columns (d)
4
True Rank (Unique Info)
3
OLS (XᵀX)⁻¹ Status
✗ Rank Deficient (CRASH!)
Try It Yourself

A feature matrix X has shape (500, 15) and rank 12. What are the dimensions of (a) Column Space C(X), (b) Null Space N(X), and (c) Left Null Space N(Xᵀ)?

4. Multicollinearity & The Dummy-Variable Trap in Data Science

Feature Matrix X (3 columns, rank = 2!)sqft[1000, 1500]price_usd[10, 25]price_cents= 100 × col2Consequence: Non-Zero Null Space Vector!z = [0, 100, -1]ᵀ ⇒ X·z = 0Any weights (w + c·z) give identical predictions →XᵀX is singular! Fix: drop redundant col or add Ridge λI.
Figure 5.3 — Multicollinearity in ML: adding a derived column (price_cents = 100 × price_usd) leaves rank unchanged and opens a Null Space of infinite OLS weights.

Now see how Rank & Null Space explain two classic Data Science bugs instantly:

  1. Duplicate / Derived Columns: If your dataset includes both price_usd and price_cents = 100 * price_usd, column 3 is a scalar multiple of column 2. Vector z=[0,100,−1]T\mathbf{z} = [0, 100, -1]^T sits in N(X)\mathcal{N}(X), so X(w+cz)=XwX(\mathbf{w} + c\mathbf{z}) = X\mathbf{w} for any cc — infinitely many OLS solutions!
  2. The One-Hot Dummy-Variable Trap: Suppose you one-hot encode region ∈ {East, West, North} into 3 binary columns is_east, is_west, is_north AND keep an all-ones intercept column 1. Notice: is_east+is_west+is_north=1(exact linear dependence!)\text{is\_east} + \text{is\_west} + \text{is\_north} = \mathbf{1} \quad (\text{exact linear dependence!}) Fix: Drop one reference category (drop_first=True) in unregularized OLS, OR use Ridge/L2 regularization!
“Wait, I'm Confused...” · Teacher Office Hours Q&A

The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.

Technically yes (rank = full), BUT your matrix becomes ill-conditioned (almost singular!), causing OLS regression weights to explode into absurd opposites like +50,000 and −50,000! That's why Data Scientists check VIF (Variance Inflation Factor) and use Ridge Regression (L2) to stabilize near-collinear features!

Interactive Visual Lab
Lab Challenge: Rotate vector v₂ until v₁ and v₂ become collinear (Rank collapses from 2 to 1 and Nullity jumps to 1)!
v₁v₂
Rotate Vector v₂110°
Rank (Span Dim)
2 (All ℝ²)
Nullity (n − r)
0

v₁ and v₂ are linearly independent — they form a valid basis reaching every single point in ℝ²!

Geometric Takeaway: Rank is the true number of linearly independent dimensions in your data. By Rank–Nullity (rank + nullity = n), every redundant feature creates a Null Space direction that breaks unique OLS invertibility.
Where This Powers Real ML, Deep Learning & GenAI

Diagnosing Multicollinearity (VIF & matrix_rank)

Checking np.linalg.matrix_rank(X) < X.shape[1] immediately catches exact feature redundancies before fitting OLS or logistic regression.

Low-Rank Structure in Recommender Systems & LoRA

A rank-r matrix has only r independent basis patterns — allowing a 4096×4096 LLM weight update to be represented cleanly as a rank-16 product BA.

PCA as Optimal Orthonormal Change of Basis

PCA replaces correlated feature columns with an orthogonal basis ordered by variance so dropping low-variance basis vectors compresses data with minimal loss.

Rank Collapse in Deep Attention Networks

Without skip connections and LayerNorm, stacking pure self-attention layers pushes token representations toward rank-1 (all tokens identical) — skip connections preserve full rank!

Production NumPy & PyTorch Code Patterns
import numpy as np

# Matrix with a collinear 3rd column (col3 = 100 * col2)
X = np.array([[1000.0, 10.0, 1000.0],
              [1500.0, 25.0, 2500.0],
              [1200.0, 15.0, 1500.0],
              [1800.0, 30.0, 3000.0]])

rank = np.linalg.matrix_rank(X)        # 2 (strictly less than 3 columns!)
nullity = X.shape[1] - rank            # 1 (1D null space!)

# Extract an orthonormal basis for the Null Space via SVD:
_, S, Vt = np.linalg.svd(X, full_matrices=True)
null_basis = Vt[rank:].T               # shape: (3, 1) -> satisfies X @ null_basis ≈ 0!
assert np.allclose(X @ null_basis, 0.0, atol=1e-10)
Common ML Interview Traps & Numerical Pitfalls
Trap #1: Forgetting the Dummy-Variable Trap when one-hot encoding with an intercept
Best Practice: If you include all K one-hot columns PLUS an intercept column of 1s, their sum equals the intercept — making X rank-deficient! Use `drop_first=True` for unregularized OLS (not needed for Ridge/Lasso/Trees).
Trap #2: Thinking rank(AB) can exceed rank(A) or rank(B)
Best Practice: Always remember: rank(AB) ≤ min(rank(A), rank(B))! Multiplying matrices can NEVER create new rank — which is why a bottleneck layer of width r caps representation rank at r.
Trap #3: Confusing 'more rows than columns' with 'full column rank'
Best Practice: A 10,000 × 5 matrix can still have rank 2 if 3 of its columns are linear combinations of the other 2!
Chapter Mastery Check (+55 XP)
Score: 0 / 5
1Which set of 2D vectors forms a valid BASIS for ℝ²?
2A design matrix X has 200 rows and 10 columns, with rank 7. What is the dimension of its Null Space N(X)?
3If A has rank 4 and B has rank 8, what is the MAXIMUM possible rank of the product AB?
4You one-hot encode a 4-category column into 4 binary features AND include an intercept column of all 1s. Why is XᵀX singular?
5In Ordinary Least Squares regression, which of the Four Fundamental Subspaces contains the optimal residual vector r = y − Xŵ?

Apply What You Learned — Solve Matching Coding Problems