← DemosIn your browser

PCA, visually

Reduce 4, 13, 30 or 64 features to 2 or 3 components and rotate the result in 3D: Iris, wine, breast cancer and handwritten digits.

ml-from-scratch

The data

The samples in component space

Hover or tap a point to see its details. Arrows show how much each feature weighs.

PC1PC2

Explained variance

How much of the information each component keeps.

Loadings

Each component is a mix of the original features.

The intuition in 2D

With the Iris flowers: rotate the line. Every flower is projected onto it, and the bar shows the variance of the projected points. PC1 is exactly the direction where that variance peaks.

Projected variance
sepal length (standardized) →↑ petal length (standardized)

PCA step by step

  1. 1

    Center

    Subtract each feature’s mean (and divide by its sd when standardizing), so all start at zero.

  2. 2

    Covariance

    Build the p×p matrix of how each pair of features varies together: 4×4 for Iris, 64×64 for digits.

  3. 3

    Eigenvectors

    The covariance eigenvectors are the component directions; the eigenvalues, the variance along each.

  4. 4

    Project

    Each sample becomes coordinates on the components. Keeping 2 or 3, p features become 2 or 3 numbers.

Correlation

The same PCA is implemented from scratch in NumPy, via SVD, in ml-from-scratch, and validated against scikit-learn. Here it runs in your browser, with eigenvectors from the Jacobi method, and the tests check each dataset against scikit-learn.