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.
1. The Raw 4-Point Dataset and Step 1: Mean-Centering
Consider n = 4 n = 4 n = 4 observations of two features ( x 1 , x 2 ) (x_1, x_2) ( x 1 , x 2 ) :
x 1 raw = ( 6 , 5 ) , x 2 raw = ( 5 , 6 ) , x 3 raw = ( 2 , 1 ) , x 4 raw = ( 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) x 1 raw = ( 6 , 5 ) , x 2 raw = ( 5 , 6 ) , x 3 raw = ( 2 , 1 ) , x 4 raw = ( 3 , 0 )
The sample mean vector is μ = ( 6 + 5 + 2 + 3 4 , 5 + 6 + 1 + 0 4 ) = ( 4 , 3 ) \boldsymbol{\mu} = \left(\frac{6+5+2+3}{4}, \frac{5+6+1+0}{4}\right) = (4, 3) μ = ( 4 6 + 5 + 2 + 3 , 4 5 + 6 + 1 + 0 ) = ( 4 , 3 ) . Subtracting μ = ( 4 , 3 ) \boldsymbol{\mu} = (4, 3) μ = ( 4 , 3 ) from each observation gives the centered matrix X X X :
X = [ 2 2 1 3 − 2 − 2 − 1 − 3 ] X = \begin{bmatrix} 2 & 2 \\ 1 & 3 \\ -2 & -2 \\ -1 & -3 \end{bmatrix} X = 2 1 − 2 − 1 2 3 − 2 − 3
2. Step 2 & 3: Covariance Matrix C and Exact Eigenpairs
We compute C = 1 4 X T X C = \frac{1}{4}X^T X C = 4 1 X T X :
c 11 = 1 4 ( 4 + 1 + 4 + 1 ) = 10 4 = 2.5 c_{11} = \frac{1}{4}(4 + 1 + 4 + 1) = \frac{10}{4} = 2.5 c 11 = 4 1 ( 4 + 1 + 4 + 1 ) = 4 10 = 2.5
c 22 = 1 4 ( 4 + 9 + 4 + 9 ) = 26 4 = 6.5 c_{22} = \frac{1}{4}(4 + 9 + 4 + 9) = \frac{26}{4} = 6.5 c 22 = 4 1 ( 4 + 9 + 4 + 9 ) = 4 26 = 6.5
c 12 = 1 4 ( 4 + 3 + 4 + 3 ) = 14 4 = 3.5 c_{12} = \frac{1}{4}(4 + 3 + 4 + 3) = \frac{14}{4} = 3.5 c 12 = 4 1 ( 4 + 3 + 4 + 3 ) = 4 14 = 3.5
Thus C = [ 2.5 3.5 3.5 6.5 ] C = \begin{bmatrix} 2.5 & 3.5 \\ 3.5 & 6.5 \end{bmatrix} C = [ 2.5 3.5 3.5 6.5 ] . Now solve det ( C − λ I ) = 0 \det(C - \lambda I) = 0 det ( C − λ I ) = 0 :
tr ( C ) = 2.5 + 6.5 = 9.0 \text{tr}(C) = 2.5 + 6.5 = 9.0 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 det ( C ) = ( 2.5 ) ( 6.5 ) − ( 3.5 ) 2 = 16.25 − 12.25 = 4.0
Characteristic equation: λ 2 − 9 λ + 4 = 0 ⟹ λ 1 = 9 + 65 2 ≈ 8.531 \lambda^2 - 9\lambda + 4 = 0 \implies \lambda_1 = \frac{9 + \sqrt{65}}{2} \approx 8.531 λ 2 − 9 λ + 4 = 0 ⟹ λ 1 = 2 9 + 65 ≈ 8.531 , λ 2 = 9 − 65 2 ≈ 0.469 \lambda_2 = \frac{9 - \sqrt{65}}{2} \approx 0.469 λ 2 = 2 9 − 65 ≈ 0.469 .
For a symmetric 2 × 2 2 \times 2 2 × 2 matrix with c 12 ≠ 0 c_{12} \neq 0 c 12 = 0 , the unit eigenvector for λ 1 \lambda_1 λ 1 is proportional to [ c 12 λ 1 − c 11 ] = [ 3.5 6.031 ] \begin{bmatrix} c_{12} \\ \lambda_1 - c_{11} \end{bmatrix} = \begin{bmatrix} 3.5 \\ 6.031 \end{bmatrix} [ c 12 λ 1 − c 11 ] = [ 3.5 6.031 ] , which normalizes to:
u 1 ≈ [ 0.502 0.865 ] , u 2 ≈ [ − 0.865 0.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} u 1 ≈ [ 0.502 0.865 ] , u 2 ≈ [ − 0.865 0.502 ]
3. Step 4 & 5: 1D Principal Scores zᵢ and 2D Reconstructed Points
Projecting onto PC1 via z i = x i T u 1 z_i = \mathbf{x}_i^T \mathbf{u}_1 z i = x i T u 1 and reconstructing in raw coordinates via x ^ i raw = μ + z i u 1 \hat{\mathbf{x}}_i^{\text{raw}} = \boldsymbol{\mu} + z_i \mathbf{u}_1 x ^ i raw = μ + z i u 1 yields:
Point i i i Centered x i \mathbf{x}_i x i 1D Score z i = x i ⋅ u 1 z_i = \mathbf{x}_i \cdot \mathbf{u}_1 z i = x i ⋅ u 1 Centered Shadow x ^ i = z i u 1 \hat{\mathbf{x}}_i = z_i \mathbf{u}_1 x ^ i = z i u 1 Squared Error e i 2 = ∥ x i − x ^ i ∥ 2 e_i^2 = \|\mathbf{x}_i - \hat{\mathbf{x}}_i\|^2 e i 2 = ∥ x i − x ^ i ∥ 2 1 ( 2 , 2 ) (2, 2) ( 2 , 2 ) + 2.734 +2.734 + 2.734 ( 1.372 , 2.365 ) (1.372, 2.365) ( 1.372 , 2.365 ) 0.527 0.527 0.527 2 ( 1 , 3 ) (1, 3) ( 1 , 3 ) + 3.097 +3.097 + 3.097 ( 1.555 , 2.679 ) (1.555, 2.679) ( 1.555 , 2.679 ) 0.411 0.411 0.411 3 ( − 2 , − 2 ) (-2, -2) ( − 2 , − 2 ) − 2.734 -2.734 − 2.734 ( − 1.372 , − 2.365 ) (-1.372, -2.365) ( − 1.372 , − 2.365 ) 0.527 0.527 0.527 4 ( − 1 , − 3 ) (-1, -3) ( − 1 , − 3 ) − 3.097 -3.097 − 3.097 ( − 1.555 , − 2.679 ) (-1.555, -2.679) ( − 1.555 , − 2.679 ) 0.411 0.411 0.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 tr ( C ) = 2.5 + 6.5 = 9.000
Captured Variance on PC1: 1 4 ( 2.734 2 + 3.097 2 + ( − 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 4 1 ( 2.73 4 2 + 3.09 7 2 + ( − 2.734 ) 2 + ( − 3.097 ) 2 ) = 8.531 = λ 1
Mean Squared Orthogonal Reconstruction Error: 1 4 ( 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 4 1 ( 0.527 + 0.411 + 0.527 + 0.411 ) = 0.469 = λ 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) λ 1 + λ 2 = 8.531 + 0.469 = 9.000 = 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) μ = ( 4 , 3 ) back at the very end when reconstructing x ^ i raw = μ + z i u 1 \hat{\mathbf{x}}_i^{\text{raw}} = \boldsymbol{\mu} + z_i \mathbf{u}_1 x ^ i raw = μ + z i u 1 ?
Answer: Because z i u 1 z_i \mathbf{u}_1 z i u 1 reconstructs the point relative to the centered origin ( 0 , 0 ) (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 = [ c 11 c 12 c 12 c 22 ] C = \begin{bmatrix} c_{11} & c_{12} \\ c_{12} & c_{22} \end{bmatrix} C = [ c 11 c 12 c 12 c 22 ] with c 12 ≠ 0 c_{12} \neq 0 c 12 = 0 , the eigenvector for λ 1 \lambda_1 λ 1 points along ( c 12 , λ 1 − c 11 ) T (c_{12}, \lambda_1 - c_{11})^T ( c 12 , λ 1 − c 11 ) T .
Exact Error Verification: The average squared reconstruction error when keeping PC1 equals λ 2 \lambda_2 λ 2 to the last decimal place.
Canonical References: See Wikipedia Reference on Computing PCA via the Covariance Method .