Lecture 8 · Clustering and PCA
What do playlists and neighbourhoods have in common?
We’ll group students by their media habits, then see how changing the variables can change the groups.
Think of a song Spotify recommended that you actually liked. What might you have in common with other people who listen to it? We’ll use a few measurements from our media example to put students into groups, then do something similar with neighbourhoods.
Which measurements did we use to make the groups?
We used four measurements to group students: short-video time, gaming time, music time and subscription spending. The first figure shows just two of them. Look for the open squares to see the group centres in that view. For neighbourhoods, we used walking times to groceries, parks and transit, plus the number of homes.
Check whether the groups make sense
PCA gives us two new axes that summarize much of the variation. In our media example the two axes keep about 90% of it. Then we compare different numbers of groups. The elbow plot shows how much variation remains within groups, and silhouette scores help us check whether a student fits their own group better than the next closest one.
In R
The snippet uses the classroom media_habits.csv file and the same feature set used for the figures.
media <- read.csv("Data/media_habits.csv")
features <- c("short_video_min", "gaming_min",
"music_min", "subscriptions_cad")
z <- scale(media[features])
set.seed(421008)
km <- kmeans(z, centers = 3, nstart = 50)
aggregate(media[features], list(cluster = km$cluster), mean)
pc <- prcomp(z)
Compare the profiles in minutes and dollars. The PCA axes describe directions of variation across the features.
Key takeaways
- Changing the features or their scales can change who ends up together.
- Check how many students are in each group and what their habits look like. Then compare that with the plots and scores.

