Column Space, Row Space and Null Space
Every matrix comes with three subspaces: the column space (all outputs ), the row space (spanned by the rows) and the null space (all with ). One row reduction gives bases for all three, and their dimensions are tied together by the rank-nullity theorem.
The three subspaces
For an matrix :
| Subspace | Definition | Lives in | Basis from |
|---|---|---|---|
| column space | span of the columns | the original columns of at the pivot positions | |
| row space | span of the rows | the non-zero rows of | |
| null space | one vector per free variable (solve ) |
Why the recipes work:
- Column space: is a combination of the columns, and the pivot columns are a basis of their span (Basis and Dimension).
- Row space: row operations replace rows by combinations of rows and can be undone, so they do not change the row space. The non-zero rows of span it and are independent (each has a leading 1 where the rows below have zeros).
- Null space: and have the same solutions; write the pivot variables in terms of the free ones (Homogeneous Systems).
Also: is consistent exactly when .
One reduction, three bases
from sympy import Matrix
A = Matrix([[1, 2, 0, 1, 3],
[2, 4, 1, 4, 7],
[1, 2, 2, 3, 3]])
R, piv = A.rref()
print("rref rows:")
for i in range(R.rows):
print(" ", list(R.row(i)))
print("pivot columns:", piv)
print("col(A) basis:", [list(A.col(j)) for j in piv])
print("row(A) basis:", [list(R.row(i)) for i in range(len(piv))])
print("null(A) basis:", [list(v) for v in A.nullspace()])
print("rank =", A.rank(), " nullity =", len(A.nullspace()), " n =", A.cols)rref rows:
[1, 2, 0, 0, 2]
[0, 0, 1, 0, -1]
[0, 0, 0, 1, 1]
pivot columns: (0, 2, 3)
col(A) basis: [[1, 2, 1], [0, 1, 2], [1, 4, 3]]
row(A) basis: [[1, 2, 0, 0, 2], [0, 0, 1, 0, -1], [0, 0, 0, 1, 1]]
null(A) basis: [[-2, 1, 0, 0, 0], [-2, 0, 1, -1, 1]]
rank = 3 nullity = 2 n = 5
Rank and the rank-nullity theorem
The number of pivots counts three things at once:
So row rank equals column rank (), a surprising fact for a non-square matrix. Every column is either a pivot column or a free column, which gives the
Rank-nullity theorem. For an matrix ,
In the example: . Consequences: a 3×5 matrix always has a null space of dimension at least 2; an matrix has exactly when , i.e. when is invertible.
In software: rank and null space in data
Take a data matrix (samples × features) and a linear model with predictions .
- Rank = number of truly independent features. Redundant columns (unit conversions, totals, one-hot dummies plus an intercept) lower the rank below the number of columns (Linear Independence).
- The null space is the set of invisible weight changes. If , then : adding to the weights changes no prediction at all. The model is not identifiable: the data cannot decide between and , which is why coefficients of collinear features are meaningless and regularisation is needed to pick one solution.
import numpy as np
rng = np.random.default_rng(5)
n = 50
temp_c = rng.uniform(-5, 35, n)
temp_f = 1.8 * temp_c + 32
X = np.column_stack([np.ones(n), temp_c, temp_f]) # intercept, °C, °F
print("rank:", int(np.linalg.matrix_rank(X)), "of", X.shape[1], "columns")
z = np.array([32.0, 1.8, -1.0]) # 32·1 + 1.8·C − F = 0
print("X @ z is zero:", np.allclose(X @ z, 0))
w = np.array([10.0, 0.5, 0.0])
w2 = w + 7 * z # very different weights...
print("w2 =", w2.tolist())
print("same predictions:", np.allclose(X @ w, X @ w2))rank: 2 of 3 columns
X @ z is zero: True
w2 = [234.0, 13.1, -7.0]
same predictions: True
The same ideas show up elsewhere: a recommendation system's user × item rating matrix is approximately low rank (a few taste factors explain most ratings), which is what matrix factorisation and the SVD exploit.
Exercises
Exercise 1: Bases by hand
For , find bases of , and , and check rank-nullity.
Answer
: ; : ; : ; .
Solution
from sympy import Matrix
A = Matrix([[1, 3, 2], [2, 6, 5]])
print(A.rref()[0].tolist(), [list(v) for v in A.nullspace()], [list(v) for v in A.columnspace()])[[1, 3, 0], [0, 0, 1]] [[-3, 1, 0]] [[1, 2], [2, 5]]
Exercise 2: Is reachable?
With from Exercise 1, is solvable? Is ?
Answer
Both are solvable, since (rank 2).
Solution
is a 2-dimensional subspace of , so it is all of and every is reachable. To find an , write in the column basis and put the coefficients on columns 1 and 3 ():
Adding any multiple of the null-space vector gives other solutions.
Exercise 3: Dimensions without computing
is a 6×9 matrix of rank 4. Give , and , and the dimension of the null space of .
Answer
4, 4, 5 and 2.
Solution
. Rank-nullity for (9 columns): . is 9×6 with the same rank 4, so has dimension .
Exercise 4: Find the invisible weights
A dataset has columns (an intercept, two features and their combination). Find a non-zero null-space vector , and show numerically that adding to some weights does not change the predictions.
Answer
(or any multiple).
Solution
gives , so .
import numpy as np
rng = np.random.default_rng(1)
a, b = rng.normal(size=(2, 30))
X = np.column_stack([np.ones(30), a, b, a + 2 * b])
z = np.array([0, 1, 2, -1.0])
w = np.array([1.0, -2.0, 0.5, 4.0])
print(np.allclose(X @ z, 0), np.allclose(X @ w, X @ (w + 3 * z)))True True
Test yourself
is 4×7 with rank 3. What is the dimension of its null space?
- 1
- 3
- 7
- 4
Which statement about row operations is true?
- They preserve the column space
- They preserve the columns
- They preserve the row space
- They change the rank
A linear model has a non-zero with . What does that mean?
- The model fits perfectly
- Different weights and give identical predictions
- has full column rank
- Training will diverge