Clustering Algorithms
Clustering partitions unlabeled data into groups (clusters) of similar instances.
Centroid-Based: K-Means
K-Means partitions data into clusters by minimizing the Within-Cluster Sum of Squares (Inertia):
Lloyd’s Algorithm
- Initialize centroids randomly (or using K-Means++).
- Assign each instance to its closest centroid: .
- Update centroids: .
- 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
MinPtsneighbors within a radius of . - Border Points: Not core points, but within 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:
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.
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 in K-Means is challenging. We use two main validation methods:
- The Elbow Method: Plotting inertia vs. 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.