Linear Independence, Basis and Rank
How many numbers do you really need to describe your data? If one feature is the sum of two others, it adds no information, and some algorithms even break on it. Linear independence detects such redundancy, a basis is a redundancy-free set of building blocks, and the rank counts them.
Linear independence
Vectors are linearly independent if the only way to combine them into zero is with all coefficients zero:
Otherwise one of them is a combination of the others: it is redundant. To test, put the vectors as columns of a matrix and row reduce: they are independent exactly when every column has a pivot, that is, when the rank equals .
Basis and dimension
A basis of a vector space is an independent set that spans (every vector of is a combination of it). Every basis of has the same size, the dimension . Coordinates with respect to a basis are unique, which is what makes a basis useful: the coordinates are a compact, lossless description.
Rank
The rank of is the number of independent columns, which always equals the number of independent rows. It is , and the rank-nullity theorem links it to the null space:
Full column rank () means , so has at most one solution and is invertible.
import numpy as np
rng = np.random.default_rng(0)
size = rng.uniform(50, 150, 6) # m^2
rooms = rng.integers(1, 6, 6).astype(float)
size_ft = 10.764 * size # the same feature in square feet!
X = np.column_stack([size, rooms, size_ft])
print("columns:", X.shape[1], " rank:", np.linalg.matrix_rank(X))
G = X.T @ X
print("cond(X^T X) = %.2e" % np.linalg.cond(G))
X2 = X[:, :2]
print("after dropping the duplicate: rank", np.linalg.matrix_rank(X2),
" cond = %.2e" % np.linalg.cond(X2.T @ X2))columns: 3 rank: 2
cond(X^T X) = 8.36e+16
after dropping the duplicate: rank 2 cond = 2.92e+03
The square-feet column is times the square-metre column, so the three columns span only a 2-dimensional space. The Gram matrix is singular (its condition number is astronomically large), and the normal equations of linear regression have no unique solution. This is perfect multicollinearity.
Numerical rank
With floating-point data, exact dependence is rare, but near dependence is common. np.linalg.matrix_rank counts singular values above a tolerance; the SVD shows how close to dependent the columns are.
import numpy as np
rng = np.random.default_rng(1)
a = rng.normal(size=100)
b = rng.normal(size=100)
c = a + b + 1e-9 * rng.normal(size=100) # almost a + b
M = np.column_stack([a, b, c])
s = np.linalg.svd(M, compute_uv=False)
print("singular values:", s)
print("rank with default tolerance:", np.linalg.matrix_rank(M))
print("rank with tol=1e-6:", np.linalg.matrix_rank(M, tol=1e-6))singular values: [1.59942007e+01 9.20598542e+00 5.35307700e-09]
rank with default tolerance: 3
rank with tol=1e-6: 2
The third singular value is about : the columns are independent in exact arithmetic, so the default tolerance (a tiny multiple of machine precision) reports rank 3, but with a tolerance that matches the noise level of real data the matrix is effectively rank 2. Treat such a matrix as rank-deficient.
In software
- scikit-learn's
OneHotEncoder(drop="first")exists because one-hot columns plus an intercept are linearly dependent (the columns of a category always sum to 1). - Ridge regression adds to , which makes it invertible even with dependent features (MAP and Regularization).
- Low-rank approximation keeps only the directions that matter.
Exercises
Exercise 1: Independent?
Are , and linearly independent?
Answer
No: .
Solution
Look for with . The first entry gives , the second so , and the third checks: . A nontrivial combination equals zero: .
Exercise 2: Rank-nullity
A matrix has rank 4. What is the dimension of its null space?
Answer
Solution
. (The rank can be at most .)
Exercise 3: One-hot trap (code)
Build the matrix with columns: intercept (all ones), "is red", "is green", "is blue" for six examples colored R, G, B, R, B, G. What is its rank?
Answer
3, not 4.
Solution
The three indicator columns add up to the ones column, so one column is redundant.
import numpy as np
colors = ["R", "G", "B", "R", "B", "G"]
X = np.array([[1, c == "R", c == "G", c == "B"] for c in colors], dtype=float)
print("rank:", np.linalg.matrix_rank(X))
print("red + green + blue - intercept:", X[:, 1] + X[:, 2] + X[:, 3] - X[:, 0])rank: 3
red + green + blue - intercept: [0. 0. 0. 0. 0. 0.]
Test yourself
The rank of a matrix equals…
- the number of rows
- the number of linearly independent columns
- the number of nonzero entries
- the largest eigenvalue
A data matrix has rank 2. Then is…
- invertible
- singular
- the identity
By rank-nullity, an matrix with full column rank has a null space of dimension…