Skip to content

00-DBSCAN

Updated
17 Sept 2026
1 min read
5 sections
Page 106 of 117
Python & Machine Learning

Introduction to DBSCAN

Let's briefly explore visually the differences between DBSCAN and other clustering techniques, such as K-Means Clustering.

DBSCAN and Clustering Examples

python
blobs = pd.read_csv('../DATA/cluster_blobs.csv')
python
blobs.head()
Out · table
X1
X2
0
4.645333
6.822294
12 more lines
1
4.784032
6.422883
2
-5.851786
5.774331
3
-7.459592
6.456415
4
4.918911
6.961479
python
sns.scatterplot(data=blobs,x='X1',y='X2')
Out · result
<AxesSubplot:xlabel='X1', ylabel='X2'>
Out · plot
Output 1
python
moons = pd.read_csv('../DATA/cluster_moons.csv')
python
moons.head()
Out · table
X1
X2
0
0.674362
-0.444625
12 more lines
1
1.547129
-0.239796
2
1.601930
-0.230792
3
0.014563
0.449752
4
1.503476
-0.389164
python
sns.scatterplot(data=moons,x='X1',y='X2')
Out · result
<AxesSubplot:xlabel='X1', ylabel='X2'>
Out · plot
Output 2
python
circles = pd.read_csv('../DATA/cluster_circles.csv')
python
circles.head()
Out · table
X1
X2
0
-0.348677
0.010157
12 more lines
1
-0.176587
-0.954283
2
0.301703
-0.113045
3
-0.782889
-0.719468
4
-0.733280
-0.757354
python
sns.scatterplot(data=circles,x='X1',y='X2')
Out · result
<AxesSubplot:xlabel='X1', ylabel='X2'>
Out · plot
Output 3

Label Discovery

python
def display_categories(model,data):
    labels = model.fit_predict(data)
    sns.scatterplot(data=data,x='X1',y='X2',hue=labels,palette='Set1')

Kmeans Results

python
from sklearn.cluster import KMeans
model = KMeans(n_clusters = 2)
python
display_categories(model,moons)
Out · plot
Output 4
python
model = KMeans(n_clusters = 3)
display_categories(model,blobs)
Out · plot
Output 5
python
model = KMeans(n_clusters = 2)
display_categories(model,circles)
Out · plot
Output 6

DBSCAN Results

python
from sklearn.cluster import DBSCAN
python
model = DBSCAN(eps=0.6)
python
display_categories(model,blobs)
Out · plot
Output 7
python
model = DBSCAN(eps=0.15)
plt.figure(figsize=(10,6),dpi=150)
display_categories(model,moons)
Out · plot
Output 8
python
display_categories(model,circles)
Out · plot
Output 9

Let's further explore DBSCAN Hyperparameters!