Principal Component Analysis (PCA)

Visualize how PCA finds directions of maximum variance and projects data onto principal components

Try these examples:

Principal Component Analysis (PCA)

Principal Component Analysis finds the directions of maximum variance in your data. These directions, called principal components, form a new coordinate system that better captures the data's structure.

What Does PCA Do?

  1. Find the direction of maximum variance (first principal component)
  2. Find perpendicular directions of remaining variance (additional components)
  3. Project data onto these new axes for dimensionality reduction

Visualizing PCA

Here is a cloud of 2D points. Click the plot to add a point, double-click a point to remove it, or drag points around: PCA is recomputed from whatever data you make.

Your Data, Its Principal Axes, and a Direction to Try

PC1PC2-505-505XYCovariance ellipse (2 SD along each PC)Drops onto your direction

The red axis (PC1) points in the direction of maximum variance, and the blue axis (PC2) is perpendicular to it. Each is drawn two standard deviations long, so together they are the axes of the dashed covariance ellipse. Nothing about these directions was typed in: they are the eigenvectors of the covariance matrix of your points.

To see why PC1 is special, pick a direction yourself: ϕ\phi = 0 degrees (the green line). Each point drops perpendicularly onto it, and the places where they land have variance 2.85, which is 63.93% of the total 4.46. Turn the line toward PC1 at 34.57 degrees: the projected variance climbs to its maximum, λ1\lambda_1, exactly when the drops are shortest, because projected variance and squared drop lengths always add up to the same total.

Eigenvectors and Eigenvalues

The principal components are the eigenvectors of the covariance matrix Σ\Sigma. The eigenvalues (λ\lambda) quantify the variance each component captures.

ComponentEigenvalue (λ\lambda)Variance Explained
PC13.9789.1%
PC20.4810.9%

The curve below runs through every direction at once: its peak is λ1\lambda_1 at PC1 and its lowest point is λ2\lambda_2 at PC2, 90 degrees away. The eigenvalues are simply the most and the least variance that any direction can capture.

Variance Along Every Direction

PC1PC2your direction05010015001234Direction angle (degrees)Variance along the directionVariance of the projectionslambda1 (largest)lambda2 (smallest)

Strong reduction opportunity! PC1 alone explains 89.1% of the variance -- reducing from 2D to 1D loses very little.

Dimensionality Reduction

Select how many components to keep:

Keeping Both Components (2D)

With both components you retain 100% of the variance in a rotated coordinate system aligned with the data's natural axes.

Explore Further

Drag a few points away from the main cloud and watch the axes swing toward them: PCA follows variance, so it is sensitive to outliers. Load the Circular preset and PC1 becomes almost arbitrary, because every direction has nearly the same variance. Switch to 1 component () to see the 1D projection.

Try it in Code

# PCA on the points from the plot: covariance, eigenvalues, PC1 direction
import numpy as np
from sklearn.decomposition import PCA

x = np.array([0.8,1,0.7,0.3,0.4,1.7,1.7,0.5,-1.4,-1.9,-2.5,-1.7,1.4,-3.6,0.8,-1.7,-0.4,3.5,0.4,-0.1])
y = np.array([1,-0.3,-0.5,2,-0.3,1.6,0.1,1.4,-1,-2.3,-1.3,-0.3,0.8,-2.3,0.8,-1.2,-0.9,1.2,1.5,0.1])
X = np.column_stack([x, y])

S = np.cov(X.T)                # sample covariance (divides by n - 1)
vals, vecs = np.linalg.eigh(S) # ascending order
print(f"Eigenvalues: {vals[1]:.3f}, {vals[0]:.3f}")
print(f"PC1 explains {100 * vals[1] / vals.sum():.1f}%")
print(f"PC1 angle: {np.degrees(np.arctan2(vecs[1, 1], vecs[0, 1])) % 180:.1f} degrees")

pca = PCA(n_components=2).fit(X)
print(pca.explained_variance_, pca.explained_variance_ratio_)

Inputs

What to explore next

You might also like