PythonPlaza - Python & AI

Density-Based Spatial Clustering of Applications with Noise (DBSCAN)


Density-Based Spatial Clustering of Applications with Noise (DBSCAN) is an unsupervised learning method. It groups data points that are close to each other while also identifying those data points that are in sparse regions. This method does not require the number of clusters to be known in advance, unlike the k-means method. This method can identify clusters of varying shapes, and outliers can also be identified.


The Two Fundamental Parameters

Max Distance (Epsilon): The greatest distance at which two locations can be regarded as neighbors.
Minimum Points (MinPts): The bare minimum of data points needed to create a dense zone within a (epsilon) radius


Let's consider the following data points:
(1,2), (2,2), (2,3), (3,2)
(8,7), (8,8), (7,8), (7,7)
(5,5)

Step 1: Pick a random point
Example: (2,2)

Step 2: Count neighbors within Epsilon 1.5 radius
Points near (2,2):
(1,2)
(2,3)
(3,2)
Total = 4 → ≥ MinPts (3)
so, (2,2) becomes a Core Point


Step 3: Expand Cluster
All density-reachable points from this core point form Cluster 1
Same process finds Cluster 2.
Final Clustering Result
Cluster 1:
(1,2), (2,2), (2,3), (3,2)
Cluster 2:
(8,7), (8,8), (7,8), (7,7)
Noise:
(5,5)

DBSCAN Customer Segmentation

Complete step-by-step calculation using 10 customers, 4 independent variables, ε = 2.0, and MinPts = 3.

1. Customer Data

We use the same four independent variables as the K-Means example:

CustomerIncomeSpendingPurchasesEngagement
C12222
C23223
C32322
C43333
C57777
C68787
C77878
C88888
C94949
C105859

2. DBSCAN Parameters

ε (Epsilon)

ε = 2.0

A customer is considered a neighbor when its Euclidean distance is ≤ 2.0.

MinPts

MinPts = 3

A point needs at least 3 points in its ε-neighborhood, including itself, to be a core point.

Important: Unlike K-Means, DBSCAN does not require us to specify the number of clusters in advance.

3. Euclidean Distance Formula

With four independent variables:

d(A,B) = √[(A₁-B₁)² + (A₂-B₂)² + (A₃-B₃)² + (A₄-B₄)²]

Example: Distance Between C1 and C2

C1 = (2,2,2,2)
C2 = (3,2,2,3)

d(C1,C2) = √[(2-3)² + (2-2)² + (2-2)² + (2-3)²]
= √[1 + 0 + 0 + 1]
= √2
= 1.414
Because 1.414 ≤ ε (2.0), C2 is in C1's ε-neighborhood.

4. Calculate Important Pairwise Distances

For DBSCAN, we are primarily interested in whether each distance is ≤ ε = 2.0.

PairDistance≤ 2.0?
C1 – C21.414Yes
C1 – C31.000Yes
C1 – C42.000Yes
C1 – C510.000No
C1 – C611.045No
C1 – C711.045No
C1 – C812.000No
C1 – C910.296No
C1 – C1010.149No
C2 – C31.414Yes
C2 – C41.414Yes
C3 – C41.414Yes
C5 – C61.414Yes
C5 – C71.414Yes
C5 – C82.000Yes
C6 – C72.000Yes
C6 – C81.414Yes
C7 – C81.414Yes
C9 – C101.732Yes
Only the relevant close pairs are shown in the table. Distances greater than 2.0 are treated as outside the ε-neighborhood.

5. Determine the ε-Neighborhood of Each Customer

The ε-neighborhood contains the customer itself plus every customer whose distance is ≤ 2.0.

Customerε-NeighborhoodNumber of PointsCore?
C1C1, C2, C3, C44Yes
C2C1, C2, C3, C44Yes
C3C1, C2, C3, C44Yes
C4C1, C2, C3, C44Yes
C5C5, C6, C7, C84Yes
C6C5, C6, C7, C84Yes
C7C5, C6, C7, C84Yes
C8C5, C6, C7, C84Yes
C9C9, C102No
C10C9, C102No

6. Core Point Calculation

DBSCAN defines a core point when:

Number of points in ε-neighborhood ≥ MinPts

Here:

ε = 2.0     MinPts = 3

Example: C1

Nε(C1) = {C1, C2, C3, C4}
|Nε(C1)| = 4

4 ≥ 3
Therefore, C1 is a CORE point.

Example: C9

Nε(C9) = {C9, C10}
|Nε(C9)| = 2

