Mathematical Foundations for Machine Learning
The Art of Machine Learning: A Practitioner's Guide to Classical Algorithms
Chapter 1: Mathematical Foundations for Machine Learning
The elegant dance between mathematics and machine learning forms the backbone of every algorithm, every prediction, and every decision that these computational systems make. To understand machine learning deeply—not merely as a collection of techniques to be applied but as a coherent discipline with deep theoretical foundations—one must first establish a solid command of the mathematical principles that underlie it. This chapter bridges the gap between general mathematical knowledge and the specific mathematical toolkit required for meaningful engagement with classical machine learning algorithms. The three pillars we examine here—linear algebra, probability theory, and optimization theory—each play distinct yet interconnected roles in the architecture of machine learning. Linear algebra provides the language for representing data and transformations in high-dimensional spaces. Probability theory offers the framework for reasoning under uncertainty, enabling us to make predictions and quantify confidence. Optimization theory supplies the methodology for finding the best solutions among candidate options, which lies at the heart of every learning algorithm. Together, these mathematical disciplines create the scaffolding upon which the entire edifice of machine learning rests.
The prerequisites for this chapter are intentionally modest: a solid grasp of calculus (including differentiation and integration of single-variable functions), basic familiarity with sets and functions, and some exposure to elementary probability concepts. However, the depth of treatment is substantial, reflecting our commitment to providing not just procedural knowledge but genuine understanding of why mathematical tools work as they do. Throughout this chapter, we shall develop geometric intuitions alongside algebraic manipulations, recognizing that the most powerful mathematical understanding comes from seeing concepts from multiple perspectives. The examples we choose are deliberately rooted in machine learning applications, demonstrating the immediate relevance of each mathematical concept before we proceed to more advanced topics in subsequent chapters.
Linear Algebra Fundamentals
Linear algebra occupies a unique position in the mathematics of machine learning. It provides the fundamental language for representing data, performing transformations, and capturing relationships between variables. When we speak of a dataset with thousands of features, we are implicitly working in a high-dimensional vector space—a concept made precise and manipulable through linear algebra. Understanding vectors and matrices is not merely helpful for machine learning; it is absolutely essential.
Vectors and Vector Spaces
A vector can be understood from multiple perspectives, each illuminating different aspects of its nature and utility. Geometrically, a vector represents a direction and magnitude in space—a directed arrow from one point to another. Algebraically, a vector is an ordered collection of numbers, typically written as a column or row. In machine learning, we most commonly work with vectors as column arrays of real numbers, such as $\mathbf{x} = [x_1, x_2, \ldots, x_n]^T$, where $n$ denotes the dimensionality of the space.
The set of all $n$-dimensional real vectors forms the vector space $\mathbb{R}^n$, which satisfies remarkable algebraic properties that make it the natural habitat for data analysis. These properties include closure under addition (the sum of any two vectors remains in the space), closure under scalar multiplication (multiplying a vector by a real number preserves membership in the space), the existence of a zero vector, and the existence of additive inverses. These seemingly abstract properties have concrete implications: they guarantee that the operations we perform on data vectors remain within the realm of tractable computation.
The dot product (or inner product) between two vectors $\mathbf{a}$ and $\mathbf{b}$ in $\mathbb{R}^n$ is defined as:
$$\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = \mathbf{a}^T \mathbf{b}$$
The dot product enables the measurement of angle between vectors through the relationship:
$$\cos \theta = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\| \|\mathbf{b}\|}$$
where $\|\mathbf{a}\| = \sqrt{\mathbf{a} \cdot \mathbf{a}}$ denotes the Euclidean norm (length) of the vector. When $\cos \theta = 0$, the vectors are orthogonal—they are perpendicular in the geometric sense. When $\cos \theta = 1$, the vectors point in the same direction (are linearly dependent in a positive sense). This geometric interpretation proves invaluable in machine learning contexts: we often want to measure similarity between data points, detect uncorrelated features, or project data onto subspaces that capture essential structure.
Consider a concrete machine learning example: document classification. We might represent each document as a vector in a high-dimensional space where each dimension corresponds to a word in our vocabulary. If a document contains word $i$ frequently, the $i$-th entry of its vector is large; if the word is absent, that entry might be zero or near-zero. The similarity between two documents—measured by the cosine of the angle between their vectors—tells us how semantically related they are. Documents with similar word distributions will have vectors pointing in roughly the same direction, yielding high cosine similarity. This observation underlies the effectiveness of methods like latent semantic analysis and document clustering algorithms.
The concept of linear independence proves crucial for understanding the dimensionality of vector spaces. A set of vectors $\{\mathbf{v}_1, \mathbf{v}_2, \ldots, \mathbf{v}_k\}$ is linearly independent if no vector in the set can be expressed as a linear combination of the others. If one vector can be written as a linear combination of the others, it adds no new "direction" to the span of the set. The maximum size of a linearly independent set within a space defines the dimension of that space. In machine learning, we frequently seek to determine the intrinsic dimensionality of our data—whether our thousands of features can be effectively summarized by a much smaller number of fundamental dimensions.
Matrices and Matrix Operations
If vectors are one-dimensional arrays of numbers, matrices are two-dimensional arrays. An $m \times n$ matrix $\mathbf{A}$ contains $m$ rows and $n$ columns, with entry $a_{ij}$ denoting the element in row $i$ and column $j$:
$$\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}$$
Matrices arise naturally in machine learning for multiple purposes. A data matrix $\mathbf{X} \in \mathbb{R}^{m \times n}$ might contain $m$ examples (rows) and $n$ features (columns), where each row is a data point and each column is a feature across all examples. A weight matrix $\mathbf{W} \in \mathbb{R}^{n \times k}$ might represent the parameters of a linear transformation mapping from an $n$-dimensional input space to a $k$-dimensional output space.
The multiplication of matrices $\mathbf{A} \in \mathbb{R}^{m \times n}$ and $\mathbf{B} \in \mathbb{R}^{n \times p}$ produces a matrix $\mathbf{C} = \mathbf{A}\mathbf{B} \in \mathbb{R}^{m \times p}$ with entries:
$$c_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj}$$
This operation can be understood as a collection of dot products: the $(i,j)$-th entry of the product is the dot product of the $i$-th row of $\mathbf{A}$ with the $j$-th column of $\mathbf{B}$. Matrix multiplication is not commutative—generally $\mathbf{A}\mathbf{B} \neq \mathbf{B}\mathbf{A}$—but it is associative and distributive.
The transpose of a matr