Unit 4: Clustering Techniques

Syllabus

4.1 Introduction to Clustering

Clustering is an unsupervised learning technique that groups similar data points together without predefined labels. Unlike classification (supervised), clustering discovers natural structures in data.

Supervised vs Unsupervised Learning

Types of Clustering

Key Concepts:
  • Distance/Similarity Measure: Euclidean distance, Manhattan distance, Cosine similarity — determines how "close" points are.
  • Centroid: The center of a cluster (mean of all points in K-Means).
  • Cluster Validation: Internal (Silhouette, SSE) and external (known labels) metrics to evaluate cluster quality.
1
Clustering Techniques Overview
graph TD CLUSTER[Clustering Techniques] --> PART[Partitioning
Divide into k groups] CLUSTER --> HIER[Hierarchical
Tree of nested clusters] CLUSTER --> DENS[Density-Based
High-density regions] CLUSTER --> MOD[Model-Based
Statistical models] PART --> P1[K-Means
Minimizes SSE] PART --> P2[K-Medoids
Uses actual points] PART --> P3[Gaussian Mixture
Probabilistic] HIER --> H1[Agglomerative
Bottom-up merge] HIER --> H2[Divisive
Top-down split] DENS --> D1[DBSCAN
Epsilon + MinPts] DENS --> D2[OPTICS
Ordered density] MOD --> M1[GMM + EM
Expectation Maximization] classDef decision fill:#d97706,stroke:#f59e0b,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0891b2,stroke:#22d3ee,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold class CLUSTER decision class PART,HIER,DENS,MOD process class P1,P2,P3,H1,H2,D1,D2,M1 success
Note: Partitioning methods require specifying k in advance. Hierarchical methods produce dendrograms. Density-based methods handle arbitrary shapes and noise.

4.2 K-Means Clustering

K-Means is the most widely used partitioning algorithm. It partitions n data points into k clusters by minimizing the within-cluster sum of squares (SSE).

Algorithm Steps

  1. Initialize: Choose k initial centroids randomly (or use K-Means++).
  2. Assign: Assign each data point to the nearest centroid (using Euclidean distance).
  3. Update: Recalculate centroids as the mean of all points assigned to each cluster.
  4. Repeat steps 2-3 until centroids stabilize (convergence) or max iterations reached.
K-Means++ Initialization: Instead of random initialization, K-Means++ picks the first centroid randomly, then selects subsequent centroids proportional to their distance from existing centroids. This produces more balanced clusters and faster convergence.

Elbow Method

The Elbow Method helps determine the optimal k. Plot SSE vs k — the "elbow" where SSE stops decreasing significantly indicates the best k.

Sensitivity to Outliers

K-Means is sensitive to outliers because centroids are calculated as means. A single outlier can shift the centroid significantly. K-Medoids (PAM) is more robust — it uses actual data points as cluster centers.

Pros and Cons

2
K-Means Clustering Process
graph TD A[Start: Choose k] --> B[K-Means++
Initialize centroids] B --> C{Assign Points
To nearest centroid} C --> D[Update Centroids
Recalculate means] D --> E{Converged?
Centroids stable?} E -->|No| C E -->|Yes| F[Final Clusters
Assignments] F --> G[Evaluate
SSE, Silhouette] G --> H{Good Clusters?} H -->|No| I[Adjust k
Elbow Method] I --> A H -->|Yes| J[Done!
Use clusters] classDef decision fill:#d97706,stroke:#f59e0b,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0891b2,stroke:#22d3ee,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold class C,E,H decision class B,D,G,I process class A,J success class F warning
Note: K-Means converges quickly but may find local optima. Running multiple times with different initializations (n_init) helps find the best solution.

4.3 Hierarchical Clustering

Hierarchical clustering builds a tree of nested clusters (dendrogram). You can cut the tree at different levels to get different numbers of clusters.

Agglomerative (Bottom-Up)

  1. Start with each data point as its own cluster (n clusters).
  2. Find the two closest clusters and merge them.
  3. Update the distance matrix.
  4. Repeat until only one cluster remains (all points merged).

Divisive (Top-Down)

  1. Start with all points in one cluster.
  2. Split the most heterogeneous cluster into two.
  3. Repeat until each point is its own cluster.

Linkage Criteria

Linkage determines how the distance between two clusters is calculated:

Dendrogram Interpretation:
  • The height at which two clusters merge represents their distance.
  • A long vertical line before a merge indicates a natural cluster boundary.
  • Cut the dendrogram horizontally at a chosen height to get the desired number of clusters.
3
Hierarchical Clustering Dendrogram
graph TD ROOT[All Data
1 Cluster] --> CLUSTER_A[Cluster A] ROOT --> CLUSTER_B[Cluster B] CLUSTER_A --> A1[Point 1] CLUSTER_A --> A2[Cluster A1] A2 --> A2a[Point 2] A2 --> A2b[Point 3] CLUSTER_B --> B1[Point 4] CLUSTER_B --> B2[Cluster B1] B2 --> B2a[Point 5] B2 --> B2b[Point 6] classDef decision fill:#d97706,stroke:#f59e0b,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0891b2,stroke:#22d3ee,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold class ROOT decision class CLUSTER_A,CLUSTER_B,A2,B2 process class A1,A2a,A2b,B1,B2a,B2b success
Note: Cutting the dendrogram at different heights yields different cluster counts. The height represents the merge distance — larger heights mean more dissimilar clusters being merged.

4.4 DBSCAN

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) groups together closely packed points and marks outliers in low-density regions.

Core Concepts

Algorithm

  1. Pick an arbitrary unvisited point.
  2. If it has ≥ MinPts neighbors within ε, start a new cluster.
  3. Recursively add all density-reachable points to the cluster.
  4. If the point has < MinPts neighbors, mark it as noise (may later become border of another cluster).
  5. Repeat until all points are visited.
Advantages over K-Means:
  • Does not require specifying k in advance.
  • Finds arbitrary-shaped clusters (not just spherical).
  • Robust to outliers — noise points are explicitly identified.
  • Works well with differently-sized clusters.

Limitations

4.5 Cluster Evaluation

Evaluating clustering quality is crucial because there are no ground-truth labels in unsupervised learning.

Internal Evaluation Metrics

External Evaluation (When Labels Exist)

Evaluation Best Practices:
  • Use multiple metrics — no single metric tells the full story.
  • Visualize clusters when possible (2D/3D, or t-SNE/UMAP for high dimensions).
  • For the Elbow Method, look for the "elbow" where SSE decrease slows.
  • Silhouette > 0.5 generally indicates good clustering structure.
Unit 3 Activity Case Studies