PCA (Principal Component Analysis) › Example

Michael BrenndoerferJuly 21, 20255 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 48PCA (Principal Component Analysis) › Example
Reading section 5 of 9 · Example · 5 minSection 5 of 9 · 5 min

Book contents

Section 5 of 9 · 5 min read

PCA (Principal Component Analysis) › ExampleOne section · Sections in this chapter5 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through PCA by hand on a tiny dataset. This will make the abstract formulas concrete. We'll calculate each step explicitly so you see exactly how the algorithm works.

Suppose we have four observations of two variables:

X=[12233445]\mathbf{X} = \begin{bmatrix} 1 & 2 \\ 2 & 3 \\ 3 & 4 \\ 4 & 5 \end{bmatrix}

Each row is an observation, each column is a variable. Notice the strong relationship: the second variable is always one more than the first.

Step 1: Mean Centering

PCA requires mean-centered data. First, calculate the mean of each variable:

  • Mean of variable 1: xˉ1=1+2+3+44=2.5\bar{x}_1 = \frac{1+2+3+4}{4} = 2.5
  • Mean of variable 2: xˉ2=2+3+4+54=3.5\bar{x}_2 = \frac{2+3+4+5}{4} = 3.5

Now subtract these means from each observation:

Xcentered=[1−2.52−3.52−2.53−3.53−2.54−3.54−2.55−3.5]=[−1.5−1.5−0.5−0.50.50.51.51.5]\mathbf{X}_{\text{centered}} = \begin{bmatrix} 1-2.5 & 2-3.5 \\ 2-2.5 & 3-3.5 \\ 3-2.5 & 4-3.5 \\ 4-2.5 & 5-3.5 \end{bmatrix} = \begin{bmatrix} -1.5 & -1.5 \\ -0.5 & -0.5 \\ 0.5 & 0.5 \\ 1.5 & 1.5 \end{bmatrix}

Each column now has mean zero. The data is centered at the origin.

Step 2: Calculate the Covariance Matrix

Next, compute the covariance matrix using C=1n−1XcenteredTXcentered\mathbf{C} = \frac{1}{n-1} \mathbf{X}_{\text{centered}}^T \mathbf{X}_{\text{centered}}. With n=4n=4 observations, we divide by n−1=3n-1=3:

C=13[−1.5−0.50.51.5−1.5−0.50.51.5][−1.5−1.5−0.5−0.50.50.51.51.5]\mathbf{C} = \frac{1}{3} \begin{bmatrix} -1.5 & -0.5 & 0.5 & 1.5 \\ -1.5 & -0.5 & 0.5 & 1.5 \end{bmatrix} \begin{bmatrix} -1.5 & -1.5 \\ -0.5 & -0.5 \\ 0.5 & 0.5 \\ 1.5 & 1.5 \end{bmatrix}

Let's calculate element by element:

  • C11=13[(−1.5)2+(−0.5)2+(0.5)2+(1.5)2]=13[2.25+0.25+0.25+2.25]=53≈1.67C_{11} = \frac{1}{3}[(-1.5)^2 + (-0.5)^2 + (0.5)^2 + (1.5)^2] = \frac{1}{3}[2.25 + 0.25 + 0.25 + 2.25] = \frac{5}{3} \approx 1.67

This is the variance of variable 1.

  • C12=C21=13[(−1.5)(−1.5)+(−0.5)(−0.5)+(0.5)(0.5)+(1.5)(1.5)]=13[2.25+0.25+0.25+2.25]=53≈1.67C_{12} = C_{21} = \frac{1}{3}[(-1.5)(-1.5) + (-0.5)(-0.5) + (0.5)(0.5) + (1.5)(1.5)] = \frac{1}{3}[2.25 + 0.25 + 0.25 + 2.25] = \frac{5}{3} \approx 1.67

This is the covariance between variables 1 and 2. It's positive and large, confirming the strong positive relationship we saw.

  • C22=13[(−1.5)2+(−0.5)2+(0.5)2+(1.5)2]=53≈1.67C_{22} = \frac{1}{3}[(-1.5)^2 + (-0.5)^2 + (0.5)^2 + (1.5)^2] = \frac{5}{3} \approx 1.67

This is the variance of variable 2.

The covariance matrix is:

C=[1.671.671.671.67]\mathbf{C} = \begin{bmatrix} 1.67 & 1.67 \\ 1.67 & 1.67 \end{bmatrix}

Notice that the covariance equals the variances. This happens when two variables are perfectly correlated, as ours nearly are.

Step 3: Find Eigenvalues and Eigenvectors

Now we solve the eigenvalue equation Cw=λw\mathbf{C}\mathbf{w} = \lambda\mathbf{w}. Rearranging, we get (C−λI)w=0(\mathbf{C} - \lambda\mathbf{I})\mathbf{w} = 0, where I\mathbf{I} is the identity matrix.

For non-trivial solutions (where w≠0\mathbf{w} \neq 0), the matrix C−λI\mathbf{C} - \lambda\mathbf{I} must be singular, meaning its determinant equals zero. This gives the characteristic equation:

det⁡(C−λI)=0\det(\mathbf{C} - \lambda\mathbf{I}) = 0

Substituting our covariance matrix:

