Vectors and Matrices in Machine Learning

Vectors and matrices are the fundamental data structures of machine learning: a vector is an ordered list of numbers encoding a single example or parameter set, and a matrix is a rectangular array that stores datasets, linear transformations, and learnable weights. Key result in words: nearly every ML model — from linear regression to deep neural networks — is built by repeatedly applying matrix-vector multiplication, transposition, and inner products, making linear algebra the computational backbone of the field. Where it applies: feature representation, regression, classification, dimensionality reduction, neural networks, recommendation systems. Who/when: linear algebra formalized in the 19th–20th centuries (Cayley, Sylvester); adapted to statistical learning from the 1950s onward and to modern deep learning from the 2010s.

A vector is an ordered -tuple of real numbers, geometrically a point or arrow in -dimensional space; a matrix is a 2D array of numbers that represents either a collection of vectors or a linear map between vector spaces. In machine learning, datasets are stored as matrices (rows = examples, columns = features), model parameters are vectors or matrices, and the act of computing a prediction is almost always a matrix-vector product followed by a nonlinearity. This matters because casting learning as linear algebra unlocks decades of numerical methods, allows massive parallelism on GPUs, and makes gradients computable through clean matrix calculus. A surprising fact: even though neural networks are famously "nonlinear," more than 99% of their floating-point operations are pure linear algebra (matrix multiplies), with nonlinearity supplied by cheap element-wise functions in between.

The Core Insight

Data, parameters, and transformations all live in the same algebraic language. A learning algorithm reduces to: stack your data into a matrix , store your model in a parameter vector/matrix , compute predictions via , measure error, and adjust using gradients that are themselves matrix expressions. Because matrix multiplication composes linear maps, stacking layers is just multiplying matrices, and this single operation — generalized inner product — is the atom from which most of ML is assembled.

Formal Statement

Vector

A vector in is an ordered tuple

1
⟳ is the -fold Cartesian product of with itself, equipped with componentwise addition and scalar multiplication.

Here is the dimension (number of features), is the -th component, and by convention vectors are columns. Each represents one example, e.g. a flower described by 4 measurements gives .

Matrix

A matrix is a rectangular array

2
⟳a matrix is a function , , displayed as a grid.

with rows and columns; is the entry in row , column . In a design matrix , each of the rows is one example and each of the columns is one feature.

Core operations

The inner (dot) product, matrix-vector product, and matrix-matrix product are

3
⟳each output entry is the dot product of a row of the left operand with a column of the right operand; sizes must conform .

The transpose has entries . The Euclidean norm is .

Linear maps are matrices

Every linear map (satisfying ) is represented by a unique matrix such that

4
⟳write , apply linearity, and read off ; the columns of are the images of the standard basis vectors.

Why It Holds / Derivation

Matrix multiplication = composition of linear maps

We show that multiplying matrices computes the composition of the maps they represent — the fact that makes stacking neural-network layers equivalent to multiplying weight matrices.

  1. Let have matrix and have matrix . The composition is linear (composition of linear maps is linear).
  2. By [4], has a matrix whose -th column is .
  3. Now is the -th column of , call it , with entries . Then has -th entry .
  4. Therefore , which is exactly the definition in [3]. Hence and .
  5. Consequence: a two-layer linear network collapses to a single matrix — which is why nonlinearities between layers are essential.

Normal equations for linear regression

We derive the closed-form least-squares solution, the canonical ML use of these objects.

  1. Minimize the squared error where , .
  2. Expand: .
  3. Take the gradient with respect to : .
  4. Set to : (the normal equations).
  5. If is invertible, .
5
⟳differentiate the quadratic form using and , with symmetric.

Arthur Cayley | 1821–1895

Cayley introduced matrices as standalone algebraic objects in his 1858 Memoir on the Theory of Matrices, defining matrix multiplication so that it mirrored the composition of linear substitutions — the exact property exploited in deep-network layer stacking. He proved the Cayley–Hamilton theorem (a matrix satisfies its own characteristic equation), establishing matrices as a self-contained algebra rather than mere notation. His collaborator James Joseph Sylvester coined the word "matrix" (Latin for "womb") in 1850. Together they transformed array notation into the algebraic machinery that, a century later, would underpin statistical learning and neural computation.

Building Intuition

Think of a vector as an arrow from the origin: its components are how far you walk along each coordinate axis. A matrix is a machine that bends space — it takes every arrow and maps it to a new arrow, possibly rotating, stretching, shearing, or projecting. Crucially, this machine is fully described by what it does to the basis arrows (the columns of the matrix). In ML, the dot product measures alignment: it is large and positive when the data point points in the same direction as the weight vector , which is exactly how a linear classifier decides "yes" vs "no." A whole dataset is a cloud of points (a matrix), and learning is the act of finding directions in which the cloud separates, aligns, or varies most.

A 2×2 matrix transforming the unit square: the columns of A are the images of the basis vectors e₁ and e₂.

unit square e₁ e₂ A·(unit square) A·e₁ (col 1) A·e₂ (col 2) area scaling = |det A|

Worked Examples

