Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 45

Principal Component Analysis

Rotate a table onto the few directions that keep the most spread, and drop the quiet directions. One example is on this page.

Intermediate12 min
Principal Component Analysis MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

PCA finds new axes that are mixes of the original columns. The first axis is the direction of greatest variance. Later axes are the next-best directions at right angles to the ones already chosen. Keeping the first axis is a compression that preserves as much spread as it can.

Example

python
import numpy as np
from sklearn.decomposition import PCA

X = np.array([[1, 2, 3], [2, 3, 4], [3, 5, 7], [4, 6, 8]], dtype=float)
pca = PCA(n_components=1).fit(X)
print(f"variance kept: {pca.explained_variance_ratio_[0]:.2f}")

Output:

text
variance kept: 0.99

When to use it

  • Use it to plot many columns, or to speed up a later model.
  • Standardize columns that live on different scales before fitting.
  • The new axes are mixes. They are not the original feature names.

The theory lesson Dimensionality Reduction goes further on PCA.

Loading playground…