Menu
CoddyTech

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:

The scaled wine twins as a cloud rising to the right, with two red arrows from the center: a long one, component 1, along the cloud, and a short one, component 2, across it

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:

Two PCA maps of the 178 wines. Left, not scaled: the three cultivars overlap in one smear. Right, scaled first: three clear groups

challenge icon

Challenge

Medium

A 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.

  1. Scale X, then reduce it to 2 components with PCA, into points.
  2. Print the number of features before and after.
  3. Draw the points with draw_springs and show.

For another lab, with 6 measurements:

From 6 features to 2

A chart titled Spring Waters in 2D with first component across and second up: alpine bottles at the lower left, volcanic at the lower right, coastal at the top, with a legend

Coddy 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler