---
title: "Homework 11"
pagetitle: "Homework 11"
body-classes: "lecture-page practice-page"
format:
  html:
    embed-resources: true
    html-math-method: mathml
    page-layout: full
    toc: true
    toc-location: body
    toc-title: "On this page"
    toc-depth: 2
---

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 $K=2$ clusters, making sure neither cluster is empty. Let $C(i)$ denote the cluster assigned to observation $x_i$, and let $m_k$ denote the mean of cluster $k$.

a. 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.

b. 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 $K$. 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,

   $$
   \sum_{k=1}^K\sum_{i:\,C(i)=k}\lVert x_i-m_k\rVert^2.
   $$

c. 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 $200\times200$ pixels and use its three RGB color channels.

a. 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.

b. For $K=2,3,5,10$, apply your function to the pixel data. Report the cluster means and within-cluster sum of squares for each $K$. Reconstruct and plot each compressed image by replacing every pixel with its assigned cluster mean. Use the same RGB scale for all fits.

c. Which value of $K$ 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.

a. 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.

b. 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.
