Unsupervised Machine Learning
Discover latent structure without labels: K-Means clustering, Voronoi partitions, WCSS inertia metrics, the Elbow method, and Principal Component Analysis (PCA).
Unsupervised vs Supervised Learning
Unsupervised learning discovers intrinsic groupings and latent structural patterns in unlabeled data instances.
Learning from an M x N data matrix without target labels Y. The objective is to partition data or project high-dimensional data into meaningful clusters.
In unsupervised clustering, objects are grouped purely on internal similarity and inter-group dissimilarity.
What is Clustering?
Clustering partitions a population of data points into K groups such that intra-cluster points have high similarity and inter-cluster points have high dissimilarity.
Partitioning methods (K-Means) produce flat non-overlapping groups. Hierarchical methods build a nested dendrogram tree.
The K-Means Algorithm (MacQueen, 1967)
K-Means iteratively minimizes within-cluster variance by alternating between two fundamental steps: Cluster Assignment and Centroid Update.
Distance & Centroid Update Formulas
Mathematical definitions for Euclidean distance assignment and cluster mean updates.
Euclidean Distance & Centroid Mean
Three Required Properties of a Distance Metric
Any valid metric D(A, B) in clustering must strictly satisfy three mathematical properties.
Full Numerical 2D Worked Walkthrough (8 Points, K=2)
Given points: P1(1,1), P2(1,2), P3(2,1), P4(2,2), P5(4,3), P6(5,4), P7(5,5), P8(6,5) with Initial Centroids C1(2,1) and C2(5,4).
Calculate distances to initial centroids C1(2,1) and C2(5,4), assign points, and compute updated centroids.
- Iteration 1 Assignment: P1, P2, P3, P4 are closest to C1 -> Cluster 1. P5, P6, P7, P8 are closest to C2 -> Cluster 2.
- Iteration 1 Centroid Update: New C1 = ((1+1+2+2)/4, (1+2+1+2)/4) = (1.5, 1.5).
- Iteration 1 Centroid Update: New C2 = ((4+5+5+6)/4, (3+4+5+5)/4) = (5.0, 4.25).
- Iteration 2 Re-assignment: All points retain identical cluster assignments with new centroids (1.5, 1.5) and (5.0, 4.25).
- Convergence Reached: No points change clusters. Algorithm terminates in 2 iterations!
Interactive Quiz: K-Means & Clustering
Is K-Means guaranteed to converge to a solution in a finite number of iterations?
Python Implementation: K-Means from Scratch
1import numpy as np23def kmeans(X, k=2, max_iters=100):4 # 1. Initialize centroids randomly5 np.random.seed(42)6 centroids = X[np.random.choice(len(X), k, replace=False)].copy()78 for i in range(max_iters):9 # 2. Cluster Assignment: find nearest centroid for each point10 distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)11 labels = np.argmin(distances, axis=1)1213 # 3. Move Centroid: mean of assigned points14 new_centroids = np.array([X[labels == j].mean(axis=0) for j in range(k)])1516 # 4. Convergence check17 if np.allclose(centroids, new_centroids):18 print(f'Converged cleanly in {i+1} iterations!')19 break20 centroids = new_centroids2122 return centroids, labels2324# 8 points from tutorial25points = np.array([26 [1,1],[1,2],[2,1],[2,2],27 [4,3],[5,4],[5,5],[6,5]28], dtype=float)2930centroids, labels = kmeans(points, k=2)31print("Final Centroid 0:", centroids[0])32print("Final Centroid 1:", centroids[1])33print("Cluster Assignments:", labels)
Converged cleanly in 2 iterations! Final Centroid 0: [1.5 1.5] Final Centroid 1: [5. 4.25] Cluster Assignments: [0 0 0 0 1 1 1 1]