TExcelTAD
Home › Math for Machine Learning › Linear Independence, Basis and Rank

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 x1,…,xk\mathbf{x}_1, \ldots, \mathbf{x}_k are linearly independent if the only way to combine them into zero is with all coefficients zero:

λ1x1+⋯+λkxk=0  ⟹  λ1=⋯=λk=0\lambda_1\mathbf{x}_1 + \cdots + \lambda_k\mathbf{x}_k = \mathbf{0} \implies \lambda_1 = \cdots = \lambda_k = 0

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 kk.

Basis and dimension

A basis of a vector space VV is an independent set that spans VV (every vector of VV is a combination of it). Every basis of VV has the same size, the dimension dim⁡V\dim V. 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 A∈Rm×nA \in \mathbb{R}^{m \times n} is the number of independent columns, which always equals the number of independent rows. It is dim⁡Im⁡(A)\dim \operatorname{Im}(A), and the rank-nullity theorem links it to the null space:

rk⁡(A)+dim⁡ker⁡(A)=n\operatorname{rk}(A) + \dim \ker(A) = n

Full column rank (rk⁡(A)=n\operatorname{rk}(A) = n) means ker⁡(A)={0}\ker(A) = \{\mathbf{0}\}, so Ax=bA\mathbf{x} = \mathbf{b} has at most one solution and ATAA^T A is invertible.

python · try it yourself
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 10.76410.764 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.

python · try it yourself
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 5×10−95 \times 10^{-9}: 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 λI\lambda I to XTXX^T X, 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 (1,2,3)(1, 2, 3), (0,1,1)(0, 1, 1) and (2,5,7)(2, 5, 7) linearly independent?

Answer

No: (2,5,7)=2(1,2,3)+(0,1,1)(2, 5, 7) = 2(1, 2, 3) + (0, 1, 1).

Solution

Look for λ1,λ2\lambda_1, \lambda_2 with λ1(1,2,3)+λ2(0,1,1)=(2,5,7)\lambda_1(1,2,3) + \lambda_2(0,1,1) = (2,5,7). The first entry gives λ1=2\lambda_1 = 2, the second 4+λ2=54 + \lambda_2 = 5 so λ2=1\lambda_2 = 1, and the third checks: 6+1=76 + 1 = 7. A nontrivial combination equals zero: 2x1+x2−x3=02\mathbf{x}_1 + \mathbf{x}_2 - \mathbf{x}_3 = \mathbf{0}.

Exercise 2: Rank-nullity

A 5×85 \times 8 matrix has rank 4. What is the dimension of its null space?

Answer
Solution

dim⁡ker⁡(A)=n−rk⁡(A)=8−4=4\dim \ker(A) = n - \operatorname{rk}(A) = 8 - 4 = 4. (The rank can be at most min⁡(5,8)=5\min(5, 8) = 5.)

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.

python · try it yourself
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…

  1. the number of rows
  2. the number of linearly independent columns
  3. the number of nonzero entries
  4. the largest eigenvalue

A 100×3100 \times 3 data matrix has rank 2. Then XTXX^T X is…

  1. invertible
  2. singular
  3. the identity
  4. 100×100100 \times 100

By rank-nullity, an m×nm \times n matrix with full column rank has a null space of dimension…

  1. nn
  2. mm
  3. 00
  4. m−nm - n