DBSCAN vs Agglomerative Clustering: A Complete Deep Dive
Clustering is one of the most fundamental techniques in machine learning and data analysis. At its core, clustering tries to answer a simple but powerful question: "Which data points are similar to each other?"
Two widely used clustering techniques are DBSCAN and Agglomerative Clustering. While both aim to group similar data, they approach the problem in very different ways.
๐ Table of Contents
- Introduction
- DBSCAN Explained
- Agglomerative Clustering Explained
- Key Differences
- Mathematics Behind Clustering
- Code Examples
- CLI Outputs
- When to Use What
- Conclusion
Introduction
Clustering belongs to unsupervised learning, meaning there are no predefined labels. The algorithm must discover patterns on its own.
DBSCAN Explained
DBSCAN stands for Density-Based Spatial Clustering of Applications with Noise.
It groups points based on density. Areas with many nearby points form clusters, while sparse areas are treated as noise.
Core Concepts
- Epsilon (ฮต): Neighborhood radius
- MinPts: Minimum points to form a cluster
- Core Points: Dense region points
- Noise Points: Outliers
๐ Expand: Intuition Behind Density
Imagine standing in a crowded room. If many people are within arm's reach, you're in a dense area. If you're alone, you're noise.
Agglomerative Clustering Explained
Agglomerative clustering is a bottom-up hierarchical method.
Each data point starts as its own cluster. Gradually, clusters merge until only one remains.
Dendrogram
A dendrogram is a tree that shows how clusters merge.
๐ Expand: Why Hierarchical Clustering?
It allows you to choose clustering granularity later instead of fixing it upfront.
Key Differences
1. Approach
- DBSCAN → Density-based
- Agglomerative → Distance-based merging
2. Shape
- DBSCAN → Arbitrary shapes
- Agglomerative → Often spherical
3. Noise Handling
- DBSCAN → Handles noise explicitly
- Agglomerative → No built-in noise handling
4. Scalability
- DBSCAN → Efficient with indexing
- Agglomerative → Expensive for large datasets
Mathematics Behind Clustering
1. Distance Metric (Euclidean)
\[ d(x, y) = \sqrt{\sum_{i=1}^{n}(x_i - y_i)^2} \]
This measures similarity between points.
2. Density Condition (DBSCAN)
\[ |N_\epsilon(p)| \geq MinPts \]
A point is a core point if enough neighbors exist.
3. Linkage Criteria
Single Linkage:
\[ d(A,B) = \min_{a \in A, b \in B} d(a,b) \]
Complete Linkage:
\[ d(A,B) = \max_{a \in A, b \in B} d(a,b) \]
Average Linkage:
\[ d(A,B) = \frac{1}{|A||B|} \sum d(a,b) \]
๐ Expand: Why Different Linkages?
Each linkage changes cluster shape and sensitivity to noise.
Code Example
from sklearn.cluster import DBSCAN, AgglomerativeClustering # DBSCAN dbscan = DBSCAN(eps=0.5, min_samples=5) db_labels = dbscan.fit_predict(X) # Agglomerative agg = AgglomerativeClustering(n_clusters=3) agg_labels = agg.fit_predict(X)
CLI Output
$ python clustering.py Running DBSCAN... Clusters found: 4 Noise points: 12 Running Agglomerative... Clusters formed: 3 Done.
When to Use What
Use DBSCAN When:
- Data has noise
- Clusters are irregular
- Density varies
Use Agglomerative When:
- You need hierarchy
- Clusters are well-defined
- Dataset is small
- DBSCAN = Density + Noise Handling
- Agglomerative = Hierarchy + Structure
- Choose based on data shape and size
Conclusion
Both DBSCAN and Agglomerative clustering are powerful, but they serve different purposes.
DBSCAN excels in noisy, complex datasets, while Agglomerative clustering shines when hierarchical insights are needed.
Understanding both gives you flexibility to tackle a wide variety of real-world problems.