Principal Component Analysis

Fewer features, most of the information kept. The classic way to shrink data.

🟡 intermediate5 min readunsupervisedmust-know
PCA reduces the number of features while keeping as much information as possible. It finds the directions along which your data varies the most, and rotates the data to line up with them. The first direction captures the most spread, the second the next most, and so on, each at right angles to the rest. Keep the top few and drop the others, and you have fewer features that still describe the data well. Always scale your features first, or the largest-unit feature dominates.

What is Principal Component Analysis (PCA)?

When working with high dimensional data (such as 1000 features), visualizations become impossible and models suffer from the Curse of Dimensionality.

Principal Component Analysis (PCA) compresses high dimensional data into a smaller number of uncorrelated linear features called Principal Components.

  2D High Dimensional Feature Space (x1, x2)
     ▲
     │    /  ◄── First Principal Component PC1 (Direction of Maximum Variance!)
     │   / .
     │  / . .
     │ / .
  ───┴─────────────► Second Principal Component PC2 (Orthogonal to PC1!)
  1. PC1: Direction along which data points are most spread out (Maximizes Variance).
  2. PC2: Direction perpendicular (orthogonal) to PC1 that captures the second highest variance.
  3. PC3...PCk: Subsequent orthogonal directions capturing remaining variance.

Step-by-Step Mathematical Algorithm

Given data matrix XX of shape [N×d][N \times d]:

Step 1: Mean Centering

Subtract feature means so every feature has mean zero:

Xcentered=X−μX_{\text{centered}} = X - \mu

Step 2: Compute Covariance Matrix

Calculate the d×dd \times d sample covariance matrix Σ\Sigma:

Σ=1N−1XcenteredTXcentered\Sigma = \frac{1}{N-1} X_{\text{centered}}^T X_{\text{centered}}

Covariance matrix measures pairwise linear correlations between all feature pairs.

Step 3: Compute Eigenvectors and Eigenvalues

Solve the characteristic equation for covariance matrix Σ\Sigma:

Σvi=λivi\Sigma v_i = \lambda_i v_i

Step 4: Sort and Select Top K Components

Sort eigenvalues in descending order (λ1≥λ2≥⋯≥λd\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_d).

Select the top kk eigenvectors to form projection matrix Wk∈Rd×kW_k \in \mathbb{R}^{d \times k}.

Step 5: Project Data onto Lower Dimension

Multiply original centered data by projection matrix:

Xreduced=XcenteredWk∈RN×kX_{\text{reduced}} = X_{\text{centered}} W_k \in \mathbb{R}^{N \times k}

Preserved Variance Ratio

The Explained Variance Ratio for component ii is:

Explained Variance Ratioi=λi∑j=1dλj\text{Explained Variance Ratio}_i = \frac{\lambda_i}{\sum_{j=1}^d \lambda_j}

Summing ratios for kk components tells you what percentage of original total information was retained (for example, keeping 95 percent of variance).

Say this out loud

PCA is an unsupervised linear dimensionality reduction algorithm that projects data onto orthogonal principal components to maximize preserved variance. It mean centers data, computes the covariance matrix, and calculates eigenvectors for direction and eigenvalues for variance magnitude. PCA decorrelates features and compresses dimensions while retaining maximum information.

Followups to expect

  1. Why is SVD (Singular Value Decomposition) preferred over Eigenvalue decomposition in practice? Computing covariance matrix Σ=XTX\Sigma = X^T X takes time and risks numerical instability. Direct SVD on X=USVTX = U S V^T is faster and numerically stable (np.linalg.svd).
  2. Can PCA capture non linear patterns? No, standard PCA finds linear combinations of features. Use Kernel PCA or non linear embedding algorithms (t-SNE / UMAP) for non linear manifolds.

Check yourself

Question 1 of 3

What primary mathematical objective does Principal Component Analysis (PCA) maximize when finding the first Principal Component (PC1)?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min