6.1 Step-by-Step Numerical PCA by Hand on a 2D Dataset

Nothing solidifies linear algebra like executing every step of Principal Component Analysis by hand on clean numbers where you can track every coordinate from raw input to compressed 1D score and back to 2D reconstruction.

In this microlearning, we work through a complete numerical example chosen so that all arithmetic—mean-centering, covariance construction, characteristic roots, unit eigenvectors, 1D projection scores, and orthogonal reconstruction errors—comes out to exact, transparent expressions.

In this Article

1. The Raw 4-Point Dataset and Step 1: Mean-Centering

Consider n=4n = 4 observations of two features (x1,x2)(x_1, x_2):

x1raw=(6,5),x2raw=(5,6),x3raw=(2,1),x4raw=(3,0)\mathbf{x}_1^{\text{raw}} = (6, 5), \quad \mathbf{x}_2^{\text{raw}} = (5, 6), \quad \mathbf{x}_3^{\text{raw}} = (2, 1), \quad \mathbf{x}_4^{\text{raw}} = (3, 0)

The sample mean vector is μ=(6+5+2+34,5+6+1+04)=(4,3)\boldsymbol{\mu} = \left(\frac{6+5+2+3}{4}, \frac{5+6+1+0}{4}\right) = (4, 3). Subtracting μ=(4,3)\boldsymbol{\mu} = (4, 3) from each observation gives the centered matrix XX:

X=[2213−2−2−1−3]X = \begin{bmatrix} 2 & 2 \\ 1 & 3 \\ -2 & -2 \\ -1 & -3 \end{bmatrix}

2. Step 2 & 3: Covariance Matrix C and Exact Eigenpairs

We compute C=14XTXC = \frac{1}{4}X^T X:

  • c11=14(4+1+4+1)=104=2.5c_{11} = \frac{1}{4}(4 + 1 + 4 + 1) = \frac{10}{4} = 2.5
  • c22=14(4+9+4+9)=264=6.5c_{22} = \frac{1}{4}(4 + 9 + 4 + 9) = \frac{26}{4} = 6.5
  • c12=14(4+3+4+3)=144=3.5c_{12} = \frac{1}{4}(4 + 3 + 4 + 3) = \frac{14}{4} = 3.5

Thus C=[2.53.53.56.5]C = \begin{bmatrix} 2.5 & 3.5 \\ 3.5 & 6.5 \end{bmatrix}. Now solve det⁡(C−λI)=0\det(C - \lambda I) = 0:

  • tr(C)=2.5+6.5=9.0\text{tr}(C) = 2.5 + 6.5 = 9.0
  • det⁡(C)=(2.5)(6.5)−(3.5)2=16.25−12.25=4.0\det(C) = (2.5)(6.5) - (3.5)^2 = 16.25 - 12.25 = 4.0
  • Characteristic equation: λ2−9λ+4=0  ⟹  λ1=9+652≈8.531\lambda^2 - 9\lambda + 4 = 0 \implies \lambda_1 = \frac{9 + \sqrt{65}}{2} \approx 8.531, λ2=9−652≈0.469\lambda_2 = \frac{9 - \sqrt{65}}{2} \approx 0.469.

For a symmetric 2×22 \times 2 matrix with c12≠0c_{12} \neq 0, the unit eigenvector for λ1\lambda_1 is proportional to [c12λ1−c11]=[3.56.031]\begin{bmatrix} c_{12} \\ \lambda_1 - c_{11} \end{bmatrix} = \begin{bmatrix} 3.5 \\ 6.031 \end{bmatrix}, which normalizes to:

u1≈[0.5020.865],u2≈[−0.8650.502]\mathbf{u}_1 \approx \begin{bmatrix} 0.502 \\ 0.865 \end{bmatrix}, \qquad \mathbf{u}_2 \approx \begin{bmatrix} -0.865 \\ 0.502 \end{bmatrix}