2 < 3
Therefore, C9 is NOT a core point.
C1 through C8 are core points. C9 and C10 are not core points.

7. Core, Border, and Noise Points

Core Points

C1–C8

Each has at least 3 points in its ε-neighborhood, including itself.

Border Points

None

A border point is not core itself but lies within ε of a core point.

Noise Points

C9, C10

Neither is core, and neither lies within ε = 2.0 of any core point.

Why are C9 and C10 noise? They are close to each other, but they form only a 2-point group. Since MinPts = 3, neither becomes a core point. They also are too far from C1–C8 to be border points.

8. How DBSCAN Forms the Clusters

Cluster 1

Start with C1.
C1 is a core point because it has 4 points in its ε-neighborhood.
Its neighbors are C1, C2, C3, C4.
C2, C3, and C4 are also core points.

Therefore: Cluster 1 = {C1, C2, C3, C4}

Cluster 2

Start with C5.
C5 is a core point because it has 4 points in its ε-neighborhood.
Its neighbors are C5, C6, C7, C8.
C6, C7, and C8 are also core points.

Therefore: Cluster 2 = {C5, C6, C7, C8}

C9 and C10

C9 is not core: only C9 and C10 are within ε.
C10 is not core: only C9 and C10 are within ε.
Neither is within ε of a core point.

Therefore: C9 and C10 = Noise

9. Final DBSCAN Result

CustomerIncomeSpendingPurchasesEngagementε NeighborsTypeCluster
C122224Core1
C232234Core1
C323224Core1
C433334Core1
C577774Core2
C687874Core2
C778784Core2
C888884Core2
C949492Noise-1
C1058592Noise-1
In common scikit-learn DBSCAN output, -1 is the label used for noise points.

10. Business Interpretation

Cluster 1 — Low-Value Customers

C1–C4

These customers have relatively low income, low spending, few purchases, and low engagement.

Possible actions:

  • Introductory offers
  • Discounts
  • Cross-selling
  • Engagement campaigns
  • Loyalty incentives

Cluster 2 — High-Value Customers

C5–C8

These customers have high income, high spending, frequent purchases, and high engagement.

Possible actions:

  • VIP programs
  • Premium products
  • Exclusive offers
  • Early access
  • Personalized recommendations

Noise — Potentially Unusual Customers

C9, C10

These customers spend heavily and are highly engaged, but they do not have enough nearby customers to form a dense DBSCAN cluster under the chosen parameters.

Possible actions:

  • Investigate individually
  • Personalized marketing
  • Upselling opportunities
  • Check for unusual purchasing behavior

11. DBSCAN vs. K-Means

FeatureK-MeansDBSCAN
Number of clusters required?Yes — KNo
Main parametersKε and MinPts
Uses centroids?YesNo
Handles noise/outliers?PoorlyYes
Cluster shapeGenerally sphericalArbitrary shapes
Distance conceptDistance to centroidDensity/neighborhood

12. Complete DBSCAN Mathematical Summary

Step 1 — Data

X = [(2,2,2,2), (3,2,2,3), (2,3,2,2), (3,3,3,3),
(7,7,7,7), (8,7,8,7), (7,8,7,8), (8,8,8,8),
(4,9,4,9), (5,8,5,9)]

Step 2 — Parameters

ε = 2.0
MinPts = 3

Step 3 — Distance

d(A,B) = √Σ(Aⱼ-Bⱼ)²

Step 4 — Neighborhood

Nε(P) = {Q : d(P,Q) ≤ ε}

Step 5 — Core Point

|Nε(P)| ≥ MinPts

Step 6 — Border Point

P is not core, but P lies within ε of a core point.

Step 7 — Noise

P is neither a core point nor within ε of a core point.
Final DBSCAN result: Cluster 1 = C1–C4, Cluster 2 = C5–C8, Noise = C9–C10.

13. Important DBSCAN Insight

Notice that DBSCAN produced a result that is different from the K-Means example.

With K-Means, C9 and C10 were placed into their own cluster because we explicitly requested K = 3.

With DBSCAN, we did not request three clusters. DBSCAN examined the density of the data and found that C9 and C10 did not have enough nearby points to satisfy MinPts = 3.

Therefore, C9 and C10 are classified as noise with these DBSCAN parameters. This does not necessarily mean they are bad customers. It means they are not part of a sufficiently dense region under ε = 2.0 and MinPts = 3.

USE CASE 1: Healthcare Patient Grouping using DBSCAN : Hospitals often need to group patients with similar characteristics to: Identify high-risk patients, Personalize treatment plans, Optimize resource allocation, Improve healthcare management


import pandas as pd from sklearn.preprocessing import StandardScaler from sklearn.cluster import DBSCAN import numpy as np # Load data # download from: # https://www.pythonplaza.com/healthcare_patient_dataset.html df = pd.read_csv("patients_data.csv") # Features used for clustering X = df[['Age', 'BMI', 'Blood_Pressure', 'Cholesterol', 'Hospital_Visits']] # Scale data scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Train DBSCAN model dbscan = DBSCAN(eps=1.5, min_samples=5) df['Cluster'] = dbscan.fit_predict(X_scaled) # Print cluster assignments print(df[['Cluster']].value_counts()) # New patient? new_patient = [[33, 28.0, 139, 210, 4]] # Apply SAME scaling new_patient_scaled = scaler.transform(new_patient) # ------------------------------ # DBSCAN prediction logic # ------------------------------ # DBSCAN has NO predict() method # We assign cluster using nearest core point (common approach) labels = df['Cluster'].values # Remove noise points (-1) valid_points = X_scaled[labels != -1] valid_labels = labels[labels != -1] # Compute distances from new patient to all points distances = np.linalg.norm(valid_points - new_patient_scaled, axis=1) # Find nearest point nearest_index = np.argmin(distances) cluster = valid_labels[nearest_index] print("Patient belongs to Cluster:", cluster)

USE CASE 2: Use DBSCAN for customer segmentation in Market Basket Analysis. Instead of finding which products are purchased together (like Apriori or FP-Growth), use DBSCAN to group customers based on their purchasing behavior. Once customers are clustered, you can create targeted promotions and personalized recommendations for each segment.



import pandas as pd
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
import numpy as np

# ----------------------------------
# Step 1: Sample Market Basket Data
# ----------------------------------
data = pd.DataFrame({
    'Customer': ['C001','C002','C003','C004','C005','C006','C007','C008'],
    'Bread': [12,10,11,1,0,2,6,5],
    'Milk': [10,8,9,2,1,1,5,6],
    'Eggs': [8,7,6,1,2,0,4,5],
    'Beer': [0,1,0,10,12,9,4,5],
    'Chips': [1,0,1,8,10,7,3,4]
})

# Load data (if using file instead)
# data = pd.read_csv("customer_shopping.csv")

print("Original Data")
print(data)

# ----------------------------------
# Step 2: Select Features
# ----------------------------------
X = data[['Bread', 'Milk', 'Eggs', 'Beer', 'Chips']]

# ----------------------------------
# Step 3: Scale Features
# ----------------------------------
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# ----------------------------------
# Step 4: Train DBSCAN Model
# ----------------------------------
dbscan = DBSCAN(eps=1.5, min_samples=2)
data['Cluster'] = dbscan.fit_predict(X_scaled)

print("\nCluster Assignments")
print(data[['Customer', 'Cluster']])

# ----------------------------------
# Step 5: Cluster Profiles
# ----------------------------------
print("\nCluster Profiles (Mean of Original Data)")

cluster_profiles = data.groupby('Cluster')[['Bread','Milk','Eggs','Beer','Chips']].mean()

print(cluster_profiles.round(2))

# ----------------------------------
# Step 6: Test New Customer
# ----------------------------------
new_customer = pd.DataFrame({
    'Bread': [11],
    'Milk': [9],
    'Eggs': [7],
    'Beer': [1],
    'Chips': [1]
})

new_customer_scaled = scaler.transform(new_customer)

# ----------------------------------
# DBSCAN has NO predict()
# So we assign cluster by nearest neighbor
# ----------------------------------

X_scaled_arr = np.array(X_scaled)
labels = np.array(data['Cluster'])

# Remove noise points (-1)
valid_mask = labels != -1

valid_X = X_scaled_arr[valid_mask]
valid_labels = labels[valid_mask]

# Compute distances to all valid points
distances = np.linalg.norm(valid_X - new_customer_scaled, axis=1)

nearest_index = np.argmin(distances)
predicted_cluster = valid_labels[nearest_index]

print("\nNew Customer")
print(new_customer)

print(f"\nPredicted Cluster: {predicted_cluster}")

# ----------------------------------
# Step 7: Recommendation Logic
# ----------------------------------
if predicted_cluster == 0:
    print("Recommendation: Bread, Milk, Eggs promotions")
elif predicted_cluster == 1:
    print("Recommendation: Beer and Chips promotions")
else:
    print("Recommendation: Mixed basket offers")



About Us  | Contact Us | Sitemap  | Privacy Policy