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:
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
MediumA 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.
- Write
spread_along(direction): the std of the cones' shadows on it. - Print both spreads, 3 decimals.
- 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
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