Menu
CoddyTech

Projections

Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 25 of 51.

A projection drops each point straight onto a line, like a shadow. Here are the wine twins from Too Many Features, centered on their mean, on two lines:

Two charts of the same wines as blue dots around the center. Left, a line along the cloud: orange shadows spread far along it, spread 1.14. Right, a line across the cloud: shadows bunch together, spread 0.30

Along the cloud, the shadows keep most of the spread, so one number per wine says nearly what two did.

A direction is two numbers whose squares add up to 1, like (0.6, 0.8). A shadow is a weighted sum:

centered = X - X.mean(axis=0)
shadow = centered[:, 0] * 0.6 + centered[:, 1] * 0.8
print(round(shadow.std(), 3))

This prints 1.136. The best direction keeps the most spread.

challenge icon

Challenge

Medium

A forester wants one number per pine cone instead of length and width. Which of two directions keeps more of the cones' spread? The program reads two directions, two numbers each.

  1. Write spread_along(direction): the std of the cones' shadows on it.
  2. Print both spreads, 3 decimals.
  3. Print the direction to keep.

For other cones:

Along [0.6, 0.8]: spread 2.104
Along [0.8, -0.6]: spread 0.412
Keep [0.6, 0.8]

Try it yourself

import numpy as np
import pandas as pd

# A forester measured 70 pine cones: length and width in cm
cones = pd.read_csv("cones.csv")
X = np.column_stack([cones["length_cm"], cones["width_cm"]])
first = [float(input()), float(input())]
second = [float(input()), float(input())]

# 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