20 - DBSCAN
Recap of K-means Algorithm
Purpose: Partitions a dataset into K non-overlapping clusters by minimizing within-cluster variance.
Initial Centroid Placement: Random initial centroid placement can affect convergence and clustering results.
Techniques for Improvement:
K-means++: An enhanced method for initial centroid placement to improve clustering.
Bisecting K-means: Another variant to find better performance.
Elbow Method:
Plots clusters vs. within-cluster sum of squares (WCSS) to find the optimal number of clusters, K.
Limitations of K-means
Non-spherical Data: Struggles to identify non-spherical clusters.
Varying Cluster Sizes/Densities: Ineffective in handling clusters that vary in size and density.
Local Minima: Frequently caught in local minima due to iterative refinement and sensitivity to initial conditions.
Density-Based Clustering
Definition: Clusters are identified as dense regions of data points, separated by regions of lower density.
Advantages: Capable of identifying clusters of arbitrary shapes, unlike K-means which favors spherical clusters.
DBSCAN (Density-Based Spatial Clustering of Applications with Noise)
Overview: Popular density-based clustering algorithm that groups points based on:
Proximity: Typically using Euclidean distance.
Minimum Point Density: Helps distinguish dense areas from sparse areas in data.
Why Use DBSCAN?
Strengths:
Performs well with non-spherical and irregularly-shaped clusters.
Robust against noise and outliers.
Weaknesses of Other Methods:
K-means and hierarchical clustering perform poorly with noise and outliers.
DBSCAN Hyperparameters
Epsilon (ϵ):
Radius defining the neighborhood around a point.
Points within this radius are considered neighbors.
Example units depend on the context: meters, pixels, etc.
minPts:
Minimum number of points required to form a dense region (core point).
Must include the point itself.
DBSCAN Outputs
Core Points: Points with at least
minPtsneighbors within ϵ.Border Points: Points with fewer than
minPtsneighbors but within ϵ of a core point.Noise or Outliers: Points that are neither core nor border points.
Classifying Points
Points are classified based on relationship to core points:
Core Point: At least
minPtsneighbors within ϵ.Border Point: Fewer than
minPtsneighbors but within ϵ of a core point.Noise Point: Any other points not classified above.
Cluster Formation in DBSCAN
Core Points: If two core points are neighbors, they belong to the same cluster.
Border Points: Assigned to the nearest core point's cluster during traversal.
Traversal Mechanism: Border points get linked to the first core point they encounter, not reassigned if reachable from multiple core points.
Impact of Hyperparameters on Clustering
Large ϵ: Can merge unrelated clusters.
Low minPts: May include noise as valid clusters.
High minPts: Can overlook sparse valid clusters, leading to underfitting.
Choosing Epsilon (ϵ)
Use a k-distance plot to identify the optimal value for ϵ:
Calculate distances from points to their kth nearest neighbor.
Sort and plot these distances to find the “elbow” point.
Rule of thumb: Set
minPts = k + 1.
Determining Parameters
minPts Impact: Smaller values yield more clusters (may include noise), while larger values yield broader, less precise clusters.
General rule: Set
minPts ≥ D + 1, where D is dimensionality, to prevent noise in high-dimensional data.
Programming DBSCAN Example
Import necessary libraries:
import numpy as npimport matplotlib.pyplot as plfrom sklearn.datasets import make_moonsfrom sklearn.cluster import DBSCAN
Generate data and apply DBSCAN:
Create synthetic data using
make_moons().Apply DBSCAN with specified
epsandmin_samples.Plot the results visually.
DBSCAN vs K-Means Characteristics
DBSCAN:
Clusters determined automatically based on density.
Suitable for arbitrary shapes and robust against noise.
Requires tuning of epsilon and minPts.
K-Means:
Requires manual specification of the number of clusters.
Best for spherical shapes, sensitive to noise, and assumes uniform density.
Applications of DBSCAN
Geospatial Analysis: Clustering points of interest.
Astronomy: Studying cosmic structures.
Biomedicine: Applications in genomics and medical imaging.
Image Processing: Segmentation of images.
Use Cases Include:
Anomaly Detection
Noise Filtering
Community Detection in Social Networks
Pros & Cons of DBSCAN
Pros:
Effective with arbitrary cluster shapes and noise handling.
Versatile across various types of data.
Cons:
Can struggle in high-dimensional datasets.
Sensitive to distance metric selection.
May not handle varying density clusters effectively due to fixed parameters.
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) should be used in the following scenarios:
When handling non-spherical and irregularly-shaped clusters: Unlike K-means, which favors spherical clusters, DBSCAN can identify clusters of arbitrary shapes.
For datasets containing noise and outliers: DBSCAN is robust against these factors and can effectively separate dense regions from noise.
In cases where the dataset has varying densities: DBSCAN adapts better to regions of varying density compared to methods like K-means, which assumes uniform density