Lab 3 of 4about 40 minutes
The penguins' best road
It uses what you learned in Projection and Eigenvectors and eigenvalues.
Start here
Run itThe penguins from Python for AI, as la-projection has them: flipper length in centimetres across, weight in kilograms up, each measured from the average penguin. One penguin a row.
1The cloud
Run itFrom Projection
Run the cell. It prints the shape and the first penguin, and draws the cloud.
The first penguin's flipper is 1.99 cm shorter than the average, and it weighs 0.45 kg less. The cloud follows a road, about two across for one up.
2One penguin, by hand
Your turnFrom Projection
Drop the first penguin onto the road [2, 1]. Shrink the road's arrow to length 1 first, then take how far along, the projection and the error.
Without the shrink, first @ road gives -4.43: the road's arrow is about 2.24 long, so "how far along" comes out 2.24 times too big.
Hint
Divide the road by its length. Then how far along is one dot product.
One way to do it
first = penguins[0]
road = np.array([2, 1])
unit = road / np.linalg.norm(road)
along = first @ unit
projection = along * unit
error = first - projection
print(unit.round(2), round(along, 2), error.round(2))3All 342 at once
Your turnFrom Projection
Now every penguin. np.outer(along, unit) gives each number of along times unit, one row each: every penguin's projection. Keep each penguin's error, then its length, and print the average.
Hint
Each row's dot product with unit is one @. The error is each penguin minus its projection.
One way to do it
along = penguins @ unit
projection = np.outer(along, unit)
error = penguins - projection
gap = np.linalg.norm(error, axis=1)
print(along.shape, gap.mean().round(2))4Any road
Your turnFrom Projection
Write mean_gap(road): it shrinks the road's arrow, drops every penguin onto it, and gives back the average error. Then compare three roads: [2, 1], flipper alone [1, 0], and weight alone [0, 1].
Hint
Steps 2 and 3, in one function, with road in place of [2, 1].
One way to do it
def mean_gap(road):
unit = road / np.linalg.norm(road)
along = penguins @ unit
error = penguins - np.outer(along, unit)
return np.linalg.norm(error, axis=1).mean()
for road in ([2, 1], [1, 0], [0, 1]):
print(road, round(mean_gap(np.array(road)), 2))5The road as an eigenvector
Your turnFrom Eigenvectors and eigenvalues and Projection
penguins.T @ penguins is a 2 by 2 matrix of how the penguins spread together. Its own lines are the directions they spread along, and the line with the biggest eigenvalue is the one they spread along most. np.linalg.eigh gives each eigenvector as a column, with the eigenvalues smallest first. Keep the eigenvector with the biggest eigenvalue in best.
Hint
Look at values: the biggest is the last. Each eigenvector is a column of vectors, in the same place as its eigenvalue.
One way to do it
spread = penguins.T @ penguins
values, vectors = np.linalg.eigh(spread)
best = vectors[:, 1]
print(values.round(1), best.round(2), round(mean_gap(best), 4))6Every road
Run itFrom Eigenvectors and eigenvalues and Projection
Run the cell. It tries a road at every degree, from flat to straight up and back, and finds the one with the least average error. np.arctan2 gives a road's angle.
One road out of 180, and the eigenvector found it with no search. This is the road PCA finds, in the Machine Learning course: the direction the data spreads along most.
7The bills
On your ownFrom Projection and Eigenvectors and eigenvalues
On your own now. Do the same for bill length and bill depth, in centimetres, each from the average penguin: keep the penguins in bills, and the eigenvector of bills.T @ bills with the biggest eigenvalue in bill_road. Keep its average error in bill_gap.
Hint
Read the CSV again for the two bill columns, then steps 3 and 5 with bills in place of penguins. mean_gap reads penguins, so work the error out again here.
One way to do it
table = pd.read_csv("penguins.csv").dropna(subset=["bill_length_mm", "bill_depth_mm"])
bill_data = np.c_[table.bill_length_mm / 10, table.bill_depth_mm / 10]
bills = bill_data - bill_data.mean(axis=0)
bill_values, bill_vectors = np.linalg.eigh(bills.T @ bills)
bill_road = bill_vectors[:, np.argmax(bill_values)]
unit = bill_road / np.linalg.norm(bill_road)
bill_error = bills - np.outer(bills @ unit, unit)
bill_gap = np.linalg.norm(bill_error, axis=1).mean()
print(bill_road.round(2), round(bill_gap, 3))Run on Pyodide 314, Python 3.14, NumPy 2.4, pandas 3.0 and matplotlib 3.10. Checked on .
