Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 13 — Lab: Unsupervised Learning

Open in Colab

Lab Overview

You will implement the core of each unsupervised method of the chapter and check it against scikit-learn or SciPy.

ExerciseTopicPoints
1The two steps of k-means20
2The silhouette15
3Agglomerative clustering and linkage20
4DBSCAN: core, border and noise points15
5PCA15
6An autoencoder that beats PCA15
ChallengeOne step of EM for a Gaussian mixture+10

Complete the code where you see # TODO, then run the check cell under it.


Exercise 1: The Two Steps of k-Means (20 points)

Complete assign(X, C) (the index of the nearest centroid for each row of X) and update(X, labels, k) (the mean of each cluster’s points; you may assume no cluster is empty). kmeans then alternates them.


Exercise 2: The Silhouette (15 points)

Complete silhouette(X, labels), returning one value per point: s=(b−a)/max⁡(a,b)s = (b - a)/\max(a, b), where aa is the mean distance to the other points of its own cluster and bb the smallest mean distance to the points of another cluster. (Assume every cluster has at least 2 points.)


Exercise 3: Agglomerative Clustering (20 points)

Complete merge_heights(X, linkage): start with one cluster per point and repeatedly merge the two closest clusters, until one remains. Return the list of merge distances, in order. linkage is 'single' (closest pair), 'complete' (farthest pair) or 'average' (mean over all pairs). A plain implementation with lists of point indices is fine.


Exercise 4: DBSCAN Point Types (15 points)

Complete point_types(X, eps, min_pts), returning an array of strings 'core', 'border' or 'noise'. A point is core if at least min_pts points (itself included) lie within distance eps; border if it is not core but lies within eps of a core point; noise otherwise.


Exercise 5: PCA (15 points)

Complete pca(X, k), returning the projected data Z (shape (n, k)) and the explained variance ratio of each of the kk components. Centre the data, compute the covariance matrix, use np.linalg.eigh, and sort the eigenvalues in decreasing order.

Question. How many components keep 80% of the digits’ variance? Compute it.


Exercise 6: An Autoencoder (15 points)

Complete make_autoencoder(k): an nn.Sequential from 64 inputs down to a code of k numbers and back to 64 outputs in [0,1][0, 1] (end with a Sigmoid), with at least one hidden ReLU layer on each side. The training loop is given. Target: with k=8k = 8, a lower test reconstruction error than PCA with 8 components.


Challenge: One EM Step (+10 points)

For a 1-D mixture of two Gaussians with weights pi, means mu and standard deviations sigma (arrays of length 2), complete em_step(x, pi, mu, sigma):

  • E-step: responsibilities rij=πjN(xi∣μj,σj)/∑lπlN(xi∣μl,σl)r_{ij} = \pi_j\mathcal{N}(x_i \mid \mu_j, \sigma_j) / \sum_l \pi_l\mathcal{N}(x_i \mid \mu_l, \sigma_l);

  • M-step: with Nj=∑irijN_j = \sum_i r_{ij}: πj=Nj/n\pi_j = N_j / n, μj=∑irijxi/Nj\mu_j = \sum_i r_{ij}x_i / N_j, σj=∑irij(xi−μj)2/Nj\sigma_j = \sqrt{\sum_i r_{ij}(x_i - \mu_j)^2 / N_j} (using the new μj\mu_j).

Return the new pi, mu, sigma.


Lab Report

Submit this notebook with every check cell showing ✅ and your answer to the question in Exercise 5.