Essential linear algebra concepts for ML including vectors, matrices, eigenvalues, and their applications in algorithms like PCA, SVD, and neural networks.
Linear algebra is the mathematical backbone of machine learning. Every dataset is a matrix, every model learns weight vectors, and transformations like PCA are eigenvalue decompositions. You don't need a PhD in math — but understanding these core concepts will make you a much better ML practitioner.
Why Linear Algebra Matters in ML
| ML Concept | Linear Algebra Operation |
| Dataset | Matrix (n × d) |
| Features | Vectors in d-dimensional space |
| Linear regression | Solving system of equations |
| PCA | Eigenvalue decomposition |
| Neural networks | Matrix multiplications + nonlinearities |
| Embeddings | Vectors in high-dimensional space |
Vectors
A vector is an ordered list of numbers representing a point or direction in space.
import numpy as np
# Vectors represent data points
feature_vector = np.array([1.5, 2.3, 0.7, 4.1]) # One data sample
print(f"Feature vector: {feature_vector}")
print(f"Dimensions: {feature_vector.shape[0]}")
# Vector operations
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Dot product (used everywhere in ML)
dot = np.dot(a, b) # 1*4 + 2*5 + 3*6 = 32
print(f"Dot product: {dot}")
# Vector norm (magnitude/length)
norm = np.linalg.norm(a)
print(f"L2 Norm of a: {norm:.4f}")
# Unit vector (normalization)
unit_a = a / np.linalg.norm(a)
print(f"Unit vector: {unit_a.round(4)}")
print(f"Its norm: {np.linalg.norm(unit_a):.4f}")
# Cosine similarity (used in NLP, recommendations)
cosine_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(f"Cosine similarity: {cosine_sim:.4f}")
Matrices
A matrix is a 2D array of numbers. In ML, your dataset IS a matrix.
import numpy as np
# Dataset as a matrix: rows = samples, columns = features
X = np.array([
[1.0, 2.0, 3.0], # Sample 1
[4.0, 5.0, 6.0], # Sample 2
[7.0, 8.0, 9.0], # Sample 3
])
print(f"Dataset shape: {X.shape}") # (3 samples, 3 features)
# Matrix multiplication (core of neural networks)
W = np.array([[0.1, 0.2],
[0.3, 0.4],
[0.5, 0.6]])
output = X @ W # (3×3) @ (3×2) = (3×2)
print(f"Linear transformation: {X.shape} @ {W.shape} = {output.shape}")
# Transpose
print(f"X^T shape: {X.T.shape}")
# Inverse (used in solving linear regression analytically)
A = np.array([[2, 1], [5, 3]])
A_inv = np.linalg.inv(A)
print(f"A * A_inv = \n{(A @ A_inv).round(10)}") # Identity matrix
Eigenvalues and Eigenvectors
Eigenvectors are directions that don't change under a transformation — they only get scaled by the eigenvalue. This is the math behind PCA.
import numpy as np
# Covariance matrix of data
np.random.seed(42)
X = np.random.randn(100, 2) @ np.array([[2, 1], [1, 3]])
# Compute covariance matrix
cov_matrix = np.cov(X.T)
print(f"Covariance matrix:\n{cov_matrix.round(3)}")
# Eigendecomposition (this IS what PCA does)
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
print(f"\nEigenvalues: {eigenvalues.round(3)}")
print(f"Eigenvectors:\n{eigenvectors.round(3)}")
print(f"\nVariance explained:")
total = eigenvalues.sum()
for i, ev in enumerate(sorted(eigenvalues, reverse=True)):
print(f" PC{i+1}: {ev/total:.1%}")
Singular Value Decomposition (SVD)
SVD decomposes any matrix into three components. It's used in dimensionality reduction, recommendations, and image compression.
import numpy as np
# SVD: A = U * Σ * V^T
A = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
U, S, Vt = np.linalg.svd(A, full_matrices=False)
print(f"U shape: {U.shape}") # Left singular vectors
print(f"S (singular values): {S.round(3)}") # Singular values
print(f"Vt shape: {Vt.shape}") # Right singular vectors
# Reconstruct with fewer components (dimensionality reduction)
k = 2 # Keep only top 2 components
A_approx = U[:, :k] @ np.diag(S[:k]) @ Vt[:k, :]
error = np.linalg.norm(A - A_approx)
print(f"\nReconstruction error with k={k}: {error:.4f}")
Linear Algebra in Neural Networks
import numpy as np
# A neural network layer IS a matrix multiplication + bias + activation
def neural_layer(X, W, b, activation='relu'):
"""Single neural network layer: z = XW + b, a = activation(z)"""
z = X @ W + b
if activation == 'relu':
return np.maximum(0, z)
elif activation == 'sigmoid':
return 1 / (1 + np.exp(-z))
return z
# Example: 2-layer neural network
X = np.random.randn(10, 4) # 10 samples, 4 features
W1 = np.random.randn(4, 8) * 0.1 # Layer 1: 4 -> 8 neurons
b1 = np.zeros(8)
W2 = np.random.randn(8, 2) * 0.1 # Layer 2: 8 -> 2 outputs
b2 = np.zeros(2)
# Forward pass
hidden = neural_layer(X, W1, b1, 'relu') # (10,4) @ (4,8) = (10,8)
output = neural_layer(hidden, W2, b2, 'sigmoid') # (10,8) @ (8,2) = (10,2)
print(f"Input shape: {X.shape}")
print(f"Hidden shape: {hidden.shape}")
print(f"Output shape: {output.shape}")
Key Concepts Summary
| Concept | ML Application |
|---|
| Dot product | Similarity, projections, linear models |
| Matrix multiply | Neural network layers, transformations |
| Transpose | Gradient computation, covariance |
| Inverse | Analytical solutions (normal equation) |
| Eigendecomposition | PCA, spectral clustering |
| SVD | Dimensionality reduction, recommendations |
| Norms | Regularization (L1, L2), distance |
Interview Questions
- What is the geometric interpretation of a dot product?
The dot product measures the projection of one vector onto another. It equals |a|*|b|*cos(θ), so it's related to the angle between vectors.
- Why is matrix multiplication the core of neural networks?
Each layer computes z = Wx + b, which is a linear transformation that combines inputs using learned weights. Stacking these with nonlinearities creates powerful function approximators.
- What is PCA doing mathematically?
PCA finds the eigenvectors of the covariance matrix and projects data onto the top-k eigenvectors (directions of maximum variance).
- When would you use SVD vs eigendecomposition?
SVD works on any matrix (even non-square), eigendecomposition requires square matrices. SVD is more numerically stable and is preferred in practice.
- What is the rank of a matrix and why does it matter in ML?
Rank is the number of linearly independent rows/columns. Low-rank matrices indicate redundancy in features. Matrix factorization exploits low-rank structure for compression and recommendations.