typestar

PCA in Python

Rotating the data so the first axes carry the most variance.

from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_iris(return_X_y=True)
reduce_to_two = make_pipeline(StandardScaler(), PCA(n_components=2))
projected = reduce_to_two.fit_transform(X)

pca = reduce_to_two.named_steps["pca"]
print(projected.shape)
print(pca.explained_variance_ratio_.round(3))
print(f"kept {pca.explained_variance_ratio_.sum():.1%} of the variance")

How it works

  1. n_components is how many axes you keep.
  2. explained_variance_ratio_ says what each one is worth.
  3. Scale first: PCA follows whichever feature has the biggest numbers.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
497
Tokens
103
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 56 seconds.

Type this snippet

Step 2 of 2 in Unsupervised, step 21 of 25 in Machine learning with scikit-learn.

← Previous Next →