Clustering Algorithms

Discover and understand AI algorithms at Programming Ocean Academy with clear explanations, equations, and illustrations for better learning.

Clustering Algorithms Explainer

K-Means Clustering

$$ \min_{C} \sum_{i=1}^k \sum_{x \in C_i} \|x - \mu_i\|^2 $$

  • C_i: The i-th cluster containing a subset of data points.
  • mu_i: Centroid (mean) of cluster i.
  • Objective: Minimize the total intra-cluster variance (sum of squared distances).
  • Efficiency: Scales well to large datasets and is computationally efficient.
  • Assumptions: Clusters are spherical and similar in size; k must be chosen in advance.
  • Use Case: Customer segmentation, document clustering, image compression.
K-means_convergence.gif

K-Medoids (PAM)

$$ \min_{M} \sum_{i=1}^k \sum_{x \in C_i} d(x, m_i) $$

  • M: Set of medoids, each representing the center of a cluster.
  • m_i: Medoid (representative data point) of cluster i.
  • d(x, m_i): Distance (e.g., Manhattan, Euclidean) from point x to its medoid.
  • Robustness: Less sensitive to outliers compared to K-Means since it uses actual data points as centers.
  • Flexibility: Works with arbitrary distance metrics, not just Euclidean.
  • Use Case: Market segmentation, biological data clustering, scenarios with noisy or non-convex data.
K-medoids_example.png

Mini-Batch K-Means

$$ \min_{C} \sum_{i=1}^k \sum_{x \in C_i} \|x - \mu_i\|^2 \quad \text{(using mini-batches)} $$

  • C_i: The i-th cluster updated using a subset (mini-batch) of data points.
  • mu_i: Cluster centroid updated incrementally with each mini-batch.
  • Efficiency: Processes small random subsets of the data at a time to reduce computation.
  • Speed: Faster convergence on large-scale datasets compared to standard K-Means.
  • Memory: Suitable for streaming or memory-constrained environments.
  • Use Case: Real-time clustering, large-scale web analytics, online recommendation systems.
Kmeans_mini_batch_convergence.gif

DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

$$ \text{Core Point: } |N_\epsilon(x)| \geq \text{minPts} $$

  • Core Point: A point with at least minPts neighbors within radius epsilon (ε).
  • epsilon (ε): Distance threshold to define a neighborhood around a point.
  • minPts: Minimum number of points required to form a dense region.
  • Clustering: Connects dense regions to form clusters and labels low-density regions as noise.
  • Flexibility: Can find clusters of arbitrary shape and size.
  • Use Case: Spatial data, anomaly detection, customer behavior patterns with noise.
DBSCAN-Illustration.svg

OPTICS & HDBSCAN

$$ \text{Reachability}(p) = \max(\text{core-distance}(o), \text{distance}(o, p)) $$

  • OPTICS: Orders data points to create a reachability plot, capturing clustering structure at different density levels.
  • HDBSCAN: Builds a hierarchy of clusters and extracts a flat clustering based on stability over density levels.
  • Reachability distance: Measures how reachable a point is from a core point based on density.
  • Adaptability: Unlike DBSCAN, these methods handle clusters with varying densities more robustly.
  • Hierarchical Output: Generates a dendrogram-like structure with automatic selection of stable clusters.
  • Use Case: Customer segmentation with variable density, noisy scientific datasets, biological clustering.
OPTICS-density-based-clustering.svg

Agglomerative Hierarchical Clustering

$$ d(C_i, C_j) = \min_{x \in C_i, y \in C_j} d(x, y) \quad \text{(single linkage)} $$

  • Start: Treats each data point as a separate cluster.
  • Merge: Iteratively combines the closest pair of clusters based on a linkage criterion.
  • Linkage Options: Single, complete, average, and Ward’s method define how inter-cluster distance is measured.
  • Dendrogram: Produces a tree diagram to visualize cluster merging hierarchy.
  • Flexibility: Allows cluster selection at different levels of granularity by cutting the dendrogram.
  • Use Case: Taxonomy building, gene expression analysis, customer persona discovery.
Hierarchical_clustering_simple_diagram.svg

BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies)

$$ \text{CF} = (N, LS, SS) $$

  • CF (Clustering Feature): Compact representation of a subcluster: number of points (N), linear sum (LS), and squared sum (SS).
  • CF-Tree: Hierarchical tree structure that incrementally builds and refines clusters from streaming data.
  • Scalability: Designed to handle very large datasets efficiently in limited memory.
  • Speed: Performs incremental clustering in a single scan with optional refinement steps.
  • Noise Handling: Efficiently detects and removes outliers through thresholding mechanisms.
  • Use Case: Large-scale log data, sensor data, network monitoring, and other memory-intensive tasks.
BIRCH_CF_tree_example.svg

Gaussian Mixture Models (GMM) / Expectation-Maximization

$$ p(x) = \sum_{i=1}^k \pi_i \mathcal{N}(x | \mu_i, \Sigma_i) $$

  • pi_i: Weight of the i-th Gaussian component (mixing coefficient).
  • mu_i, Sigma_i: Mean and covariance of the i-th Gaussian.
  • Soft Clustering: Assigns probabilities of belonging to each cluster instead of hard labels.
  • EM Algorithm: Alternates between estimating cluster membership (E-step) and updating parameters (M-step).
  • Overlap: Suitable for data where clusters may overlap in feature space.
  • Use Case: Speaker recognition, customer behavior modeling, bioinformatics, and anomaly detection.
Gaussian-mixture.svg

Mean Shift Clustering

$$ m(x) = \frac{\sum_{x_i \in N(x)} K(x_i - x) x_i}{\sum_{x_i \in N(x)} K(x_i - x)} $$

  • m(x): Mean shift vector pointing toward higher data density.
  • K: Kernel function (e.g., Gaussian) that weights neighboring points.
  • N(x): Neighborhood of point x within a defined bandwidth.
  • Nonparametric: Does not assume a predefined number or shape of clusters.
  • Convergence: Iteratively moves points toward the nearest mode (density peak).
  • Use Case: Image segmentation, object tracking, peak detection in data distributions.
Mean_shift_clustering.gif

Affinity Propagation

$$ s(i, k) = \text{similarity between point } i \text{ and exemplar } k $$

  • Similarity Matrix: Measures how well-suited each point is to be another's exemplar.
  • Messages: Updates two matrices — responsibilities and availabilities — that guide cluster formation.
  • Exemplars: Data points selected to represent each cluster based on mutual preference.
  • Automatic k: The number of clusters is determined automatically based on input preferences.
  • Input: Requires a similarity function rather than a distance threshold or number of clusters.
  • Use Case: Text clustering, genomics, and recommendation systems where exemplar-based grouping is useful.
Affinity_propagation_example.png

Spectral Clustering

$$ L = D - W, \quad \text{eigendecompose } L \text{ or } L_{\text{norm}} = D^{-1/2}LD^{-1/2} $$

  • W: Similarity matrix encoding pairwise relationships between data points.
  • D: Degree matrix where each diagonal entry is the sum of the corresponding row in W.
  • L: Graph Laplacian used to capture the structure of the similarity graph.
  • Eigenvectors: Top eigenvectors of L form a new representation for clustering (typically via K-Means).
  • Non-Convex Clusters: Can detect complex cluster shapes that traditional methods might miss.
  • Use Case: Image segmentation, social network analysis, grouping non-linearly separable data.
Spectral_clustering_example.svg