Homework 11
Before you begin
Homework 11 contains three questions on implementing K-means, image color compression, and hierarchical clustering. It is evaluated as Complete or Incomplete. Use either R or Python. You are not expected to use both languages.
Attempt each question before asking an AI tool for help. You may use AI tools, but you are responsible for checking the work and being able to explain your calculations and plots.
Download the materials
Solutions will be available after Monday, November 9, 2026.
Question 1: Implementing K-means
K-means is a relatively simple algorithm that we can write ourselves. For Questions 1 and 2, do not use an existing function that performs K-means directly.
Generate 10 observations with two independent standard normal coordinates. Set a random seed and report it. Randomly assign the observations to clusters, making sure neither cluster is empty. Let denote the cluster assigned to observation , and let denote the mean of cluster .
Perform one iteration: calculate the cluster means, then assign each observation to its closest mean using Euclidean distance. Report the means and updated assignments, and plot the observations colored by their updated assignments.
Write a function that repeats these two steps until the assignments stop changing or a specified maximum number of iterations is reached. Your function should work for a data matrix with any number of variables and a chosen number of clusters . Start from a nonempty assignment to each cluster; if a cluster becomes empty during iteration, keep its previous mean. Recalculate the cluster means from the final assignments before returning them. Return the final assignments, cluster means, and within-cluster sum of squares,
Apply your function to the same 10 observations from two different random initial assignments. Report the final plots, cluster means, and within-cluster sums of squares. Do the two runs produce the same grouping and objective value? Explain your findings.
Question 2: Image color compression
Pick your favorite color image and use your K-means function from Question 1 to cluster its pixels. For computational efficiency, resize the image to no more than pixels and use its three RGB color channels.
Plot the original image and report its dimensions. Transform it into a data matrix with one observation per pixel and three variables: red, green, and blue. Report the dimensions of this matrix.
For , apply your function to the pixel data. Report the cluster means and within-cluster sum of squares for each . Reconstruct and plot each compressed image by replacing every pixel with its assigned cluster mean. Use the same RGB scale for all fits.
Which value of would you choose for this image? Briefly justify your choice using the reconstructed images and the within-cluster sums of squares.
Question 3: Hierarchical clustering of cars
The file data/mtcars.csv contains 11 numerical variables for 32 cars, including fuel economy, horsepower, and weight. The car column gives the model names; use it to label observations, not as a clustering variable. You may use existing hierarchical clustering functions for this question.
Standardize each numerical variable to have mean 0 and standard deviation 1. Using Euclidean distance, perform hierarchical clustering with single, complete, and average linkage. Plot the three dendrograms with the car names and compare the results.
Choose one linkage method and a number of clusters that you consider reasonable. Report the cluster memberships and a table of cluster means on the original variable scales. Describe the main characteristics of the groups and explain your choice using the dendrogram and table. Briefly explain why standardizing the variables matters for this analysis.