Basis and Dimension
âšī¸ Why It Matters
Basis and dimension are foundational concepts in linear algebra that determine how we represent, compress, and transform data. Every data point in machine learning exists in a high-dimensional vector space, and choosing the right basis â the right coordinate system â can reveal hidden structure, reduce computational cost, and improve model performance. PCA finds the basis of maximum variance; Fourier transforms use frequency basis to decompose signals; wavelet basis enables multi-resolution analysis. Understanding basis and dimension is essential for feature engineering, dimensionality reduction, and efficient computation in AI/ML pipelines.
Vector Space Axioms
DfVector Space
A vector space over a field (typically or ) is a set together with two operations â vector addition () and scalar multiplication () â satisfying the following eight axioms for all and all :
- Closure under addition:
- Commutativity:
- Associativity of addition:
- Additive identity: There exists such that
- Additive inverse: For each , there exists such that
- Closure under scalar multiplication:
- Distributivity: and
- Compatibility of scalar multiplication: , and
đExamples of Vector Spaces
: The set of all -tuples of real numbers with componentwise addition and scalar multiplication. This is the canonical example.
Polynomials of degree : The set with polynomial addition and scalar multiplication.
Matrices: The set of all real matrices with matrix addition and scalar multiplication.
Function spaces: The set of all continuous functions with pointwise addition and scalar multiplication: , .
đNon-Example
The set (first quadrant) is not a vector space because it is not closed under additive inverses â e.g., but .
Subspace
DfSubspace
A subspace of a vector space is a non-empty subset that is itself a vector space under the same operations. Equivalently, is a subspace if and only if:
- (contains the zero vector)
- If , then (closed under addition)
- If and , then (closed under scalar multiplication)
đExamples of Subspaces
Line through the origin: for any fixed .
The set of symmetric matrices: is a subspace of .
The set of solutions to : The null space is always a subspace of .
đNon-Subspace
The set is not a subspace because (since ).
Linear Combinations
DfLinear Combination
A linear combination of vectors is any expression of the form:
where are scalars (called weights or coefficients).
đExample
Let , .
Then is a linear combination of and .
The vector cannot be written as a linear combination of and because they live in , not .
âšī¸ Geometric Intuition
In , the set of all linear combinations of two non-parallel vectors is the entire plane. If the vectors are parallel (scalar multiples of each other), their linear combinations form only a line through the origin.
Span
DfSpan
The span of a set of vectors is the set of all linear combinations:
The span is always a subspace â it is the smallest subspace containing all the given vectors.
đFinding the Span
Let , .
is the plane in passing through the origin and containing both vectors. Since and are not parallel, this is a 2-dimensional subspace of .
đSpan of Standard Basis
in is all of , since any vector .
Linear Independence
DfLinear Independence
A set of vectors is linearly independent if the only solution to:
is .
If a non-trivial solution exists (some ), the set is linearly dependent.
ThTest for Linear Independence
A set of vectors in is linearly independent if and only if the matrix has a pivot in every column (i.e., ).
đLinearly Independent
is linearly independent because:
implies .
đLinearly Dependent
is linearly dependent because , so with non-zero coefficients.
ThKey Properties
- Any set containing the zero vector is linearly dependent.
- A set of vectors in with is always linearly dependent.
- A set with one vector is linearly independent if and only if that vector is non-zero.
Basis
DfBasis
A basis for a vector space is a set of vectors that is:
- Linearly independent, and
- Spans (i.e., )
In other words, a basis is a minimal spanning set and a maximal linearly independent set.
đStandard Basis
For , the standard basis is:
For : , .
Every vector can be uniquely written as .
đNon-Standard Basis for R2
The vectors and also form a basis for because:
- They are linearly independent (not scalar multiples).
- They span : .
â ī¸ Uniqueness
A basis is not unique â any vector space has infinitely many bases. However, all bases for a given vector space have the same number of vectors (this is the dimension).
Dimension
DfDimension
The dimension of a vector space , denoted , is the number of vectors in any basis for .
ThDimension Theorems
- .
- If has a basis with vectors, then every basis for has exactly vectors.
- If , then any set of more than vectors in is linearly dependent.
- If , then any set of fewer than vectors cannot span .
- If , then any linearly independent set of vectors is automatically a basis.
- If , then any spanning set of vectors is automatically a basis.
đDimension Examples
- . Standard basis: .
- (polynomials of degree ). Basis: .
- . Basis: .
- The subspace in has dimension 2 (a plane through the origin).
Change of Basis
DfChange of Basis
Let and be two bases for . The change-of-basis matrix from to is:
Then for any vector :
đChange of Basis Example
Let and (standard basis).
To find , express each vector of in the standard basis:
For in the standard basis:
Verification: â
import numpy as np
# Change of basis
B = np.array([[1, 1], [1, -1]]) # New basis vectors as columns
B_inv = np.linalg.inv(B)
# Vector in standard basis
x_standard = np.array([3, 1])
# Coordinates in basis B
x_in_B = B_inv @ x_standard
print(f"Coordinates in new basis: {x_in_B}") # [2. 1.]
# Verify: reconstruct from new coordinates
x_reconstructed = B @ x_in_B
print(f"Reconstructed: {x_reconstructed}") # [3. 1.]
Rank
DfRank
The rank of a matrix , denoted , is the dimension of the column space (equivalently, the row space):
It equals the number of pivot positions in the row echelon form of .
ThRank-Nullity Theorem
For an matrix :
where is the dimension of the null space.
đComputing Rank
Let .
Row reduce:
Two pivots, so . By rank-nullity: .
Null Space
DfNull Space
The null space (or kernel) of an matrix is the set of all solutions to :
The null space is always a subspace of .
đFinding the Null Space
Let .
Solve :
- Row 1:
- Row 2:
- is free.
The null space is a line through the origin in with dimension 1 (nullity = 1).
import numpy as np
from scipy.linalg import null_space
A = np.array([[1, 2, 0],
[0, 0, 1],
[0, 0, 0]])
ns = null_space(A)
print("Null space basis:")
print(ns)
# [[-0.89442719]
# [ 0.4472136 ]
# [ 0. ]]
Column Space
DfColumn Space
The column space (or range) of an matrix is the span of its columns:
The column space is a subspace of .
ThConsistency of Linear Systems
The system has a solution if and only if .
đColumn Space and Ax = b
Let . The column space is the plane in .
-
: Is in ? Yes, since . Solution: .
-
: Is in ? No, since . No solution exists.
âšī¸ Row Space vs Column Space
- The row space of is the column space of : .
- â row rank always equals column rank.
- The null space is orthogonal to the row space: .
Python Implementation
import numpy as np
from scipy.linalg import null_space, orth
def analyze_vector_space(A):
"""Comprehensive analysis of a matrix's vector space properties."""
m, n = A.shape
print(f"Matrix A ({m}x{n}):")
print(A)
print()
# Rank
rank = np.linalg.matrix_rank(A)
print(f"Rank: {rank}")
# Nullity
nullity = n - rank
print(f"Nullity: {nullity}")
print(f"Rank + Nullity = {rank} + {nullity} = {rank + nullity} = n â")
# Null space
ns = null_space(A)
print(f"\nNull space basis ({nullity} vectors):")
print(ns) if ns.size > 0 else print(" {0}")
# Column space
cs = orth(A)
print(f"\nColumn space basis ({rank} vectors):")
print(cs)
# Check if Ax = b has solutions
b = np.array([1, 2, 3])
try:
x, residuals, rank_check, sv = np.linalg.lstsq(A, b, rcond=None)
if np.allclose(A @ x, b):
print(f"\nAx = b has solution: x = {x}")
else:
print(f"\nAx = b has no solution (b not in column space)")
except:
print(f"\nAx = b has no solution")
return rank, nullity
# Example
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
rank, nullity = analyze_vector_space(A)
print("\n" + "="*50)
# Finding a basis for the column space
def column_space_basis(A):
"""Find a basis for the column space using row reduction."""
# Row reduce and find pivot columns
U = np.array(A, dtype=float)
pivot_cols = []
row, col = 0, 0
while row < U.shape[0] and col < U.shape[1]:
# Find pivot
max_row = np.argmax(np.abs(U[row:, col])) + row
if U[max_row, col] == 0:
col += 1
continue
# Swap rows
U[[row, max_row]] = U[[max_row, row]]
pivot_cols.append(col)
# Eliminate below
for i in range(row + 1, U.shape[0]):
if U[i, col] != 0:
U[i] -= U[i, col] / U[row, col] * U[row]
row += 1
col += 1
return A[:, pivot_cols], pivot_cols
basis, pivots = column_space_basis(A)
print(f"Pivot columns: {pivots}")
print(f"Column space basis:")
print(basis)
# Linear independence check
def check_linear_independence(vectors):
"""Check if a set of vectors is linearly independent."""
if vectors.shape[1] == 0:
return True
rank = np.linalg.matrix_rank(vectors)
return rank == vectors.shape[1]
# Examples
v1 = np.array([[1], [0], [0]])
v2 = np.array([[0], [1], [0]])
v3 = np.array([[1], [1], [0]])
v4 = np.array([[1], [1], [1]])
print("\n{[1,0,0], [0,1,0]}:", check_linear_independence(np.hstack([v1, v2])))
print("{[1,0,0], [0,1,0], [1,1,0]}:", check_linear_independence(np.hstack([v1, v2, v3])))
print("{[1,0,0], [0,1,0], [1,1,1]}:", check_linear_independence(np.hstack([v1, v2, v4])))
# Change of basis
def change_of_basis(x_standard, P):
"""Change coordinates from standard basis to basis P."""
P_inv = np.linalg.inv(P)
return P_inv @ x_standard
# Vector in standard basis
x = np.array([3, 1])
# New basis: rotated 45 degrees
theta = np.pi / 4
P = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
x_new = change_of_basis(x, P)
print(f"\nVector {x} in standard basis")
print(f"Coordinates in rotated basis: {x_new}")
print(f"Verification: {P @ x_new}")
Applications in AI/ML
Dimensionality Reduction
- PCA (Principal Component Analysis): Finds the orthonormal basis of maximum variance. The first principal components form a basis for the -dimensional subspace that best approximates the data in the least-squares sense.
- SVD (Singular Value Decomposition): Provides optimal low-rank approximation. Truncating to singular values gives the best rank- approximation.
Feature Spaces
- Kernel methods: Map data to high-dimensional feature spaces where linear separation is possible. The basis of this feature space determines the kernel function.
- Word embeddings: Words are represented as vectors in a learned basis. The dimension of the embedding space (e.g., 300 for Word2Vec) is the number of basis vectors.
Signal Processing
- Fourier transform: Decomposes signals into frequency basis vectors.
- Wavelet transform: Multi-resolution basis for time-frequency analysis.
- Compressed sensing: Exploits sparsity in a given basis for signal recovery from few measurements.
Neural Networks
- Weight matrices: Each layer's weight matrix transforms from one basis (input features) to another (hidden features). Learning is finding optimal bases.
- Batch normalization: Normalizes activations to have zero mean and unit variance, effectively choosing a convenient basis for each layer.
# PCA as change of basis
from sklearn.decomposition import PCA
import numpy as np
# Generate sample data
np.random.seed(42)
n_samples = 200
X = np.random.randn(n_samples, 2) @ np.array([[3, 1], [1, 2]])
# PCA finds the optimal basis
pca = PCA(n_components=2)
X_transformed = pca.fit_transform(X)
print("Original data shape:", X.shape)
print("Transformed data shape:", X_transformed.shape)
print("Explained variance ratio:", pca.explained_variance_ratio_)
print("Principal components (new basis):")
print(pca.components_) # These are the basis vectors
# The principal components form an orthonormal basis
dot_product = np.dot(pca.components_[0], pca.components_[1])
print(f"Dot product of components (should be 0): {dot_product:.10f}")
# Reconstruction: X â X_transformed @ pca.components_
X_reconstructed = X_transformed @ pca.components_ + pca.mean_
reconstruction_error = np.mean((X - X_reconstructed) ** 2)
print(f"Reconstruction MSE: {reconstruction_error:.6f}")
Common Mistakes
| Mistake | Correction |
|---|---|
| Confusing basis with spanning set | A basis must be both spanning AND linearly independent |
| Assuming the standard basis is the only basis | Any linearly independent set of vectors in is a basis |
| Forgetting the zero vector is always dependent | Any set containing is linearly dependent |
| Using when has new basis as rows | Use columns: |
| Confusing column space with row space | , (different ambient spaces!) |
| Assuming rank = number of rows | Rank = number of pivots, not rows; a tall matrix can have rank < rows |
| Forgetting rank-nullity theorem | Always: (number of columns) |
| Confusing linear independence with orthogonality | Orthogonal non-zero vectors are independent, but independent vectors need not be orthogonal |
| Assuming span of vectors in has dimension | If vectors are dependent, |
| Not checking if before solving | First verify is in the column space; otherwise no solution exists |
Interview Questions
Q1: What is the difference between a basis and a spanning set?
A spanning set generates the entire space but may contain redundant vectors. A basis is a spanning set with no redundancy â it is linearly independent. Every spanning set can be reduced to a basis by removing dependent vectors.
Q2: How do you find a basis for the null space of a matrix?
Solve via row reduction. Express the solution in parametric vector form. The vectors multiplying each free variable form a basis for the null space.
Q3: Why is the dimension of equal to ?
Because any basis for must have exactly vectors. The standard basis has vectors, and by the dimension theorem, all bases have the same size.
Q4: What is the relationship between rank, column space, and null space?
- = number of pivots.
- = number of free variables.
- By rank-nullity: (columns).
- The column space tells you which allow solutions to .
- The null space tells you the solutions when they exist (particular + null space).
Q5: Can a matrix have the same row space and column space?
Yes. Example: (identity matrix). Row space = column space = . But generally, row space and column space are in different ambient spaces unless .
Q6: How does change of basis relate to diagonalization?
If is diagonalizable, where is diagonal and 's columns are eigenvectors. This means: in the eigenbasis, acts as scaling by eigenvalues. Change of basis to the eigenbasis simplifies matrix operations from to .
Q7: What is the rank of the product ?
. Equality holds when the column space of intersects the null space of only at .
Practice Problems
đProblem 1
Find a basis and the dimension of the subspace .
đĄSolution
The equation gives . So:
These two vectors are linearly independent (not scalar multiples), so they form a basis.
.
đProblem 2
Determine if is a basis for .
đĄSolution
Form the matrix and row reduce:
Only 2 pivots, so . The vectors are not a basis for .
They are linearly dependent and span only a 2-dimensional subspace (a plane).
đProblem 3
Find a basis for the column space of and determine the dimension.
đĄSolution
Row reduce :
Pivot columns are columns 1 and 2. The corresponding columns of the original matrix form a basis:
.
đProblem 4
If is a matrix with rank 2, what is the dimension of the null space?
đĄSolution
By rank-nullity: .
.
The null space is a 1-dimensional subspace of (a line through the origin).
đProblem 5
Find a change-of-basis matrix from to the standard basis, and use it to find the coordinates of in basis .
đĄSolution
The change-of-basis matrix from to standard is:
To find : solve :
Verification: â
Quick Reference
| Concept | Definition | Key Formula |
|---|---|---|
| Linear combination | Scalars multiply vectors, then add | |
| Span | Set of all linear combinations | |
| Linear independence | Only trivial solution to | Row reduce; pivot in every column |
| Basis | Linearly independent spanning set | Minimal spanning set = maximal independent set |
| Dimension | Number of vectors in any basis | |
| Rank | Dimension of column space | pivots |
| Null space | Solutions to | ; subspace of |
| Column space | Span of columns of | ; subspace of |
| Rank-nullity | Fundamental theorem | |
| Change of basis |
Cross-References
- Previous topic: 019 - Linear Algebra Span
- Next topic: 021 - Linear Algebra Eigenvalues
- Related concepts:
- Linear Transformations â basis determines matrix representation
- Matrix Multiplication â change of basis is matrix multiplication
- Determinant â is invertible iff iff columns form a basis
- Orthogonal Basis â orthonormal basis simplifies computations
- SVD â provides optimal basis for low-rank approximation