PCA
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 26 of 51.
PCA finds the direction of most spread, then the best one at a right angle to it, and so on: the components. On the wine twins:
Each row's shadows on the first few components become its new features:
from sklearn.decomposition import PCA
scaled = StandardScaler().fit_transform(X)
points = PCA(n_components=2).fit_transform(scaled)
print(X.shape, points.shape)For all 13 wine measurements, this prints (178, 13) (178, 2).
Scale first. PCA chases spread, so a feature in big units, like proline in the hundreds, wins on size alone:
Challenge
MediumA water lab tests bottles from three springs, 8 measurements each. Can a 2D map tell the springs apart? draw_springs(ax, points) colors any 2D points by spring.
- Scale
X, then reduce it to 2 components with PCA, intopoints. - Print the number of features before and after.
- Draw the points with
draw_springsand show.
For another lab, with 6 measurements:
From 6 features to 2Coddy describes your map.
Try it yourself
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
# A water lab: 8 measurements for each of 120 bottles from three springs
waters = pd.read_csv("waters.csv")
X = waters.drop(columns="spring")
# Scatters 2D points, one group per spring, with a legend
def draw_springs(ax, points):
for spring in ["alpine", "volcanic", "coastal"]:
rows = (waters["spring"] == spring).to_numpy()
ax.scatter(points[rows, 0], points[rows, 1], s=16, label=spring)
ax.legend()
fig, ax = plt.subplots()
ax.set_title("Spring Waters in 2D")
ax.set_xlabel("First component")
ax.set_ylabel("Second component")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler