CLASSICAL ML Β· INTERVIEW QUESTION
Compare K-Means, DBSCAN, and hierarchical clustering. When would you use each?
π‘ intermediate Β· Unsupervised Learning
Hint: Think about cluster shape assumptions, scalability, and hyperparameters.
Answer
Clustering algorithms differ in their assumptions about cluster shape, scalability, and hyperparameters.
Comparison
| Algorithm | Cluster Shape | Needs K? | Handles Outliers | Complexity |
|---|---|---|---|---|
| K-Means | Roughly spherical | β Yes | β No | for t Lloyd iterations |
| DBSCAN | Density-connected | β No | β Yes | worst case; faster with effective spatial indexing |
| Hierarchical | Linkage-dependent | β No | β No | Linkage/implementation-dependent; often quadratic memory |
K-Means
Assumes spherical clusters, sensitive to initialization (use k-means++). Best for large datasets with roughly equal-sized clusters.
DBSCAN
Density-based β finds clusters of arbitrary shape. Requires (neighborhood radius) and . Naturally handles outliers as noise points.
Hierarchical
Produces dendrograms, captures multi-scale structure. No K needed upfront but memory.
python
from sklearn.cluster import KMeans, DBSCAN
# K-Means
kmeans = KMeans(n_clusters=5, init='k-means++', n_init=10)
labels_km = kmeans.fit_predict(X)
# DBSCAN
db = DBSCAN(eps=0.5, min_samples=5)
labels_db = db.fit_predict(X)Also consider GMMs for soft clustering and spectral clustering for non-convex shapes.
Related questions
- What is cluster analysis and what are its main applications?
- How do you determine the optimal number of clusters?
- Explain Gaussian Mixture Models (GMM). How do they differ from K-Means?
- What is overfitting? How do you detect and prevent it?
- Explain the difference between bagging and boosting. When would you use each?