Level 3 — ProductionUnsupervised Learning

Unsupervised Learning

45 minutes60 marksprintable — key stays hidden on paper

Difficulty: Level 3 (Production — from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60

Instructions: Answer all questions. Show full derivations. Where code is requested, pseudocode or NumPy-style code from memory is acceptable but must be logically complete.


Question 1 — K-Means from scratch (12 marks)

(a) State the K-Means objective function (within-cluster sum of squares) mathematically, defining every symbol. (3)

(b) K-Means alternates two steps. Derive, by taking the derivative of the objective and setting it to zero, why the optimal centroid for a fixed assignment is the mean of its assigned points. (4)

(c) Write from-scratch pseudocode (NumPy-style) for one full K-Means iteration (assignment + update) given data matrix X (n×d) and centroids C (k×d). (3)

(d) Explain out loud (2–3 sentences) why standard K-Means can converge to a poor local optimum and how K-Means++ initialization mitigates this. (2)


Question 2 — Choosing K (9 marks)

(a) Define the silhouette score for a single point ii, giving the formula in terms of a(i)a(i) and b(i)b(i), and state its range. (4)

(b) A point has mean intra-cluster distance a(i)=2.0a(i)=2.0 and mean nearest-cluster distance b(i)=5.0b(i)=5.0. Compute its silhouette value. (2)

(c) Contrast the elbow method and silhouette score as tools for choosing K — one strength/weakness each. (3)


Question 3 — DBSCAN (10 marks)

(a) Define the three point categories in DBSCAN (core, border, noise) in terms of parameters ε\varepsilon and minPts. (3)

(b) Given 1-D points {1,2,3,8,9,25}\{1, 2, 3, 8, 9, 25\} with ε=1.5\varepsilon = 1.5 and minPts = 2 (a point is a core point if it has at least minPts points within ε\varepsilon, including itself), classify every point and list the resulting clusters and noise. (5)

(c) State one advantage of DBSCAN over K-Means and one situation where DBSCAN struggles. (2)


Question 4 — PCA derivation & computation (14 marks)

(a) Derive the first principal component as the direction ww (with w=1\|w\|=1) that maximizes the variance of the projected data. Set up the Lagrangian and show the solution is an eigenvector of the covariance matrix. (5)

(b) For the covariance matrix Σ=(4224)\Sigma = \begin{pmatrix} 4 & 2 \\ 2 & 4 \end{pmatrix} compute both eigenvalues and the (unit) first principal component direction. (5)

(c) Compute the fraction of variance explained by the first principal component from part (b). (2)

(d) Briefly relate PCA-via-eigendecomposition to PCA-via-SVD: given centered data XX, what SVD quantity gives the principal directions? (2)


Question 5 — Gaussian Mixture Models / EM (9 marks)

(a) Write the log-likelihood of a GMM with KK components for data {x1,,xn}\{x_1,\dots,x_n\}, defining mixing weights πk\pi_k and component densities. (3)

(b) Write the E-step formula: the responsibility γik\gamma_{ik} that component kk takes for point xix_i. (3)

(c) Explain out loud why we use EM rather than directly maximizing the log-likelihood by differentiation. (3)


Question 6 — t-SNE vs UMAP (6 marks)

(a) Explain what t-SNE preserves in its embedding and one reason why inter-cluster distances in a t-SNE plot are unreliable. (3)

(b) Give two practical advantages of UMAP over t-SNE. (3)


Answer keyMark scheme & solutions

Question 1

(a) [3 marks] J=k=1KiCkxiμk2J = \sum_{k=1}^{K}\sum_{i \in C_k} \|x_i - \mu_k\|^2 where xix_i is data point, CkC_k is the set of points assigned to cluster kk, μk\mu_k is the centroid of cluster kk. (1 mark formula, 1 mark symbols, 1 mark stating it is WCSS to be minimized.)

(b) [4 marks] Fix assignments. For cluster kk, minimize Jk=iCkxiμk2J_k=\sum_{i\in C_k}\|x_i-\mu_k\|^2. Take gradient w.r.t. μk\mu_k: μkJk=iCk2(xiμk)=0\nabla_{\mu_k} J_k = \sum_{i\in C_k} -2(x_i-\mu_k) = 0 (2 marks) iCkxi=Ckμkμk=1CkiCkxi\Rightarrow \sum_{i\in C_k}x_i = |C_k|\,\mu_k \Rightarrow \mu_k = \frac{1}{|C_k|}\sum_{i\in C_k}x_i (2 marks) So optimal centroid = mean. (Second derivative positive ⇒ minimum.)

(c) [3 marks]

# Assignment step
dists = ((X[:,None,:] - C[None,:,:])**2).sum(axis=2)  # n×k
labels = dists.argmin(axis=1)                          # n
# Update step
for j in range(k):
    if (labels==j).any():
        C[j] = X[labels==j].mean(axis=0)

(2 marks assignment, 1 mark update; handle empty-cluster acceptable.)

(d) [2 marks] Random init can seed multiple centroids in the same region, leaving others unoccupied → bad local minimum (1). K-Means++ picks initial centroids with probability proportional to squared distance from nearest chosen centroid, spreading them out and giving provably better expected WCSS (1).


Question 2

(a) [4 marks] s(i)=b(i)a(i)max(a(i),b(i))s(i) = \frac{b(i)-a(i)}{\max(a(i),b(i))} (2) a(i)a(i) = mean distance from ii to other points in its own cluster; b(i)b(i) = lowest mean distance from ii to points of any other cluster (1). Range [1,1][-1,1] (1).

(b) [2 marks] s=5.02.0max(2.0,5.0)=3.05.0=0.6s = \frac{5.0-2.0}{\max(2.0,5.0)} = \frac{3.0}{5.0} = 0.6

(c) [3 marks]

  • Elbow: plot WCSS vs K, choose "elbow"; simple/cheap but the elbow is often ambiguous/subjective (1.5).
  • Silhouette: measures cohesion vs separation, gives clear numeric optimum; but O(n2)O(n^2) distance computation is expensive on large data (1.5).

Question 3

(a) [3 marks]

  • Core point: has ≥ minPts points within radius ε\varepsilon (1).
  • Border point: not core, but within ε\varepsilon of a core point (1).
  • Noise: neither core nor border (1).

(b) [5 marks] Neighbourhoods (points within 1.5, incl. self):

  • 1: {1,2} → 2 ≥ 2 → core
  • 2: {1,2,3} → 3 → core
  • 3: {2,3} → 2 → core
  • 8: {8,9} → 2 → core
  • 9: {8,9} → 2 → core
  • 25: {25} → 1 < 2 → noise

Clusters: {1,2,3} and {8,9}; 25 = noise. (3 for classification, 2 for cluster grouping.)

(c) [2 marks] Advantage: finds arbitrary-shaped clusters and detects outliers, no need to specify K (1). Struggles: clusters of varying density / high-dimensional data where a single ε\varepsilon fails (1).


Question 4

(a) [5 marks] Projected variance = wΣww^\top \Sigma w with Σ\Sigma the covariance matrix. Maximize subject to ww=1w^\top w=1: L=wΣwλ(ww1)\mathcal{L}=w^\top\Sigma w - \lambda(w^\top w -1) (2) Lw=2Σw2λw=0Σw=λw\frac{\partial\mathcal{L}}{\partial w}=2\Sigma w - 2\lambda w = 0 \Rightarrow \Sigma w = \lambda w (2) So ww is an eigenvector of Σ\Sigma; variance = wΣw=λw^\top\Sigma w=\lambda, maximized by largest eigenvalue (1).

(b) [5 marks] Characteristic eqn: (4λ)24=0(4λ)=±2(4-\lambda)^2-4=0 \Rightarrow (4-\lambda)=\pm2. Eigenvalues: λ1=6, λ2=2\lambda_1=6,\ \lambda_2=2 (2). For λ=6\lambda=6: (46)w1+2w2=0w1=w2(4-6)w_1+2w_2=0 \Rightarrow w_1=w_2. Unit vector: w=12(1,1)w=\tfrac{1}{\sqrt2}(1,1)^\top (3).

(c) [2 marks] λ1λ1+λ2=66+2=68=0.75\frac{\lambda_1}{\lambda_1+\lambda_2}=\frac{6}{6+2}=\frac{6}{8}=0.75

(d) [2 marks] For centered X=UΣSVDVX=U\Sigma_{SVD}V^\top, the columns of VV (right singular vectors) are the principal directions; singular values relate to eigenvalues by λi=σi2/(n1)\lambda_i=\sigma_i^2/(n-1).


Question 5

(a) [3 marks] logL=i=1nlog(k=1KπkN(xiμk,Σk))\log L = \sum_{i=1}^{n}\log\left(\sum_{k=1}^{K}\pi_k\,\mathcal{N}(x_i\mid\mu_k,\Sigma_k)\right) πk0, kπk=1\pi_k\ge0,\ \sum_k\pi_k=1 mixing weights; N\mathcal{N} Gaussian density. (Formula 2, definitions 1.)

(b) [3 marks] γik=πkN(xiμk,Σk)j=1KπjN(xiμj,Σj)\gamma_{ik}=\frac{\pi_k\,\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{\sum_{j=1}^{K}\pi_j\,\mathcal{N}(x_i\mid\mu_j,\Sigma_j)}

(c) [3 marks] The log-likelihood contains a log-of-sum, so setting derivatives to zero gives coupled equations with no closed form (latent assignments unknown) (1.5). EM introduces responsibilities and optimizes a tractable lower bound (Jensen), guaranteeing monotonic increase of likelihood and closed-form M-step updates (1.5).


Question 6

(a) [3 marks] t-SNE preserves local neighbourhood structure — nearby points stay near (1.5). Inter-cluster distances/global geometry are unreliable because t-SNE optimizes a KL divergence on local probabilities and expands dense regions/shrinks sparse ones; cluster sizes and gaps are not meaningful (1.5).

(b) [3 marks] Any two of: (i) faster and scales better to large data; (ii) better preserves global structure; (iii) can transform new points (has a learned mapping); (iv) fewer perplexity-tuning sensitivities. (1.5 each.)

[
{"claim":"Silhouette (5-2)/max(2,5)=0.6","code":"a=2.0;b=5.0;result=abs((b-a)/max(a,b)-0.6)<1e-9"},
{"claim":"Eigenvalues of [[4,2],[2,4]] are 6 and 2","code":"M=Matrix([[4,2],[2,4]]);ev=sorted([complex(e).real for e in M.eigenvals().keys()]);result=ev==[2.0,6.0]"},
{"claim":"First PC direction (1,1)/sqrt2 is eigenvector with eigenvalue 6","code":"M=Matrix([[4,2],[2,4]]);w=Matrix([1,1])/sqrt(2);result=simplify(M*w-6*w)==Matrix([0,0])"},
{"claim":"Explained variance fraction of PC1 = 0.75","code":"result=Rational(6,6+2)==Rational(3,4)"}
]