3. Step 4 & 5: 1D Principal Scores zᵢ and 2D Reconstructed Points

Projecting onto PC1 via zi=xiTu1z_i = \mathbf{x}_i^T \mathbf{u}_1 and reconstructing in raw coordinates via x^iraw=μ+ziu1\hat{\mathbf{x}}_i^{\text{raw}} = \boldsymbol{\mu} + z_i \mathbf{u}_1 yields:

Point iiCentered xi\mathbf{x}_i1D Score zi=xi⋅u1z_i = \mathbf{x}_i \cdot \mathbf{u}_1Centered Shadow x^i=ziu1\hat{\mathbf{x}}_i = z_i \mathbf{u}_1Squared Error ei2=∥xi−x^i∥2e_i^2 = \|\mathbf{x}_i - \hat{\mathbf{x}}_i\|^2
1(2,2)(2, 2)+2.734+2.734(1.372,2.365)(1.372, 2.365)0.5270.527
2(1,3)(1, 3)+3.097+3.097(1.555,2.679)(1.555, 2.679)0.4110.411
3(−2,−2)(-2, -2)−2.734-2.734(−1.372,−2.365)(-1.372, -2.365)0.5270.527
4(−1,−3)(-1, -3)−3.097-3.097(−1.555,−2.679)(-1.555, -2.679)0.4110.411

4. Verifying the Pythagorean Accounting Identity to the Last Decimal

  • Total Original Scatter: tr(C)=2.5+6.5=9.000\text{tr}(C) = 2.5 + 6.5 = 9.000
  • Captured Variance on PC1: 14(2.7342+3.0972+(−2.734)2+(−3.097)2)=8.531=λ1\frac{1}{4}(2.734^2 + 3.097^2 + (-2.734)^2 + (-3.097)^2) = 8.531 = \lambda_1
  • Mean Squared Orthogonal Reconstruction Error: 14(0.527+0.411+0.527+0.411)=0.469=λ2\frac{1}{4}(0.527 + 0.411 + 0.527 + 0.411) = 0.469 = \lambda_2
  • Conservation Check: λ1+λ2=8.531+0.469=9.000=tr(C)\lambda_1 + \lambda_2 = 8.531 + 0.469 = 9.000 = \text{tr}(C)!

5. Active Recall Check and Key Takeaways

Quick Check: Test Your Understanding

Question: In the worked example above, why must we add μ=(4,3)\boldsymbol{\mu} = (4, 3) back at the very end when reconstructing x^iraw=μ+ziu1\hat{\mathbf{x}}_i^{\text{raw}} = \boldsymbol{\mu} + z_i \mathbf{u}_1?

Answer: Because ziu1z_i \mathbf{u}_1 reconstructs the point relative to the centered origin (0,0)(0, 0). Adding the mean vector μ\boldsymbol{\mu} shifts the reconstructed shadow back to the original coordinate frame of the raw dataset.

  • 2x2 Eigenvector Formula: For C=[c11c12c12c22]C = \begin{bmatrix} c_{11} & c_{12} \\ c_{12} & c_{22} \end{bmatrix} with c12≠0c_{12} \neq 0, the eigenvector for λ1\lambda_1 points along (c12,λ1−c11)T(c_{12}, \lambda_1 - c_{11})^T.
  • Exact Error Verification: The average squared reconstruction error when keeping PC1 equals λ2\lambda_2 to the last decimal place.
  • Canonical References: See Wikipedia Reference on Computing PCA via the Covariance Method.

Take a Quiz Based on This Article

Test your understanding with AI-generated questions tailored to this content

Adaptive Learning with SynLearn

Explore this article through guided practice that adapts to your answers

✓Step-by-step concepts & lessons
✓Active recall & adaptive feedback
✓Personalized spaced repetition
pca-by-hand
worked-example
covariance-matrix
eigenvalues
eigenvectors
linear-algebra