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?
- Find the direction of maximum variance (first principal component)
- Find perpendicular directions of remaining variance (additional components)
- 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
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: =
Eigenvectors and Eigenvalues
The principal components are the eigenvectors of the covariance matrix . The eigenvalues () quantify the variance each component captures.
| Component | Eigenvalue () | Variance Explained |
|---|---|---|
| PC1 | ||
| PC2 |
The curve below runs through every direction at once: its peak is at PC1 and its lowest point is at PC2, 90 degrees away. The eigenvalues are simply the most and the least variance that any direction can capture.
Variance Along Every Direction
Strong reduction opportunity! PC1 alone explains
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 (
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
Birthday Paradox
Explore the surprising probability that two people share a birthday
Comparing Distributions with Box Plots
Visualize and compare multiple datasets using box plots and five-number summaries
Correlation Explorer
Visualize and understand correlation strength and direction
Cross-Validation Techniques
Learn k-fold, stratified, and leave-one-out cross-validation for robust model evaluation
You might also like
Cross-Validation Techniques
ExplorationLearn k-fold, stratified, and leave-one-out cross-validation for robust model evaluation
Feature Scaling for Machine Learning
ExplorationUnderstand standardization, normalization, and why feature scaling matters for ML algorithms
Gradient Descent Optimization
ExplorationExplore how gradient descent finds function minima by iteratively following the steepest descent direction
Logistic Regression Explorer
ExplorationVisualize binary classification with the sigmoid function, decision boundary, and probability predictions