Unsupervised Learning
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 , giving the formula in terms of and , and state its range. (4)
(b) A point has mean intra-cluster distance and mean nearest-cluster distance . 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 and minPts. (3)
(b) Given 1-D points with and minPts = 2 (a point is a core point if it has at least minPts points within , 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 (with ) 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 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 , 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 components for data , defining mixing weights and component densities. (3)
(b) Write the E-step formula: the responsibility that component takes for point . (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] where is data point, is the set of points assigned to cluster , is the centroid of cluster . (1 mark formula, 1 mark symbols, 1 mark stating it is WCSS to be minimized.)
(b) [4 marks] Fix assignments. For cluster , minimize . Take gradient w.r.t. : (2 marks) (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] (2) = mean distance from to other points in its own cluster; = lowest mean distance from to points of any other cluster (1). Range (1).
(b) [2 marks]
(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 distance computation is expensive on large data (1.5).
Question 3
(a) [3 marks]
- Core point: has ≥ minPts points within radius (1).
- Border point: not core, but within 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 fails (1).
Question 4
(a) [5 marks] Projected variance = with the covariance matrix. Maximize subject to : (2) (2) So is an eigenvector of ; variance = , maximized by largest eigenvalue (1).
(b) [5 marks] Characteristic eqn: . Eigenvalues: (2). For : . Unit vector: (3).
(c) [2 marks]
(d) [2 marks] For centered , the columns of (right singular vectors) are the principal directions; singular values relate to eigenvalues by .
Question 5
(a) [3 marks] mixing weights; Gaussian density. (Formula 2, definitions 1.)
(b) [3 marks]
(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)"}
]