Python ML PCA: Fewer Dimensions, Same Picture
PCA shrinks many measurements into a few new ones. The new measurements keep most of the spread in the data.
Four measurements become two
PCA with two components turns 150 rows of four numbers into 150 rows of two numbers.
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris = load_iris()
changed = PCA(n_components=2, random_state=42).fit_transform(iris.data)
print(changed.shape)The first component keeps most of the spread
The first component keeps 0.9246 of the spread. The two together keep 0.9777, so little is lost.
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris = load_iris()
pca = PCA(n_components=2, random_state=42)
pca.fit(iris.data)
print([round(float(v), 4) for v in pca.explained_variance_ratio_])
print("Total kept:", round(float(pca.explained_variance_ratio_.sum()), 4))Setosa sits apart from the rest
The mean first component is -2.64 for setosa, 0.53 for versicolor, and 2.11 for virginica. The species separate along one line.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris = load_iris()
changed = PCA(n_components=2, random_state=42).fit_transform(iris.data)
names = [str(name) for name in iris.target_names]
for code in range(3):
print("Mean first component", names[code] + ":", round(float(changed[iris.target == code, 0].mean()), 2))Example
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris = load_iris()
X = iris.data
y = iris.target
names = [str(name) for name in iris.target_names]
pca = PCA(n_components=2, random_state=42)
changed = pca.fit_transform(X)
print("Explained variance:", [round(float(v), 4) for v in pca.explained_variance_ratio_])
print("Total kept:", round(float(pca.explained_variance_ratio_.sum()), 4))
print("Shape:", changed.shape)
for i in range(5):
print("Row", i, [round(float(v), 2) for v in changed[i]], names[y[i]])
for code in range(3):
print("Mean first component", names[code] + ":", round(float(changed[y == code, 0].mean()), 2))Key points
- PCA: a method that shrinks many columns into a few new ones
- Component: one new column built by PCA from the old columns
- Explained variance: the share of the data spread one component keeps
- Use PCA with n_components=2 to plot four measurements on one flat picture
- The first component keeps the most spread, and each later component keeps less
In this example, PCA takes the four Iris measurements and builds two new columns. The first new column keeps 0.9246 of the spread, and the two together keep 0.9777, so almost nothing is lost. The shape drops from 150 rows of four numbers to 150 rows of two numbers. The first five rows print with their species, and all five are setosa with a first component near -2.7. The species means sit far apart on the first component, so the three species still separate in the smaller picture.
Note
PCA needs scaling when the columns use different units, but the Iris columns all use centimetres, so plain PCA is fair here.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
PCA turns four Iris measurements into two components that keep 0.9777 of the spread. What does that mean?