det⁡[1.67−λ1.671.671.67−λ]=0\det\begin{bmatrix} 1.67-\lambda & 1.67 \\ 1.67 & 1.67-\lambda \end{bmatrix} = 0

The determinant of a 2×2 matrix [abcd]\begin{bmatrix} a & b \\ c & d \end{bmatrix} is ad−bcad - bc, so:

(1.67−λ)(1.67−λ)−(1.67)(1.67)=0(1.67-\lambda)(1.67-\lambda) - (1.67)(1.67) = 0 (1.67−λ)2−(1.67)2=0(1.67-\lambda)^2 - (1.67)^2 = 0

This factors as a difference of squares:

[(1.67−λ)−1.67][(1.67−λ)+1.67]=0[(1.67-\lambda) - 1.67][(1.67-\lambda) + 1.67] = 0 (−λ)(3.34−λ)=0(-\lambda)(3.34 - \lambda) = 0

So λ=0\lambda = 0 or λ=3.34\lambda = 3.34. These are our two eigenvalues.

Now find the eigenvector for each eigenvalue.

For λ1=3.34\lambda_1 = 3.34:

Substitute into (C−λI)w=0(\mathbf{C} - \lambda\mathbf{I})\mathbf{w} = 0:

[1.67−3.341.671.671.67−3.34][w1w2]=[00]\begin{bmatrix} 1.67-3.34 & 1.67 \\ 1.67 & 1.67-3.34 \end{bmatrix}\begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} [−1.671.671.67−1.67][w1w2]=[00]\begin{bmatrix} -1.67 & 1.67 \\ 1.67 & -1.67 \end{bmatrix}\begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}

The first row gives −1.67w1+1.67w2=0-1.67w_1 + 1.67w_2 = 0, which simplifies to w1=w2w_1 = w_2. So the eigenvector has equal components. To make it unit length, we need w12+w22=1w_1^2 + w_2^2 = 1. Since w1=w2w_1 = w_2, we have 2w12=12w_1^2 = 1, so w1=12w_1 = \frac{1}{\sqrt{2}}:

w1=[1212]≈[0.7070.707]\mathbf{w}_1 = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} \end{bmatrix} \approx \begin{bmatrix} 0.707 \\ 0.707 \end{bmatrix}

This is the first principal component. It points equally in both variable directions, capturing their shared variation.

For λ2=0\lambda_2 = 0:

[1.671.671.671.67][w1w2]=[00]\begin{bmatrix} 1.67 & 1.67 \\ 1.67 & 1.67 \end{bmatrix}\begin{bmatrix} w_1 \\ w_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}

This gives 1.67w1+1.67w2=01.67w_1 + 1.67w_2 = 0, so w1=−w2w_1 = -w_2. With unit length:

w2=[12−12]≈[0.707−0.707]\mathbf{w}_2 = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} \end{bmatrix} \approx \begin{bmatrix} 0.707 \\ -0.707 \end{bmatrix}

This is the second principal component. It points in the direction perpendicular to the first. Note that λ2=0\lambda_2 = 0 means this direction has zero variance, which makes sense because our data lies along a perfect line.

Step 4: Transform the Data

Finally, project the data onto the principal components using Y=XcenteredW\mathbf{Y} = \mathbf{X}_{\text{centered}}\mathbf{W}:

Y=[−1.5−1.5−0.5−0.50.50.51.51.5][0.7070.7070.707−0.707]\mathbf{Y} = \begin{bmatrix} -1.5 & -1.5 \\ -0.5 & -0.5 \\ 0.5 & 0.5 \\ 1.5 & 1.5 \end{bmatrix} \begin{bmatrix} 0.707 & 0.707 \\ 0.707 & -0.707 \end{bmatrix}

Let's compute a few entries to see the pattern:

  • Y11=(−1.5)(0.707)+(−1.5)(0.707)=−1.061−1.061=−2.121Y_{11} = (-1.5)(0.707) + (-1.5)(0.707) = -1.061 - 1.061 = -2.121
  • Y12=(−1.5)(0.707)+(−1.5)(−0.707)=−1.061+1.061=0Y_{12} = (-1.5)(0.707) + (-1.5)(-0.707) = -1.061 + 1.061 = 0
  • Y21=(−0.5)(0.707)+(−0.5)(0.707)=−0.354−0.354=−0.707Y_{21} = (-0.5)(0.707) + (-0.5)(0.707) = -0.354 - 0.354 = -0.707
  • Y22=(−0.5)(0.707)+(−0.5)(−0.707)=−0.354+0.354=0Y_{22} = (-0.5)(0.707) + (-0.5)(-0.707) = -0.354 + 0.354 = 0

Following this pattern for all rows:

Y=[−2.1210−0.70700.70702.1210]\mathbf{Y} = \begin{bmatrix} -2.121 & 0 \\ -0.707 & 0 \\ 0.707 & 0 \\ 2.121 & 0 \end{bmatrix}

Look at what happened. The second column (second principal component) is all zeros. This makes perfect sense. Our original data was perfectly correlated, lying along a single line. All the variance is captured by the first principal component (the direction along that line). The second principal component, perpendicular to that line, has no variance at all.

If you wanted to reduce dimensionality, you could keep only the first column and represent your data in one dimension instead of two, losing zero information.