scikit-learn
DBSCAN
machine learning
clustering
data science

scikit-learn Predicting new points with DBSCAN

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Overview of DBSCAN

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is a popular clustering algorithm in machine learning, particularly well-suited for datasets that exhibit clusters of varying shapes and sizes. Distinct from algorithms like K-Means, which require the number of clusters to be defined a priori, DBSCAN determines clusters based on the density of data points.

How DBSCAN Works

  1. Core Points: Points within a dense region that exceed a minimum number of neighboring points (min_samples) within eps (epsilon) distance.
  2. Border Points: Points that are within the neighborhood (eps distance) of a core point but do not themselves have enough neighbors to be core points.
  3. Noise Points: Points that are neither core nor border points and fail to meet the density criteria.

DBSCAN visits each point in the dataset, classifying them as core, border, or noise, and expands clusters from core points using a depth-first search approach. The process continues until all points are properly classified.

Predicting New Points with DBSCAN

DBSCAN primarily focuses on unsupervised learning, clustering existing data points, and isn't designed for predicting labels of new, unseen data points. However, some practical approaches enable attempting such predictions:

  1. Re-Classification: Integrate new data into the original dataset and re-run DBSCAN. While computationally expensive, this preserves coherence with initial cluster distributions.
  2. Nearest Neighbor Assignment: For each new data point, identify its nearest existing data point (or set of points). Assign the new data point to the same cluster, provided it meets min_samples criterion.

Example Implementation using Scikit-Learn

python
1from sklearn.cluster import DBSCAN
2import numpy as np
3
4# Example dataset
5X = np.array([
6    [1, 2], [2, 2], [2, 3],
7    [8, 7], [8, 8], [25, 80]
8])
9
10# Creating and fitting DBSCAN
11dbscan = DBSCAN(eps=3, min_samples=2)
12dbscan.fit(X)
13
14# Cluster labels
15print(f'Cluster labels: {dbscan.labels_}')
16
17# Adding new points
18new_points = np.array([[3, 3], [9, 9], [99, 99]])
19
20def predict_new_points(dbscan_model, new_points):
21    labels = dbscan_model.labels_
22    core_samples = dbscan_model.components_
23
24    new_labels = []
25    for point in new_points:
26        # Calculate distance to all core samples
27        distances = np.linalg.norm(core_samples - point, axis=1)
28        nearest_core_index = np.argmin(distances)
29
30        # Assign to the closest core point's cluster if it's within `eps`
31        if distances[nearest_core_index] <= dbscan_model.eps:
32            new_labels.append(labels[np.where(dbscan_model.core_sample_indices_ == nearest_core_index)[0][0]])
33        else:
34            new_labels.append(-1)  # Mark as noise
35
36    return np.array(new_labels)
37
38predictions = predict_new_points(dbscan, new_points)
39print(f'Predicted labels for new points: {predictions}')

Technical Considerations

  • Scalability: DBSCAN scales poorly with large datasets because it necessitates comparing every point against eps.
  • Parameter Sensitivity: Choosing appropriate values for eps and min_samples is crucial. They significantly affect the resulting cluster formation.
  • Dimensionality Challenges: DBSCAN can struggle in high-dimensional spaces due to the curse of dimensionality, which makes measuring density complex.

Applications of DBSCAN

  • Geospatial Data: Ideal for clustering geographical locations, such as detecting natural groupings in city locations.
  • Image Processing: Used for separating distinct objects or features within an image.
  • Anomaly Detection: Identifying noise points helps in detecting outliers, crucial for fraud detection or fault diagnoses in systems.

Key Points Summary

AspectDetails
Algorithm TypeDensity-based clustering
Core ComponentsCore points, Border points, Noise points
Key Parameterseps distance, min_samples
StrengthsHandles noise, discovers clusters of varying shapes
WeaknessesHigh sensitivity to parameters, poor scalability in large datasets
Application AreasGeospatial clustering, image processing, anomaly detection

In summary, DBSCAN is a powerful clustering tool within Scikit-Learn, particularly effective for identifying complex cluster shapes and separating noise. While not typically used for direct prediction, strategies to assess new data points provide some level of adaptability to new data.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.