Predicting house price with a dot product

Given: a weight vector (price per sq-ft, per mile from city, per bedroom), bias , and a house (sq-ft, miles, bedrooms). Steps:
  1. Inner product (using [3]):
.
  1. .
  2. Add bias: .
Answer:
The single dot product is the entire prediction of a linear regression model.

Batch predictions and least squares

Given: design matrix and targets
(first column = bias, second = a feature). Fit via [5]. Steps:
  1. , with .
  2. .
  3. → compute: .
  4. .
Answer:
The fitted line is (dimensionless prediction matching best in squared error).

Common Misconceptions

Matrix multiplication is NOT commutative

In general — and often one product is undefined even when the other exists (shapes must conform). Order encodes which transformation happens first. A frequent ML bug is writing when you mean , flipping the meaning of rows and columns. Also: (the order reverses), not .

Stacking linear layers gains nothing without nonlinearity

Consider two "layers" then . One might hope a deeper network is more expressive. But for a single matrix . So any number of stacked linear layers is equivalent to one linear layer — it represents exactly the same set of functions. This is precisely why neural networks insert nonlinear activations (ReLU, sigmoid) between matrix multiplies; without them, depth is an illusion.

Singular $X^\top X$ breaks the normal equations

Equation [5] requires to be invertible, which fails when features are collinear or when (more features than examples). In practice one adds regularization: (ridge regression), guaranteeing invertibility for any , or uses the pseudoinverse / gradient descent. Never blindly invert a matrix in code — solve the linear system instead, for numerical stability.

Connections

Vectors and matrices are the substrate for nearly every neighbouring topic. Eigenvalues and Eigenvectors reveal the invariant directions of a matrix and power Principal Component Analysis for dimensionality reduction. The Singular Value Decomposition factorizes any matrix and underlies recommendation systems and low-rank approximation. Gradient Descent navigates parameter space using gradient vectors, and Matrix Calculus gives the rules for differentiating the vector/matrix expressions appearing in Backpropagation. The geometry of Inner Product Spaces formalizes the dot products used by Support Vector Machines and kernel methods, while Norms and Distances define the loss functions being minimized.

Real-World Applications

  1. Linear & logistic regression — predictions are (a matrix-vector product); used in credit scoring, epidemiology, and A/B testing.
  2. Deep neural networks — each dense layer computes ; GPUs/TPUs exist primarily to run these matrix multiplies fast (e.g. GPT-class transformers).
  3. Principal Component Analysis (PCA) — the covariance matrix's eigenvectors compress high-dimensional data (face recognition, gene expression).
  4. Recommendation systems — Netflix/Spotify factor a huge user-item matrix to predict ratings (matrix factorization, SVD).
  5. Computer graphics & vision — image rotations, projections, and convolutions are matrix operations; convolutional layers reduce to matrix multiplies via im2col.
  6. Word embeddings & transformers — words become vectors; attention computes , three matrix products at the heart of large language models.
  7. PageRank — the web's link structure is a giant matrix whose dominant eigenvector ranks pages (the original Google algorithm).

Historical Development

  • 1843 — Hamilton invents quaternions, an early non-commutative system foreshadowing matrix algebra.
  • 1850 — Sylvester coins the term "matrix."
  • 1858 — Cayley's Memoir on the Theory of Matrices defines matrix multiplication and the algebra of matrices.
  • 1888 — Peano gives the modern axiomatic definition of a vector space.
  • 1900s — Hilbert and others develop infinite-dimensional vector (Hilbert) spaces, foundational for functional analysis and later kernel methods.
  • 1936 — Eckart and Young prove the low-rank approximation theorem via SVD, basis of modern data compression.
  • 1958 — Rosenblatt's perceptron computes , framing learning as finding a weight vector.
  • 1986 — Rumelhart, Hinton & Williams popularize backpropagation, expressing learning as matrix-calculus gradient flow.
  • 2012 — AlexNet exploits GPU matrix multiplication to win ImageNet, igniting the deep-learning era; matrix multiply becomes the dominant compute primitive of AI.

Subtleties & Limits

The relentless growth of model sizes makes the cost of matrix multiplication a central bottleneck and open research frontier. The asymptotic complexity of multiplying two matrices is still unknown: the naïve algorithm is , Strassen (1969) achieved , and the current record (Williams et al.) is below , yet the true exponent remains undetermined. Practically, these fast algorithms are numerically delicate and rarely used in ML, where hardware-optimized kernels dominate. Parallel open questions include: how to compress weight matrices via low-rank or sparse structure without losing accuracy, how to multiply huge matrices in low precision (8-bit, 4-bit) stably, and how to schedule matrix operations across thousands of accelerators. The mathematics of which matrices ML actually needs (highly structured, low effective rank) versus the worst case studied in theory is itself an active area.

See Also

Eigenvalues and Eigenvectors · Singular Value Decomposition · Principal Component Analysis · Matrix Calculus · Gradient Descent · Inner Product Spaces · Norms and Distances · Backpropagation · Support Vector Machines · Systems of Linear Equations

Before this

Where this leads