TExcelTAD
Home › Math for Machine Learning › Vector Spaces and Subspaces

Vector Spaces and Subspaces

A vector space is any collection of objects you can add and scale without leaving the collection. Arrows in R3\mathbb{R}^3, polynomials, images and even functions qualify. ML cares because the set of things a model can produce (predictions of a linear model, outputs of a layer, the solutions of Ax=0A\mathbf{x} = \mathbf{0}) is very often a subspace, and subspaces have a dimension you can compute.

The definition in one breath

A real vector space VV is a set with an addition x+y\mathbf{x} + \mathbf{y} and a scalar multiplication λx\lambda\mathbf{x} that obey the familiar rules: addition is associative and commutative, there is a zero vector 0\mathbf{0} and negatives −x-\mathbf{x}, and scaling distributes:

λ(x+y)=λx+λy,(λ+ψ)x=λx+ψx,λ(ψx)=(λψ)x,1x=x\lambda(\mathbf{x} + \mathbf{y}) = \lambda\mathbf{x} + \lambda\mathbf{y}, \quad (\lambda + \psi)\mathbf{x} = \lambda\mathbf{x} + \psi\mathbf{x}, \quad \lambda(\psi\mathbf{x}) = (\lambda\psi)\mathbf{x}, \quad 1\mathbf{x} = \mathbf{x}

Examples: Rn\mathbb{R}^n; all m×nm \times n matrices Rm×n\mathbb{R}^{m \times n}; polynomials of degree at most 3; all grayscale 28×2828 \times 28 images (adding two images pixel by pixel gives an image). The full axiomatic treatment is in Vector Spaces.

Subspaces

A subset U⊆VU \subseteq V is a subspace when it contains 0\mathbf{0} and is closed under addition and scaling: x,y∈U⇒λx+ψy∈U\mathbf{x}, \mathbf{y} \in U \Rightarrow \lambda\mathbf{x} + \psi\mathbf{y} \in U. Geometrically, subspaces of R3\mathbb{R}^3 are the origin, lines and planes through the origin, and R3\mathbb{R}^3 itself.

Two subspaces appear in every ML problem with a data matrix AA:

Subspace Definition Meaning
column space (image) Im⁡(A)\operatorname{Im}(A) all AxA\mathbf{x} every output a linear model can produce
null space (kernel) ker⁡(A)\ker(A) all x\mathbf{x} with Ax=0A\mathbf{x} = \mathbf{0} parameter changes that leave every output unchanged
python · try it yourself
import numpy as np

A = np.array([[1.0, 2.0, 3.0],
              [2.0, 4.0, 6.0],
              [1.0, 0.0, 1.0]])
# two vectors in the null space
n1 = np.array([-1.0, -1.0, 1.0])
print("A n1 =", A @ n1)
# closure: any combination stays in the null space
x = 2.5 * n1 - 4.0 * n1
print("A (combination) =", A @ x)
# the image: every output A x has y2 = 2 y1
rng = np.random.default_rng(0)
for _ in range(3):
    y = A @ rng.normal(size=3)
    print("output", np.round(y, 3), " y2 - 2 y1 =", round(float(y[1] - 2 * y[0]), 10))
A n1 = [0. 0. 0.]
A (combination) = [0. 0. 0.]
output [1.783 3.566 0.766]  y2 - 2 y1 = 0.0
output [0.118 0.237 0.466]  y2 - 2 y1 = 0.0
output [1.087 2.174 0.6  ]  y2 - 2 y1 = 0.0

The image here is a plane in R3\mathbb{R}^3 (all outputs satisfy y2=2y1y_2 = 2y_1), so no input can produce, say, (1,0,0)(1, 0, 0).

Not every set is a subspace

The solution set of Ax=bA\mathbf{x} = \mathbf{b} with b≠0\mathbf{b} \ne \mathbf{0} is not a subspace: it does not contain 0\mathbf{0}, and the sum of two solutions solves Ax=2bA\mathbf{x} = 2\mathbf{b}. It is an affine subspace, a shifted copy of the null space. Likewise, the set of probability vectors (non-negative, summing to 1) is not a subspace, because scaling by −1-1 leaves it.

python · try it yourself
import numpy as np

A = np.array([[1.0, 1.0]])
b = np.array([2.0])
s1, s2 = np.array([2.0, 0.0]), np.array([0.5, 1.5])     # both solve x1 + x2 = 2
print("A s1 =", A @ s1, " A s2 =", A @ s2, " A (s1 + s2) =", A @ (s1 + s2))
print("contains zero?", np.allclose(A @ np.zeros(2), b))
A s1 = [2.]  A s2 = [2.]  A (s1 + s2) = [4.]
contains zero? False

In software

  • Word embeddings live in Rd\mathbb{R}^{d} and analogies are vector arithmetic (king−man+woman\text{king} - \text{man} + \text{woman}), which only makes sense because embeddings form a vector space.
  • A linear layer with weight matrix WW can only output vectors in Im⁡(W)\operatorname{Im}(W); a bottleneck layer of width kk restricts the next layer's inputs to a subspace of dimension at most kk.
  • Low-rank adapters (LoRA) restrict weight updates to a small subspace on purpose.

Exercises

Exercise 1: Subspace or not?

Which are subspaces of R2\mathbb{R}^2? (a) the line y=3xy = 3x; (b) the line y=3x+1y = 3x + 1; (c) the first quadrant x,y≥0x, y \ge 0.

Answer

Only (a).

Solution

(a) contains 0\mathbf{0} and is closed: if y1=3x1y_1 = 3x_1 and y2=3x2y_2 = 3x_2 then λy1+ψy2=3(λx1+ψx2)\lambda y_1 + \psi y_2 = 3(\lambda x_1 + \psi x_2). (b) misses 0\mathbf{0}. (c) contains (1,1)(1, 1) but not −1⋅(1,1)-1 \cdot (1, 1).

Exercise 2: Polynomials

Is the set of polynomials pp of degree at most 2 with p(1)=0p(1) = 0 a subspace?

Answer

Yes.

Solution

The zero polynomial has p(1)=0p(1) = 0. If p(1)=q(1)=0p(1) = q(1) = 0, then (λp+ψq)(1)=λ⋅0+ψ⋅0=0(\lambda p + \psi q)(1) = \lambda \cdot 0 + \psi \cdot 0 = 0, and the degree stays at most 2. (With the condition p(1)=5p(1) = 5 it would fail.)

Exercise 3: Find a null vector (code)

Find a nonzero vector in the null space of [110011]\begin{bmatrix} 1 & 1 & 0 \\ 0 & 1 & 1 \end{bmatrix} and confirm it.

Answer

(1,−1,1)(1, -1, 1) (or any multiple).

Solution

From the equations x1=−x2x_1 = -x_2 and x3=−x2x_3 = -x_2; take x2=−1x_2 = -1.

python · try it yourself
import numpy as np
A = np.array([[1.0, 1, 0], [0, 1, 1]])
print(A @ np.array([1.0, -1, 1]))
[0. 0.]

Test yourself

Which of these is always a subspace of Rn\mathbb{R}^n?

  1. The solution set of Ax=bA\mathbf{x} = \mathbf{b} with b≠0\mathbf{b} \ne \mathbf{0}
  2. The null space of AA
  3. The unit sphere
  4. The set of vectors with positive entries

Every subspace must contain…

  1. the vector of all ones
  2. the zero vector
  3. a unit vector
  4. at least two vectors

The column space of a weight matrix WW is…

  1. the set of inputs that give zero output
  2. the set of all outputs WxW\mathbf{x} the layer can produce
  3. the diagonal of WW
  4. the rows of WW