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
n_componentsis how many axes you keep.explained_variance_ratio_says what each one is worth.- Scale first: PCA follows whichever feature has the biggest numbers.
Keywords and builtins used here
print
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.
Step 2 of 2 in Unsupervised, step 21 of 25 in Machine learning with scikit-learn.