Course contentsShow
← Machine Learning and Deep Learning
Lesson 354 of 3,5388. Unsupervised Learning: ClusteringFree lesson

Implementing and Evaluating Density-Based Clustering

Using scikit-learn's DBSCAN and HDBSCAN; interpreting results and choosing between density-based methods.

Implementing and Evaluating Density-Based Clustering

You'll learn how to apply DBSCAN and HDBSCAN in scikit-learn and interpret their results to choose the right density-based method for your data.

Putting Density Clustering Into Practice

Now that you understand the theory behind DBSCAN and HDBSCAN, it's time to use them. Both algorithms are available in scikit-learn, making implementation straightforward. The key difference: DBSCAN requires you to set epsilon and MinPts, while HDBSCAN automatically adapts to varying densities.

Using Scikit-learn's DBSCAN

from sklearn.cluster import DBSCAN

# Initialize with your chosen parameters
dbscan = DBSCAN(eps=0.5, min_samples=5)
labels = dbscan.fit_predict(X)

# Points labeled -1 are noise/outliers
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)

Using HDBSCAN

import hdbscan

# HDBSCAN auto-determines number of clusters
clusterer = hdbscan.HDBSCAN(min_samples=5)
labels = clusterer.fit_predict(X)

Interpreting Results and Making Choices

Noise points are labeled -1—these are outliers in regions too sparse to form clusters. This is actually a feature, not a bug!

Choose DBSCAN when:

  • You have relatively uniform density across your dataset
  • You can tune epsilon and MinPts through domain knowledge
  • Speed is critical (DBSCAN is faster)

Choose HDBSCAN when:

  • Your data has varying densities (common in real-world data)
  • You want more automation in parameter selection
  • You need hierarchical cluster relationships

Evaluate using Silhouette Score (excluding noise points) or visualize the clusters directly. Unlike K-Means, you don't force every point into a cluster—sometimes being labeled "noise" is the right answer!

Key Takeaway: DBSCAN gives you control with manual parameters, while HDBSCAN offers flexibility for varying densities; both identify noise naturally, which K-Means cannot do.