Skip to main content
AI & Machine Learning•Last Updated : 09 Oct 2026
~40 min read

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.

Definition: Unsupervised Learning

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.

TEACHER ANALOGYSupervised = Teacher gives marks; Unsupervised = No teacher, discover patterns yourself

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.

Note

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

d(p, q) = sqrt( sum_{i=1}^n (p_i - q_i)^2 ) mu_k = (1 / |C_k|) * sum_{x in C_k} x
Variable Definitions & Term Breakdown:
d(p, q)Euclidean distance between point p and centroid q
mu_kNew center of cluster k (average of all x assigned to cluster k)
|C_k|Total count of points belonging to cluster k

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

Example: K-Means Iteration Tracing

Calculate distances to initial centroids C1(2,1) and C2(5,4), assign points, and compute updated centroids.

  1. Iteration 1 Assignment: P1, P2, P3, P4 are closest to C1 -> Cluster 1. P5, P6, P7, P8 are closest to C2 -> Cluster 2.
  2. Iteration 1 Centroid Update: New C1 = ((1+1+2+2)/4, (1+2+1+2)/4) = (1.5, 1.5).
  3. Iteration 1 Centroid Update: New C2 = ((4+5+5+6)/4, (3+4+5+5)/4) = (5.0, 4.25).
  4. Iteration 2 Re-assignment: All points retain identical cluster assignments with new centroids (1.5, 1.5) and (5.0, 4.25).
  5. Convergence Reached: No points change clusters. Algorithm terminates in 2 iterations!

Interactive Quiz: K-Means & Clustering

Suggested Quiz
Question 1 of 3

Is K-Means guaranteed to converge to a solution in a finite number of iterations?

Python Implementation: K-Means from Scratch

PythonK-Means Vectorized Implementation
1import numpy as np
2
3def kmeans(X, k=2, max_iters=100):
4 # 1. Initialize centroids randomly
5 np.random.seed(42)
6 centroids = X[np.random.choice(len(X), k, replace=False)].copy()
7
8 for i in range(max_iters):
9 # 2. Cluster Assignment: find nearest centroid for each point
10 distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)
11 labels = np.argmin(distances, axis=1)
12
13 # 3. Move Centroid: mean of assigned points
14 new_centroids = np.array([X[labels == j].mean(axis=0) for j in range(k)])
15
16 # 4. Convergence check
17 if np.allclose(centroids, new_centroids):
18 print(f'Converged cleanly in {i+1} iterations!')
19 break
20 centroids = new_centroids
21
22 return centroids, labels
23
24# 8 points from tutorial
25points = np.array([
26 [1,1],[1,2],[2,1],[2,2],
27 [4,3],[5,4],[5,5],[6,5]
28], dtype=float)
29
30centroids, labels = kmeans(points, k=2)
31print("Final Centroid 0:", centroids[0])
32print("Final Centroid 1:", centroids[1])
33print("Cluster Assignments:", labels)
Output
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]
Article Tags:#Machine Learning#Unsupervised Learning#K-Means#Clustering#PCA
Was this lesson clear and helpful?

Discussion (0)

Sign in to ask questions and reply.
Loading discussion…
Discuss in community