Search Knowledge

© 2026 LIBREUNI PROJECT

Machine Learning / Unsupervised Learning

Clustering Algorithms

Clustering Algorithms

Clustering partitions unlabeled data into groups (clusters) of similar instances.

Centroid-Based: K-Means

K-Means partitions data into KK clusters by minimizing the Within-Cluster Sum of Squares (Inertia):

J=j=1KxCjxμj2J = \sum_{j=1}^{K} \sum_{x \in C_j} ||x - \mu_j||^2

Lloyd’s Algorithm

  1. Initialize KK centroids μj\mu_j randomly (or using K-Means++).
  2. Assign each instance to its closest centroid: c(i)=argminjx(i)μj2c^{(i)} = \arg\min_j ||x^{(i)} - \mu_j||^2.
  3. Update centroids: μj=1CjxCjx\mu_j = \frac{1}{|C_j|} \sum_{x \in C_j} x.
  4. Repeat steps 2-3 until convergence.

K-Means++ initialization spreads centroids far apart during initialization, reducing the risk of converging to sub-optimal local minima.

Density-Based: DBSCAN

DBSCAN groups points based on local density, classifying them into three categories:

  • Core Points: At least MinPts neighbors within a radius of ϵ\epsilon.
  • Border Points: Not core points, but within ϵ\epsilon of a core point.
  • Noise Points: Neither core nor border points.

DBSCAN detects the number of clusters automatically and handles outliers effectively by marking them as noise.

Hierarchical (Agglomerative) Clustering

Agglomerative clustering is a “bottom-up” approach. It begins by treating every single data point as its own cluster. It then iteratively merges the closest pairs of clusters until all points are merged into a single root cluster.

  • Dendrogram: This merging process is visualized as a tree-like diagram called a dendrogram. By analyzing the vertical height of the branches in the dendrogram, one can decide on an optimal number of clusters by cutting the tree horizontally.
  • WCSS: Within-Cluster Sum of Squares can also be used as a metric to evaluate cluster cohesion.

Linkage Criteria

The decision of which clusters to merge depends on the defined distance (linkage) between clusters:

  • Single Linkage: The distance between two clusters is defined by their two closest members.
  • Complete Linkage: The distance is defined by their two furthest members.
  • Average Linkage: The average distance between all points in the two clusters.
  • Ward’s Linkage: Merges clusters in a way that minimizes the total variance within all clusters.

Probabilistic: Gaussian Mixture Models

Gaussian Mixture Models (GMM) represent clusters as covariance ellipsoids rather than spheres, allowing for varying cluster shapes. GMM uses the Expectation-Maximization (EM) algorithm to assign “soft clustering” probabilities.

Example: Comparing Algorithms

The following example demonstrates clustering using scikit-learn:

python

Interactive Lab

Compare K-Means and DBSCAN clustering on non-linear crescent-shaped data. Observe how K-Means struggles with complex shapes while DBSCAN correctly groups them.

Step 1
Inspect the idea
Step 2
Edit the program
Step 3
Run and compare

Exercise

Test your understanding of clustering paradigms:

Which algorithm is most appropriate for a dataset where clusters have non-spherical, interlocking crescent shapes?

Selecting the Number of Clusters

Choosing the parameter KK in K-Means is challenging. We use two main validation methods:

  • The Elbow Method: Plotting inertia vs. KK and finding the point where the rate of decrease drops.
  • Silhouette Analysis: Computing the average silhouette coefficient for all points. A higher average coefficient indicates well-defined, dense clusters.

References & Further Reading