Principal Component Analysis
Learning Objectives
After reading this page, you should be able to:
- Project a vector onto a subspace.
- Prove that minimizing reconstruction error is equivalent to maximizing the variance of the reconstructions.
- Perform all the steps of principal component analysis on a given dataset.
- Explain whether PCA is suitable for solving a given problem based on the properties of PCA.
1 Introduction
Real-world data often has many features, and such high-dimensional data is hard to visualize, analyze, or model. We would like to find a lower-dimensional representation of the data that still preserves most of its information. This process is called dimensionality reduction. Reducing the number of dimensions lets us visualize complex data, extract meaningful patterns or features, and compress data with minimal information loss.
Several techniques can reduce the dimensionality of data, and each one finds a lower-dimensional representation in a different way. Autoencoders use a neural network to compress each data point into a short code and then reconstruct the input from that code. Matrix factorization approximates a large data matrix as the product of two smaller matrices, which is useful in applications such as recommender systems. On this page, we focus on principal component analysis (PCA), one of the most widely used techniques. PCA finds the few directions along which the data spreads out the most and describes each data point using only those directions. PCA and matrix factorization are linear techniques, while autoencoders are non-linear and can capture curved structure in the data, such as the Swiss roll in Figure 1 below.
2 Projecting Data onto a Subspace
In many data sets, the features are related to each other, so the data does not spread out equally in every direction. Imagine a cloud of points in three dimensions shaped like a slightly thick pancake. The points spread out a lot along two directions, the length and width of the pancake, but barely at all along the third direction, its thickness. If we flatten the pancake onto a plane, each point moves only a little, so we lose very little information. In return, we need only two numbers instead of three to describe each point. Flattening the data like this is called a projection onto a subspace. You can think of a projection as looking at the data from a particular direction, like a shadow cast onto a wall. The key question is which subspace to project onto. A good choice keeps the directions along which the data spreads out the most, and PCA gives us a way to find them.
2.1 Projecting a Two-Dimensional Vector onto a One-Dimensional Subspace
Let’s start with the simplest case. Suppose that we have some two-dimensional data and want to project each data point onto a one-dimensional subspace.
We start by defining a one-dimensional subspace.
Definition: A one-dimensional subspace \(S\) is defined by a unit vector \(\mathbf{u}\) (a vector of length one). The subspace \(S\) is the line through the origin along \(\mathbf{u}\), and it contains every scalar multiple of \(\mathbf{u}\), \[ S = \{\, z\mathbf{u} \mid z \in \mathbb{R} \,\}. \]
You can think of \(z\) as a coordinate along the line. Its magnitude tells us how far the point is from the origin, and its sign tells us the direction.
For example, we can define the one-dimensional subspace for the line \(x_2 = x_1\) in Figure 3 as
\[ S = \left\{\, \begin{bmatrix} \tfrac{z}{\sqrt{2}} \\ \tfrac{z}{\sqrt{2}} \end{bmatrix} \,\middle|\, z \in \mathbb{R} \,\right\}, \]
with the unit vector
\[ \mathbf{u} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}. \]
Question: Consider the line through the origin and the point
\[ \begin{bmatrix} 3 & 4 \end{bmatrix}^\top. \]
Write down the definition of this line as a one-dimensional subspace \(S\).
Answer: The subspace is
\[ S = \left\{\, \begin{bmatrix} \tfrac{3z}{5} & \tfrac{4z}{5} \end{bmatrix}^\top \,\middle|\, z \in \mathbb{R} \,\right\}, \]
with the unit vector
\[ \mathbf{u} = \begin{bmatrix} \tfrac{3}{5} & \tfrac{4}{5} \end{bmatrix}^\top. \]
To find \(\mathbf{u}\), we divide the vector from the origin to the given point by its length, which is
\[ \sqrt{3^2 + 4^2} = 5. \]
The unit vector pointing the other way, \[ -\mathbf{u} = \begin{bmatrix} -\tfrac{3}{5} & -\tfrac{4}{5} \end{bmatrix}^\top, \] defines the same subspace, so it is also a correct answer.
Next, let’s define a point in the two-dimensional data space and project this point onto the one-dimensional subspace.
Definition: Let \(\mathbf{x} \in \mathbb{R}^2\) be a data point and \(S\) be a one-dimensional subspace of \(\mathbb{R}^2\). The orthogonal projection of \(\mathbf{x}\) onto \(S\), written \(\hat{\mathbf{x}}\), is the unique point in \(S\) with the smallest Euclidean distance to \(\mathbf{x}\),
\[ \hat{\mathbf{x}} = \operatorname*{arg\,min}_{\mathbf{v} \in S} \, \|\mathbf{x} - \mathbf{v}\|. \]
See Figure 4 for an example of projecting the vector \(\begin{bmatrix} 2 & 1 \end{bmatrix}^\top\) onto the subspace \(S\) to get the vector \(\begin{bmatrix} \tfrac{3}{2} & \tfrac{3}{2} \end{bmatrix}^\top\). Intuitively, the projection \(\hat{\mathbf{x}}\) is the shadow that \(\mathbf{x}\) casts on \(S\) when light shines onto the line from a direction perpendicular to it.
Although the definition above provides a nice intuition, it doesn’t tell us how to derive the projection. For this case, a simple derivation gives us the following formulas for computing the projection.
Definition: Let \(\mathbf{x} \in \mathbb{R}^2\) and let \(S\) be the one-dimensional subspace defined by the unit vector \(\mathbf{u}\).
The representation or code of \(\mathbf{x}\) in \(S\) is the scalar \[ z = \mathbf{x}^\top \mathbf{u}. \]
The reconstruction or orthogonal projection of \(\mathbf{x}\) is the vector \[ \hat{\mathbf{x}} = z\mathbf{u} = (\mathbf{x}^\top \mathbf{u})\,\mathbf{u}. \]
Intuitively, \(z\) tells us the (signed) magnitude of the projection, and the unit vector \(\mathbf{u}\) gives its direction. The names describe what each quantity does. One number \(z\) is enough to represent \(\mathbf{x}\) within \(S\), and \(\hat{\mathbf{x}}\) is our best attempt to reconstruct \(\mathbf{x}\) from that \(z\).
Let’s apply these formulas to the vector \(\begin{bmatrix} 2 & 1 \end{bmatrix}^\top\) in Figure 4 and verify its projection. The data point is \[ \mathbf{x} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}. \] Its representation is \[ z = \mathbf{x}^\top \mathbf{u} = \begin{bmatrix} 2 & 1 \end{bmatrix} \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ \tfrac{1}{\sqrt{2}} \end{bmatrix} = \tfrac{2}{\sqrt{2}} + \tfrac{1}{\sqrt{2}} = \tfrac{3}{\sqrt{2}}, \]
and its reconstruction is
\[ \hat{\mathbf{x}} = \tfrac{3}{\sqrt{2}} \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ \tfrac{1}{\sqrt{2}} \end{bmatrix} = \begin{bmatrix} \tfrac{3}{2} \\ \tfrac{3}{2} \end{bmatrix}. \]
Question: Consider the one-dimensional subspace \(S\) for the line through the origin and the point \(\mathbf{a}\). Determine the orthogonal projection of the data point \(\mathbf{x}\) onto \(S\). Then, on Figure 5, draw the subspace \(S\) and the projection \(\hat{\mathbf{x}}\).
\[\mathbf{a} = \begin{bmatrix} 2 \\ -1 \end{bmatrix} \qquad \qquad \mathbf{x} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}\]
Answer: The vector \(\mathbf{a}\) points along the line, but it is not a unit vector because its length is
\[ \|\mathbf{a}\| = \sqrt{2^2 + (-1)^2} = \sqrt{5}. \]
Dividing \(\mathbf{a}\) by its length gives the unit vector
\[ \mathbf{u} = \frac{\mathbf{a}}{\|\mathbf{a}\|} = \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ -\tfrac{1}{\sqrt{5}} \end{bmatrix}, \]
so the subspace is
\[ S = \left\{\, z \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ -\tfrac{1}{\sqrt{5}} \end{bmatrix} \,\middle|\, z \in \mathbb{R} \,\right\}. \]
The representation of \(\mathbf{x}\) is
\[ z = \mathbf{x}^\top \mathbf{u} = \begin{bmatrix} 2 & 1 \end{bmatrix} \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ -\tfrac{1}{\sqrt{5}} \end{bmatrix} = \tfrac{4}{\sqrt{5}} - \tfrac{1}{\sqrt{5}} = \tfrac{3}{\sqrt{5}}, \]
and its reconstruction is
\[ \hat{\mathbf{x}} = \tfrac{3}{\sqrt{5}} \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ -\tfrac{1}{\sqrt{5}} \end{bmatrix} = \begin{bmatrix} \tfrac{6}{5} \\ -\tfrac{3}{5} \end{bmatrix}. \]
Figure 6 shows the subspace \(S\) and the projection \(\hat{\mathbf{x}}\).
An alternative solution is to choose \(-\mathbf{u}\) as the unit vector, and you will get the representation \(z = -\tfrac{3}{\sqrt{5}}\) and the same reconstruction \(\hat{\mathbf{x}}\).
2.2 Projecting a D-Dimensional Vector onto a K-Dimensional Subspace
Now, let’s generalize our formulas to a larger number of dimensions. Consider data in \(D\)-dimensional space and a subspace with \(K\) dimensions, where \(K < D\). We start by defining a \(K\)-dimensional subspace.
Definition: A \(K\)-dimensional subspace \(S\) of \(\mathbb{R}^D\) is defined by \(K\) vectors \(\mathbf{u}_1, \mathbf{u}_2, \dots, \mathbf{u}_K \in \mathbb{R}^D\) that are orthonormal, meaning each vector has length one and every pair of distinct vectors is perpendicular.
The subspace \(S\) contains every linear combination of these vectors, \[ S = \{\, z_1 \mathbf{u}_1 + z_2 \mathbf{u}_2 + \cdots + z_K \mathbf{u}_K \mid z_1, z_2, \dots, z_K \in \mathbb{R} \,\}. \]
We call \(\mathbf{u}_1, \mathbf{u}_2, \dots, \mathbf{u}_K\) an orthonormal basis for \(S\).
This definition generalizes the one-dimensional case. The unit vectors define \(K\) perpendicular directions. They act like the axes of a coordinate system for \(S\), just like the two axes for the plane. The values \(z_1, z_2, \dots, z_K\) define the signed magnitudes along these directions.
In practice, we often stack the basis vectors as the columns of a matrix \[ \mathbf{U} = \begin{bmatrix} \mathbf{u}_1 & \mathbf{u}_2 & \cdots & \mathbf{u}_K \end{bmatrix} \in \mathbb{R}^{D \times K}, \] and collect the coordinates into a vector \[ \mathbf{z} = \begin{bmatrix} z_1 & z_2 & \cdots & z_K \end{bmatrix}^\top \in \mathbb{R}^K. \] With this notation, every vector in \(S\) can be expressed as the following matrix-vector product, \[ \mathbf{U}\mathbf{z} = z_1 \mathbf{u}_1 + z_2 \mathbf{u}_2 + \cdots + z_K \mathbf{u}_K. \]
Now we can project a data point \(\mathbf{x} \in \mathbb{R}^D\) onto \(S\). The formulas look just like the one-dimensional case, with the matrix \(\mathbf{U}\) taking the place of the unit vector \(\mathbf{u}\).
Definition: Let \(\mathbf{x} \in \mathbb{R}^D\) and let \(S\) be the \(K\)-dimensional subspace whose orthonormal basis vectors are the columns of \(\mathbf{U} \in \mathbb{R}^{D \times K}\).
The representation or code of \(\mathbf{x}\) in \(S\) is the vector \[ \mathbf{z} = \mathbf{U}^\top \mathbf{x} \in \mathbb{R}^K. \]
The reconstruction or orthogonal projection of \(\mathbf{x}\) onto \(S\) is the vector \[ \hat{\mathbf{x}} = \mathbf{U}\mathbf{z} = \mathbf{U}\mathbf{U}^\top \mathbf{x} \in \mathbb{R}^D. \]
Let’s apply these formulas to project a three-dimensional data point onto a two-dimensional subspace. Consider \[ \mathbf{x} = \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix}, \qquad \mathbf{U} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 \\ 0 & 1 \\ \tfrac{1}{\sqrt{2}} & 0 \end{bmatrix}. \] The two columns of \(\mathbf{U}\) each have length one and are perpendicular to each other, so they form an orthonormal basis for a plane \(S\) in \(\mathbb{R}^3\). The representation of \(\mathbf{x}\) is \[ \mathbf{z} = \mathbf{U}^\top \mathbf{x} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 & \tfrac{1}{\sqrt{2}} \\ 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix} = \begin{bmatrix} \tfrac{5}{\sqrt{2}} \\ 1 \end{bmatrix}, \] and its reconstruction is \[ \hat{\mathbf{x}} = \mathbf{U}\mathbf{z} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 \\ 0 & 1 \\ \tfrac{1}{\sqrt{2}} & 0 \end{bmatrix} \begin{bmatrix} \tfrac{5}{\sqrt{2}} \\ 1 \end{bmatrix} = \begin{bmatrix} \tfrac{5}{2} \\ 1 \\ \tfrac{5}{2} \end{bmatrix}. \] The code \(\mathbf{z}\) describes \(\mathbf{x}\) with two numbers instead of three.
Question: Project the same data point \(\mathbf{x}\) onto a different two-dimensional subspace, where \[ \mathbf{x} = \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix}, \qquad \mathbf{U} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 \\ -\tfrac{1}{\sqrt{2}} & 0 \\ 0 & 1 \end{bmatrix}. \] Compute the representation \(\mathbf{z}\) and the reconstruction \(\hat{\mathbf{x}}\).
Answer: The representation of \(\mathbf{x}\) is \[ \mathbf{z} = \mathbf{U}^\top \mathbf{x} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & -\tfrac{1}{\sqrt{2}} & 0 \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 3 \end{bmatrix}, \] and its reconstruction is \[ \hat{\mathbf{x}} = \mathbf{U}\mathbf{z} = \begin{bmatrix} \tfrac{1}{\sqrt{2}} & 0 \\ -\tfrac{1}{\sqrt{2}} & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 3 \end{bmatrix} = \begin{bmatrix} \tfrac{1}{2} \\ -\tfrac{1}{2} \\ 3 \end{bmatrix}. \]
3 Learning a Good Subspace
Figure 8 shows the same data point \(\mathbf{x}\) projected onto two different one-dimensional subspaces. Which subspace is better?
(A)
(B)
Intuitively, the projection in (A) gives a much better approximation of \(\mathbf{x}\). The subspace in (A) is closer to the original vector in direction. As a result, the projection in (A) is much closer to the original vector in both direction and magnitude, giving us a reconstruction that keeps more information about the original vector.
Recall that our overall goal for PCA is to represent data in a lower-dimensional space while losing as little information as possible. Intuitively, we can measure the amount of information in a set of data points through its variance or spread. We can capture this goal in two concrete ways. One, we can choose a subspace that minimizes the information lost by the projection. Two, we can choose a subspace that preserves as much variance in the data as possible. These two objectives look very different on the surface. As you will see below, they are in fact mathematically equivalent.
Let’s make the two objectives more concrete. Suppose that we have a data set of \(N\) data points \(\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \dots, \mathbf{x}^{(N)} \in \mathbb{R}^D,\) and we want to project each data point onto a \(K\)-dimensional subspace \(S\), where \(K < D\). The subspace \(S\) is defined by the orthonormal basis \(\mathbf{U} = \begin{bmatrix} \mathbf{u}_1 & \mathbf{u}_2 & \cdots & \mathbf{u}_K \end{bmatrix} \in \mathbb{R}^{D \times K}.\)
Before writing down the objectives, we need to take care of one detail. We care about preserving the variance of the data, which measures how the data spreads out around its mean. However, our subspaces always pass through the origin, so the reconstructions end up capturing the spread around the origin instead, which is not what we want. To fix this, we shift the data so that its mean is at the origin. Let \(\boldsymbol{\mu}\) be the mean of the data points, \[ \boldsymbol{\mu} = \frac{1}{N} \sum_{i=1}^N \mathbf{x}^{(i)}. \]
We centre the data by subtracting the mean from each data point,
\[ \tilde{\mathbf{x}}^{(i)} = \mathbf{x}^{(i)} - \boldsymbol{\mu}, \quad \text{for } i = 1, 2, \dots, N. \]
Centring shifts the whole data set without changing its shape, so the spread of the data stays the same. From now on, we will work with the centred data points \(\tilde{\mathbf{x}}^{(i)}\) and simply call them \(\mathbf{x}^{(i)}\). This means that the data points now have mean \(\boldsymbol{\mu} = \mathbf{0}\). Later, we will see that centring the data is the first step of PCA.
For our first objective, we want to minimize the information lost through the projection. We can quantify the information loss by the reconstruction error, defined as the squared distance between each original vector and its projection. A small reconstruction error means that the subspace \(S\) preserves \(\mathbf{x}\) well.
Definition: Let \(\hat{\mathbf{x}}\) be the reconstruction of a data point \(\mathbf{x}\) in a subspace \(S\). The reconstruction error of \(\mathbf{x}\) is \[ \|\mathbf{x} - \hat{\mathbf{x}}\|^2. \]
Given this definition, we can aim to choose the subspace that minimizes the average reconstruction error over the data set, \[ \min_{\mathbf{U} \,:\, \mathbf{U}^\top \mathbf{U} = \mathbf{I}} \; \frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2. \] The constraint \(\mathbf{U}^\top \mathbf{U} = \mathbf{I}\) says that the columns of \(\mathbf{U}\) are orthonormal.
For the second objective, we want to choose the subspace that maximizes the variance of the reconstructions, \[ \max_{\mathbf{U} \,:\, \mathbf{U}^\top \mathbf{U} = \mathbf{I}} \; \frac{1}{N} \sum_{i=1}^N \|\hat{\mathbf{x}}^{(i)} - \boldsymbol{\mu}\|^2. \] Since the data is centred, \(\boldsymbol{\mu} = \mathbf{0}\), and the reconstructions also have mean \(\mathbf{0}\). We keep \(\boldsymbol{\mu}\) in the formula to make it clear that this quantity is a variance.
To show that these two objectives are equivalent, we will show that the sum of the average reconstruction error and the variance of the reconstructions is equal to the variance of the original data points.
Theorem: Let \(\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \dots, \mathbf{x}^{(N)} \in \mathbb{R}^D\) be centred data points with mean \(\boldsymbol{\mu} = \mathbf{0}\), and let \(\mathbf{U} \in \mathbb{R}^{D \times K}\) have orthonormal columns. For each \(i\), let \(\hat{\mathbf{x}}^{(i)} = \mathbf{U}\mathbf{U}^\top \mathbf{x}^{(i)}\) be the reconstruction of \(\mathbf{x}^{(i)}\). Then \[ \underbrace{\frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2}_{\text{average reconstruction error}} + \underbrace{\frac{1}{N} \sum_{i=1}^N \|\hat{\mathbf{x}}^{(i)} - \boldsymbol{\mu}\|^2}_{\text{variance of the reconstructions}} = \underbrace{\frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \boldsymbol{\mu}\|^2}_{\text{variance of the data}}. \]
The variance of the original data points does not depend on \(\mathbf{U}\), so it is a constant. As a result, minimizing the average reconstruction error over \(\mathbf{U}\) is equivalent to maximizing the variance of the reconstructions over \(\mathbf{U}\).
Question: Prove the theorem above.
Answer: The proof has three steps.
Step 1: The error vector is orthogonal to the reconstruction.
Since \(\hat{\mathbf{x}}^{(i)}\) is the orthogonal projection of \(\mathbf{x}^{(i)}\) onto \(S\), the error vector \(\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\) is orthogonal to every vector in \(S\). The reconstruction \(\hat{\mathbf{x}}^{(i)}\) lies in \(S\). Therefore, \(\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\) is orthogonal to \(\hat{\mathbf{x}}^{(i)}\), as Figure 9 illustrates.
We can also verify this algebraically. Let’s drop the superscript for simplicity. We need to show that \[ \hat{\mathbf{x}}^\top (\mathbf{x} - \hat{\mathbf{x}}) = 0. \] First, we substitute the formula \(\hat{\mathbf{x}} = \mathbf{U}\mathbf{U}^\top \mathbf{x}\), \[ \hat{\mathbf{x}}^\top (\mathbf{x} - \hat{\mathbf{x}}) = (\mathbf{U}\mathbf{U}^\top \mathbf{x})^\top (\mathbf{x} - \mathbf{U}\mathbf{U}^\top \mathbf{x}). \] Next, we apply the identity for the transpose of a product, \((\mathbf{A}\mathbf{B})^\top = \mathbf{B}^\top \mathbf{A}^\top\), which gives \[ (\mathbf{U}\mathbf{U}^\top \mathbf{x})^\top = \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top. \] Substituting this and expanding, we get \[ \begin{aligned} \hat{\mathbf{x}}^\top (\mathbf{x} - \hat{\mathbf{x}}) &= \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top (\mathbf{x} - \mathbf{U}\mathbf{U}^\top \mathbf{x}) \\ &= \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x} - \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x}. \end{aligned} \] Using \(\mathbf{U}^\top \mathbf{U} = \mathbf{I}\), we get \[ \begin{aligned} \hat{\mathbf{x}}^\top (\mathbf{x} - \hat{\mathbf{x}}) &= \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x} - \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x} \\ &= \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x} - \mathbf{x}^\top \mathbf{U}\mathbf{U}^\top \mathbf{x} \\ &= 0. \end{aligned} \]
Step 2: Apply the Pythagorean theorem.
By Step 1, the vectors \(\hat{\mathbf{x}}^{(i)}\) and \(\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\) form the two legs of a right triangle whose hypotenuse is \(\mathbf{x}^{(i)}\). The Pythagorean theorem gives \[ \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2 + \|\hat{\mathbf{x}}^{(i)}\|^2 = \|\mathbf{x}^{(i)}\|^2. \]
Step 3: Average over the data points.
Averaging this equation over \(i = 1, 2, \dots, N\) gives \[ \frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2 + \frac{1}{N} \sum_{i=1}^N \|\hat{\mathbf{x}}^{(i)}\|^2 = \frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)}\|^2. \] Since the data is centred, this equation is exactly the theorem.
4 The PCA Algorithm
Now we are ready to put everything together. Given \(N\) data points \(\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \dots, \mathbf{x}^{(N)} \in \mathbb{R}^D\), PCA computes a representation \(\mathbf{z}^{(i)} \in \mathbb{R}^K\) for each data point, where \(K < D\), that preserves as much of the variance in the data as possible. PCA has five steps.
- Centre the data.
- Compute the covariance matrix.
- Find the principal components.
- Choose the top \(K\) principal components.
- Compute the representations.
Let’s go through these steps one at a time.
Step 1: Centre the Data
PCA projects the data onto a subspace, and every subspace passes through the origin. As we discussed in the previous section, we want the subspace to capture how the data spreads out around its mean, so we first shift the data so that its mean is at the origin. We compute the mean of the data points, \[ \boldsymbol{\mu} = \frac{1}{N} \sum_{i=1}^N \mathbf{x}^{(i)}, \] and subtract it from every data point, \[ \tilde{\mathbf{x}}^{(i)} = \mathbf{x}^{(i)} - \boldsymbol{\mu}, \quad \text{for } i = 1, 2, \dots, N. \]
After centring, we may also want to standardize the data, as we did for \(k\)-nearest neighbours, by dividing each feature by its standard deviation so that every feature has variance one. PCA is sensitive to scale, because it looks for the directions with the most variance, and a feature with a much larger variance than the others will dominate the principal components. Whether this is a problem depends on the data, so there are a few things to consider when deciding whether to standardize. Standardizing makes sense when the features have different units, such as age in years and income in dollars, or very different scales, such as one feature between 0 and 1 and another between 0 and 10,000. In these cases, standardizing gives every feature an equal influence. On the other hand, if all the features share the same units and have comparable scales, or if the differences in variance are meaningful and should influence the results, it is better to leave the variances as they are.
Step 2: Compute the Covariance Matrix
Next, we compute the covariance matrix of the data, \[ \boldsymbol{\Sigma} = \frac{1}{N} \sum_{i=1}^N \left(\mathbf{x}^{(i)} - \boldsymbol{\mu}\right)\left(\mathbf{x}^{(i)} - \boldsymbol{\mu}\right)^\top = \frac{1}{N} \sum_{i=1}^N \tilde{\mathbf{x}}^{(i)} \left(\tilde{\mathbf{x}}^{(i)}\right)^\top. \]
The covariance matrix is a \(D \times D\) matrix. The entry in row \(j\) and column \(k\) is \[ \Sigma_{jk} = \frac{1}{N} \sum_{i=1}^N \tilde{x}^{(i)}_j \, \tilde{x}^{(i)}_k. \] Each diagonal entry \(\Sigma_{jj}\) is the variance of feature \(j\), which tells us how much the data spreads out along the \(j\)-th axis. Each off-diagonal entry \(\Sigma_{jk}\) is the covariance between features \(j\) and \(k\), which tells us how the two features vary together. The covariance is positive when the two features tend to increase together, negative when one tends to decrease as the other increases, and close to zero when they have no linear relationship.
The covariance matrix tells us more than how the data spreads out along the axes. Together, its entries describe how the data spreads out in every direction. If we pick any direction and project the data onto it, we can compute the variance of the projected data from \(\boldsymbol{\Sigma}\) alone, without going back to the data. This is exactly what we need, since PCA looks for the directions with the most variance.
Step 3: Find the Principal Components
The covariance matrix \(\boldsymbol{\Sigma}\) is symmetric and positive semi-definite. By the spectral theorem, \(\boldsymbol{\Sigma}\) can be decomposed as \[ \boldsymbol{\Sigma} = \mathbf{Q} \boldsymbol{\Lambda} \mathbf{Q}^\top, \] where \[ \mathbf{Q} = \begin{bmatrix} \mathbf{u}_1 & \mathbf{u}_2 & \cdots & \mathbf{u}_D \end{bmatrix}, \qquad \boldsymbol{\Lambda} = \operatorname{diag}(\lambda_1, \lambda_2, \dots, \lambda_D). \] The columns of \(\mathbf{Q}\) are the eigenvectors of \(\boldsymbol{\Sigma}\), and the diagonal entries of \(\boldsymbol{\Lambda}\) are the corresponding eigenvalues, so \(\boldsymbol{\Sigma}\mathbf{u}_j = \lambda_j \mathbf{u}_j\) for each \(j\). The eigenvalues are real and non-negative, and the eigenvectors are orthonormal. We call the eigenvectors \(\mathbf{u}_1, \mathbf{u}_2, \dots, \mathbf{u}_D\) the principal components of the data.
Step 4: Choose the Top \(K\) Principal Components
We sort the eigenvalues in descending order, \[ \lambda_1 \geq \lambda_2 \geq \cdots \geq \lambda_D, \] and keep the eigenvectors for the \(K\) largest eigenvalues as the columns of \[ \mathbf{U}_K = \begin{bmatrix} \mathbf{u}_1 & \mathbf{u}_2 & \cdots & \mathbf{u}_K \end{bmatrix} \in \mathbb{R}^{D \times K}. \] These \(K\) eigenvectors are orthonormal, so they form an orthonormal basis for a \(K\)-dimensional subspace. Based on the derivation in the previous section, this subspace maximizes the variance of the reconstructions and minimizes the average reconstruction error.
Step 5: Compute the Representations
We project each centred data point onto the subspace spanned by \(\mathbf{U}_K\). Its representation is \[ \mathbf{z}^{(i)} = \mathbf{U}_K^\top \tilde{\mathbf{x}}^{(i)} \in \mathbb{R}^K. \] The \(j\)-th entry of \(\mathbf{z}^{(i)}\) is the coordinate of the data point along the \(j\)-th principal component. The vectors \(\mathbf{z}^{(1)}, \mathbf{z}^{(2)}, \dots, \mathbf{z}^{(N)}\) are the lower-dimensional representation of the data that PCA produces.
We can also compute the representations of all the data points at once. Let \(\tilde{\mathbf{X}} \in \mathbb{R}^{N \times D}\) be the matrix whose \(i\)-th row is the transpose of the centred data point \(\tilde{\mathbf{x}}^{(i)}\). Then we can compute the representations as the matrix \(\mathbf{Z} \in \mathbb{R}^{N \times K}\), where its \(i\)-th row is the transpose of the representation \(\mathbf{z}^{(i)}\), \[ \mathbf{Z} = \tilde{\mathbf{X}} \mathbf{U}_K = \begin{bmatrix} (\mathbf{z}^{(1)})^\top \\ (\mathbf{z}^{(2)})^\top \\ \vdots \\ (\mathbf{z}^{(N)})^\top \end{bmatrix}. \]
5 Evaluating PCA and Choosing \(K\)
After running PCA, we usually want to know how much information the representations keep, which can help us choose a good value of \(K\). We can measure this using two approaches: computing the fraction of variance explained and computing the reconstruction error.
The fraction of variance explained can be computed directly from the eigenvalues of the covariance matrix. Each eigenvalue \(\lambda_j\) gives the variance of the data along the principal component \(\mathbf{u}_j\). So we can compute the fraction of the total variance captured by the top \(K\) principal components.
Definition: The fraction of variance explained by the top \(K\) principal components is \[ \frac{\lambda_1 + \lambda_2 + \cdots + \lambda_K}{\lambda_1 + \lambda_2 + \cdots + \lambda_D}. \]
The fraction is always between \(0\) and \(1\). A value close to \(1\) means the representations keep almost all of the variance in the data. This gives us a common way to choose \(K\). For example, we can pick the smallest \(K\) whose principal components explain at least 95% of the variance.
The second approach is to reconstruct the data using the representations in the original space and compute the reconstruction error, which measures how much the reconstructions differ from the original data points. To reconstruct a data point, we multiply its representation by \(\mathbf{U}_K\) to map it back to the \(D\)-dimensional space, and then add the mean back to undo the centring. The reconstruction of each data point is \[ \hat{\mathbf{x}}^{(i)} = \mathbf{U}_K \mathbf{z}^{(i)} + \boldsymbol{\mu} \in \mathbb{R}^D. \] Then, we can compute the average reconstruction error over the data set as follows. \[ \frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2. \]
The two approaches measure the same thing from opposite sides. The eigenvalues we keep add up to the variance that the representations capture, and the eigenvalues we discard add up to the average reconstruction error, \[ \frac{1}{N} \sum_{i=1}^N \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2 = \lambda_{K+1} + \lambda_{K+2} + \cdots + \lambda_D. \] So a large fraction of variance explained means a small reconstruction error. Calculating the fraction of variance explained is usually the easier way to choose \(K\), since it only requires the eigenvalues.
6 A Numerical Example of PCA
Let’s run PCA on a small data set with \(N = 5\) data points and \(D = 2\) features. The data points are
\[ \mathbf{x}^{(1)} = \begin{bmatrix} 1 \\ 3 \end{bmatrix}, \qquad \mathbf{x}^{(2)} = \begin{bmatrix} 7 \\ 1 \end{bmatrix}, \qquad \mathbf{x}^{(3)} = \begin{bmatrix} 10 \\ 5 \end{bmatrix}, \qquad \mathbf{x}^{(4)} = \begin{bmatrix} 15 \\ 5 \end{bmatrix}, \qquad \mathbf{x}^{(5)} = \begin{bmatrix} 17 \\ 11 \end{bmatrix}. \]
Figure 10 shows the data. The points lie close to a slanted line, so we expect a one-dimensional subspace to describe them well. The figure is interactive. Press Next and Previous to step through PCA on this data set and see what each step does to the data as you read the calculations below.
Let’s reduce the data to \(K = 1\) dimension by following the steps of PCA.
Step 1: Centre the Data
The mean of the data points is \[ \boldsymbol{\mu} = \frac{1}{5} \left( \begin{bmatrix} 1 \\ 3 \end{bmatrix} + \begin{bmatrix} 7 \\ 1 \end{bmatrix} + \begin{bmatrix} 10 \\ 5 \end{bmatrix} + \begin{bmatrix} 15 \\ 5 \end{bmatrix} + \begin{bmatrix} 17 \\ 11 \end{bmatrix} \right) = \frac{1}{5} \begin{bmatrix} 50 \\ 25 \end{bmatrix} = \begin{bmatrix} 10 \\ 5 \end{bmatrix}. \] Subtracting the mean from every data point gives the centred data points \[ \tilde{\mathbf{x}}^{(1)} = \begin{bmatrix} -9 \\ -2 \end{bmatrix}, \qquad \tilde{\mathbf{x}}^{(2)} = \begin{bmatrix} -3 \\ -4 \end{bmatrix}, \qquad \tilde{\mathbf{x}}^{(3)} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}, \qquad \tilde{\mathbf{x}}^{(4)} = \begin{bmatrix} 5 \\ 0 \end{bmatrix}, \qquad \tilde{\mathbf{x}}^{(5)} = \begin{bmatrix} 7 \\ 6 \end{bmatrix}. \]
Step 2: Compute the Covariance Matrix
We substitute the centred data points into the formula for the covariance matrix, \[ \begin{aligned} \boldsymbol{\Sigma} &= \frac{1}{5} \sum_{i=1}^{5} \tilde{\mathbf{x}}^{(i)} \left(\tilde{\mathbf{x}}^{(i)}\right)^\top \\ &= \frac{1}{5} \left( \begin{bmatrix} -9 \\ -2 \end{bmatrix} \begin{bmatrix} -9 & -2 \end{bmatrix} + \begin{bmatrix} -3 \\ -4 \end{bmatrix} \begin{bmatrix} -3 & -4 \end{bmatrix} + \begin{bmatrix} 0 \\ 0 \end{bmatrix} \begin{bmatrix} 0 & 0 \end{bmatrix} + \begin{bmatrix} 5 \\ 0 \end{bmatrix} \begin{bmatrix} 5 & 0 \end{bmatrix} + \begin{bmatrix} 7 \\ 6 \end{bmatrix} \begin{bmatrix} 7 & 6 \end{bmatrix} \right) \\ &= \frac{1}{5} \begin{bmatrix} 164 & 72 \\ 72 & 56 \end{bmatrix}. \end{aligned} \] The off-diagonal entries are positive and large, which tells us that the two features tend to increase together.
Step 3: Find the Principal Components
The spectral decomposition of the covariance matrix is \[ \boldsymbol{\Sigma} = \mathbf{Q} \boldsymbol{\Lambda} \mathbf{Q}^\top, \] where \[ \mathbf{Q} = \begin{bmatrix} \tfrac{2}{\sqrt{5}} & \tfrac{1}{\sqrt{5}} \\ \tfrac{1}{\sqrt{5}} & -\tfrac{2}{\sqrt{5}} \end{bmatrix}, \qquad \boldsymbol{\Lambda} = \begin{bmatrix} 40 & 0 \\ 0 & 4 \end{bmatrix}. \] The eigenvalues are \[ \lambda_1 = 40, \qquad \lambda_2 = 4, \] and the columns of \(\mathbf{Q}\) are the principal components \[ \mathbf{u}_1 = \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ \tfrac{1}{\sqrt{5}} \end{bmatrix}, \qquad \mathbf{u}_2 = \begin{bmatrix} \tfrac{1}{\sqrt{5}} \\ -\tfrac{2}{\sqrt{5}} \end{bmatrix}. \] The first principal component points along the line \(x_2 = \tfrac{1}{2} x_1\), which is the direction in which the data spreads out the most.
Step 4: Choose the Top \(K\) Principal Components
The eigenvalues are already in descending order, \[ \lambda_1 = 40 \geq \lambda_2 = 4. \] Since \(K = 1\), we keep only the first principal component, \[ \mathbf{U}_1 = \mathbf{u}_1 = \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ \tfrac{1}{\sqrt{5}} \end{bmatrix}. \]
Step 5: Compute the Representations
We compute the representations of all the data points at once. The rows of \(\tilde{\mathbf{X}}\) are the centred data points, so \[ \mathbf{Z} = \tilde{\mathbf{X}} \mathbf{U}_1 = \begin{bmatrix} -9 & -2 \\ -3 & -4 \\ 0 & 0 \\ 5 & 0 \\ 7 & 6 \end{bmatrix} \begin{bmatrix} \tfrac{2}{\sqrt{5}} \\ \tfrac{1}{\sqrt{5}} \end{bmatrix} = \begin{bmatrix} -4\sqrt{5} \\ -2\sqrt{5} \\ 0 \\ 2\sqrt{5} \\ 4\sqrt{5} \end{bmatrix}. \] The \(i\)-th row of \(\mathbf{Z}\) is the representation of the \(i\)-th data point. For example, the representation of the first data point is \[ \mathbf{z}^{(1)} = \begin{bmatrix} -4\sqrt{5} \end{bmatrix}. \] PCA describes each two-dimensional data point with one number, its position along the first principal component.
Evaluating PCA
Let’s evaluate how much information the representations keep. The fraction of variance explained by the first principal component is \[ \frac{\lambda_1}{\lambda_1 + \lambda_2} = \frac{40}{40 + 4} = \frac{10}{11} \approx 0.91. \] The first principal component explains about 91% of the variance in the data.
To compute the reconstruction error, we first reconstruct all the data points at once. Let \(\hat{\mathbf{X}}\) be the matrix whose \(i\)-th row is the transpose of the reconstruction \(\hat{\mathbf{x}}^{(i)}\). We multiply \(\mathbf{Z}\) by the transpose of \(\mathbf{U}_1\) to map the representations back to the original space, and then add the mean back to every row, \[ \begin{aligned} \hat{\mathbf{X}} &= \mathbf{Z} \mathbf{U}_1^\top + \begin{bmatrix} \boldsymbol{\mu}^\top \\ \vdots \\ \boldsymbol{\mu}^\top \end{bmatrix} = \begin{bmatrix} -4\sqrt{5} \\ -2\sqrt{5} \\ 0 \\ 2\sqrt{5} \\ 4\sqrt{5} \end{bmatrix} \begin{bmatrix} \tfrac{2}{\sqrt{5}} & \tfrac{1}{\sqrt{5}} \end{bmatrix} + \begin{bmatrix} 10 & 5 \\ 10 & 5 \\ 10 & 5 \\ 10 & 5 \\ 10 & 5 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 6 & 3 \\ 10 & 5 \\ 14 & 7 \\ 18 & 9 \end{bmatrix}. \end{aligned} \] Each row of \(\hat{\mathbf{X}}\) is one reconstruction. For example, the reconstruction of the first data point is \[ \hat{\mathbf{x}}^{(1)} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}. \] All the reconstructions lie on the line \(x_2 = \tfrac{1}{2} x_1\). The average reconstruction error is \[ \begin{aligned} & \frac{1}{5} \sum_{i=1}^5 \|\mathbf{x}^{(i)} - \hat{\mathbf{x}}^{(i)}\|^2 \\ &= \frac{1}{5} \left( \left\| \begin{bmatrix} 1 \\ 3 \end{bmatrix} - \begin{bmatrix} 2 \\ 1 \end{bmatrix} \right\|^2 + \left\| \begin{bmatrix} 7 \\ 1 \end{bmatrix} - \begin{bmatrix} 6 \\ 3 \end{bmatrix} \right\|^2 \right. \\ &\qquad \left. + \left\| \begin{bmatrix} 10 \\ 5 \end{bmatrix} - \begin{bmatrix} 10 \\ 5 \end{bmatrix} \right\|^2 + \left\| \begin{bmatrix} 15 \\ 5 \end{bmatrix} - \begin{bmatrix} 14 \\ 7 \end{bmatrix} \right\|^2 + \left\| \begin{bmatrix} 17 \\ 11 \end{bmatrix} - \begin{bmatrix} 18 \\ 9 \end{bmatrix} \right\|^2 \right) \\ &= 4. \end{aligned} \] As expected, the average reconstruction error equals the eigenvalue we discarded, \(\lambda_2 = 4\).
7 Summary
On this page, we introduced principal component analysis (PCA), a technique that represents high-dimensional data in fewer dimensions while keeping as much of its variance as possible. PCA projects the data onto a lower-dimensional subspace. To run PCA, we centre the data, compute its covariance matrix, find the eigenvectors of the covariance matrix, keep the eigenvectors with the largest eigenvalues, and project the centred data onto the subspace that they span. The eigenvalues also tell us how much of the variance the representations keep, which helps us choose the number of dimensions.
PCA illustrates two of the Fundamental Ideas of machine learning. We framed the search for a good subspace as an optimization problem, either minimizing the reconstruction error or maximizing the variance of the reconstructions, and we showed that the two objectives are equivalent (Fundamental Idea #1: Learning Is Optimization). Unlike many models we have seen, PCA solves this optimization problem exactly through the eigenvectors of the covariance matrix, without gradient descent. PCA is also geometric at its core (Fundamental Idea #4: ML Describes Geometric Processes). It projects data points onto a subspace, measures information loss as squared distances, and uses the spectral decomposition of the covariance matrix, which we first saw with the multivariate Gaussian, to find the directions along which the data spreads out the most.