DBSCAN Made Simple & How To Python Tutorial With Scikit-Learn

by | Aug 29, 2023 | Data Science, Machine Learning

What is DBSCAN?

DBSCAN stands for “Density-Based Spatial Clustering of Applications with Noise.” It is a popular clustering algorithm used in machine learning and data mining to group data points close to each other in a high-dimensional space. DBSCAN is particularly useful for discovering clusters of arbitrary shapes and identifying noise points within a dataset.

The primary idea behind DBSCAN is to define clusters as dense regions of data points separated by sparser regions. This contrasts traditional clustering algorithms like k-means, which assume clusters as spherical or convex shapes and can struggle with non-linear and irregular cluster structures.

The DBSCAN algorithm explained

DBSCAN works by considering two main parameters:

  1. Epsilon (ε): This parameter defines the radius around a data point used to determine its neighbourhood. Points within this radius are considered neighbours of the central point.
  2. MinPoints: This parameter sets the minimum number of points required to form a dense region or cluster. A point with at least MinPoints neighbours within its ε-radius is considered a core point. Points that are not core but fall within the ε-radius of a core point are considered border points. Points that are neither core nor border points are considered noise points.

The algorithm proceeds as follows:

  1. Core Point Identification: DBSCAN determines whether it has enough neighbours within its ε-radius to be considered a core point for each data point.
  2. Cluster Expansion: The algorithm expands the cluster by adding neighbouring core and border points starting from a core point. This process continues recursively until no more points can be added.
  3. Density Reachability: Points that are reachable from a core point (directly or through a chain of core points) are part of the same cluster. Points that are not reachable from any core point are considered noise.

The advantages of DBSCAN are its ability to discover clusters of varying shapes and sizes, its resistance to noise, and its ability to find clusters without requiring the number of clusters to be specified beforehand. However, DBSCAN can be sensitive to the choice of ε and MinPoints parameters, and it might struggle with clusters of significantly different densities.

The advantages of DBSCAN are its ability to discover clusters of varying shapes and sizes

The advantages of DBSCAN are its ability to discover clusters of varying shapes and sizes.

Important considerations when using DBSCAN

  1. Choosing Parameters: Selecting appropriate values for ε and MinPoints is crucial. The choice of these parameters depends on the data and the desired cluster characteristics.
  2. Handling Outliers: DBSCAN can naturally identify noise points (outliers) as they are not assigned to any cluster.
  3. Cluster Shapes: DBSCAN effectively finds clusters with arbitrary shapes and can handle clusters of different sizes and densities.
  4. Scalability: The performance of DBSCAN can be impacted by the size of the dataset and the choice of parameters. For larger datasets, optimizations or alternative algorithms might be necessary.
  5. Distance Metric: The choice of distance metric used to calculate the distances between points can affect the results.

DBSCAN advantages and disadvantages

DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is a robust clustering algorithm, but it has advantages and disadvantages like any method.

Advantages

  1. Ability to Handle Complex Cluster Shapes: DBSCAN can discover clusters of arbitrary shapes, including non-convex and irregular shapes, which traditional methods like k-means struggle with.
  2. Automatic Number of Clusters: DBSCAN does not require you to specify the number of clusters in advance, making it suitable for cases where the number of clusters is unknown or variable.
  3. Noise Detection: DBSCAN naturally identifies and labels noise points, data points that do not belong to any cluster. This can be valuable for outlier detection.
  4. Robust to Outliers: Noise points and outliers do not significantly affect the clustering process, as DBSCAN focuses on dense regions rather than distances to all data points.
  5. Efficient Processing: DBSCAN’s algorithmic complexity is relatively low, especially when dealing with large datasets. It doesn’t require a priori distance calculations for all pairs of points, which makes it faster than hierarchical clustering methods.
  6. Works with Various Data Types: DBSCAN is not restricted to any specific data type or metric space, allowing it to be applied to a wide range of data.

Disadvantages

  1. Sensitivity to Parameters: The performance of DBSCAN can be sensitive to the choice of parameters, such as epsilon (ε) and min_samples. Finding the optimal parameter values can be challenging and require domain knowledge or experimentation.
  2. Difficulty with Varying Densities: DBSCAN might struggle with datasets containing clusters with significantly different densities. It might treat regions of low density as noise or form unintended clusters.
  3. Difficulties with High-Dimensional Data: Like many clustering algorithms, DBSCAN can face challenges when dealing with high-dimensional data due to the curse of dimensionality.
  4. Initialization: DBSCAN’s performance can vary depending on the initial choice of points, which can impact the creation of clusters and potentially lead to suboptimal results.
  5. Lack of Guarantees for Global Optima: DBSCAN finds local density maxima and can miss global structure in the data if the density distribution is complex.
  6. Not Suitable for All Datasets: While DBSCAN is a versatile algorithm, there are cases where other clustering algorithms, such as k-means or hierarchical clustering, might be more appropriate.

DBSCAN is a valuable tool for identifying clusters in data with arbitrary shapes and detecting noise points. Its ability to automatically determine the number of clusters and its robustness to outliers are vital strengths. However, parameter tuning and sensitivity to varying densities can pose challenges in practical applications. It’s essential to consider the specific characteristics of your dataset and problem when choosing a clustering method.

What are the alternatives to DBSCAN?

There are several alternative clustering algorithms that you can consider if you’re looking for options beyond DBSCAN. The choice of algorithm depends on the characteristics of your data, the nature of the clusters you’re trying to discover, and your specific goals. Here are a few alternatives:

  1. K-Means Clustering: K-means is one of the most widely used clustering algorithms. It partitions data into a specified number of clusters by minimizing the sum of squared distances between data points and their cluster centroids. It’s simple and efficient but assumes clusters to be spherical and equally sized.
  2. Hierarchical Clustering: This algorithm family builds a cluster hierarchy by iteratively merging or splitting clusters. Agglomerative hierarchical clustering starts with individual data points as clusters and then joins them based on a linkage criterion. Divisive hierarchical clustering starts with all data points as a single cluster and recursively divides them.
  3. Mean Shift Clustering: Mean shift identifies clusters as regions of high data density by iteratively shifting points towards their neighbourhood’s mode (high-density area). It’s effective for finding clusters of varying shapes and sizes.
  4. Gaussian Mixture Models (GMM): GMM assumes that data points are generated from a mixture of several Gaussian distributions. It estimates the parameters of these distributions and assigns data points to clusters based on the likelihood.
  5. Agglomerative Clustering: Agglomerative clustering builds a hierarchy of clusters by iteratively merging the most similar clusters. It’s flexible and can handle various cluster shapes but can be computationally expensive for large datasets.
  6. OPTICS: Order-based Clustering for Identification of Clustering Structure (OPTICS) is an extension of DBSCAN that produces cluster ordering rather than strict partitioning. It can handle varying densities and provides a visualization of the cluster structure.
  7. Spectral Clustering: Spectral clustering uses the eigenvectors of a similarity matrix to project data into a lower-dimensional space where clusters are more easily separable. It’s useful for complex cluster shapes and can handle non-convex clusters.
  8. BIRCH: Balanced Iterative Reducing and Clustering using Hierarchies (BIRCH) is a memory-efficient hierarchical clustering algorithm suitable for large datasets. It constructs a tree structure to represent the data.
  9. Affinity Propagation: Affinity Propagation identifies exemplars among data points and assigns other points to these exemplars based on their similarity. It can handle varying cluster sizes and automatically determine the number of clusters.
  10. Self-Organizing Maps (SOM): SOM is an unsupervised neural network technique that maps high-dimensional data onto a lower-dimensional grid, preserving the topological structure of the data. It’s useful for visualization and identifying clusters.

Remember that the choice of algorithm depends on factors like the size of your dataset, the desired cluster shapes, the presence of noise, and the nature of your data. Trying multiple algorithms and evaluating their performance on your problem is often a good idea.

What is HDBSCAN?

HDBSCAN, short for “Hierarchical Density-Based Spatial Clustering of Applications with Noise,” is an extension of the original DBSCAN algorithm that adds a hierarchical approach to density-based clustering. HDBSCAN was designed to address some of the limitations of DBSCAN and provide a more robust and flexible clustering solution. It combines the advantages of both hierarchical and density-based clustering methods.

Here’s an overview of how HDBSCAN works:

1. Input Parameters:

  • Dataset: The dataset containing the data points to be clustered.
  • MinPoints: The minimum number of points required within the ε-radius to consider a point a core point.
  • MinClusterSize: The minimum number of points needed for a cluster to be considered valid.

2. Constructing the Hierarchical Graph:

  • The algorithm starts by creating a mutual reachability graph, similar to how DBSCAN identifies core points and their reachable points. However, instead of considering only direct density-reachable points, HDBSCAN computes a mutual reachability distance between each pair of points based on their shared neighbours.

3. Condensing the Graph:

  • HDBSCAN constructs a condensed tree of the hierarchical graph using a technique called “single linkage clustering.” This tree represents a hierarchy of clusters at different levels of density.

4. Identifying the Cluster Hierarchy:

  • Starting from the most dense level of the tree, clusters are formed by cutting branches at different levels based on a threshold value. The user determines the threshold which can be chosen using the “minimum cluster size” criterion.

5. Outlier Detection:

  • Points that do not fall into any cluster and are not part of the minimum cluster size are marked as outliers.

HDBSCAN offers several advantages over traditional clustering algorithms

  1. Automatically Determines the Number of Clusters: HDBSCAN does not require you to specify the number of clusters beforehand. It provides a hierarchy of clusters that can be explored at different levels of granularity.
  2. Handles Clusters of Varying Densities: HDBSCAN can effectively manage clusters with different densities, which is a limitation of traditional DBSCAN.
  3. Produces Stable Results: The hierarchical approach helps generate stable and robust cluster assignments.
  4. Identifies Noise Points: HDBSCAN can also identify noise points not part of any cluster.

However, HDBSCAN’s performance might be influenced by its hyperparameters, such as MinPoints, MinClusterSize, and the threshold for cutting the cluster hierarchy. Finding suitable values for these hyperparameters can require experimentation and data understanding.

HDBSCAN is an extension of DBSCAN that introduces a hierarchical clustering approach, allowing it to discover clusters of varying densities and automatically determine the number of clusters in the data.

How to tutorial for DBSCAN in Python with sklearn

Here’s an example of how you can use the DBSCAN algorithm in Python using the popular machine learning library scikit-learn.

Make sure to install scikit-learn and matplotlib in your Python environment before running this code. You can install them using:

pip install scikit-learn matplotlib 
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

# Generate some sample data
n_samples = 300
n_features = 2
X, _ = make_blobs(n_samples=n_samples, n_features=n_features, centers=4, cluster_std=0.60, random_state=0)

# Create a DBSCAN instance
epsilon = 0.3  # Epsilon radius
min_samples = 5  # Minimum number of points to form a core point
dbscan = DBSCAN(eps=epsilon, min_samples=min_samples)

# Fit the DBSCAN model to the data
labels = dbscan.fit_predict(X)

# Number of clusters in labels, ignoring noise points (-1)
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
n_noise = list(labels).count(-1)

print(f'Estimated number of clusters: {n_clusters}')
print(f'Estimated number of noise points: {n_noise}')

# Visualize the clusters
unique_labels = set(labels)
colors = [plt.cm.Spectral(each) for each in range(len(unique_labels))]
for k, col in zip(unique_labels, colors):
    if k == -1:
        col = [0, 0, 0, 1]  # Black for noise points

    class_members = [index[0] for index in np.argwhere(labels == k)]
    cluster_core_samples = [index for index in class_members if labels[index] != -1]
    
    plt.plot(X[class_members, 0], X[class_members, 1], 'o', markerfacecolor=tuple(col), markersize=10)
    plt.plot(X[cluster_core_samples, 0], X[cluster_core_samples, 1], 'o', markerfacecolor=tuple(col), markersize=15)
    
plt.title('Estimated number of clusters: %d' % n_clusters)
plt.show()
DBSCAN plot with python and sklearn

In this example, we first generate synthetic data using the make_blobs function from scikit-learn. Then, we create a DBSCAN instance with the desired epsilon radius and minimum samples. We fit the DBSCAN model to the data and obtain the cluster labels. Finally, we visualize the clusters and noise points using matplotlib.

Remember that tuning the epsilon and min_samples parameters is crucial for obtaining meaningful results with DBSCAN for your specific dataset.

Conclusion

Clustering is a fundamental technique in unsupervised machine learning that aims to group similar data points to discover patterns, structures, and insights within the data. DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is a popular clustering algorithm known for finding clusters of arbitrary shapes and its robustness in handling noise. It offers several advantages, such as automatic determination of cluster count, noise detection, and efficiency in processing.

However, DBSCAN has limitations, including sensitivity to parameter settings, challenges with varying densities, and potential difficulties with high-dimensional data. Therefore, when deciding on an appropriate clustering approach, it’s essential to consider the characteristics of your data and your specific goals.

If DBSCAN isn’t an ideal fit for your dataset or problem, several alternatives exist, such as k-means, hierarchical clustering, mean shift, and Gaussian mixture models. Each algorithm has its strengths and weaknesses, and the best choice depends on factors like the dataset size, desired cluster shapes, and noise.

Remember that no single clustering algorithm is universally superior, and it’s often beneficial to experiment with different methods, preprocess your data appropriately, and evaluate the results based on your domain knowledge and the objectives of your analysis. Clustering algorithms, including DBSCAN, provide valuable tools for uncovering hidden structures and relationships within data, which can lead to deeper insights and better decision-making.

About the Author

Neri Van Otten

Neri Van Otten

Neri Van Otten is the founder of Spot Intelligence, a machine learning engineer with over 12 years of experience specialising in Natural Language Processing (NLP) and deep learning innovation. Dedicated to making your projects succeed.

Recent Articles

One class SVM anomaly detection plot

How To Implement Anomaly Detection With One-Class SVM In Python

What is One-Class SVM? One-class SVM (Support Vector Machine) is a specialised form of the standard SVM tailored for unsupervised learning tasks, particularly anomaly...

decision tree example of weather to play tennis

Decision Trees In ML Complete Guide [How To Tutorial, Examples, 5 Types & Alternatives]

What are Decision Trees? Decision trees are versatile and intuitive machine learning models for classification and regression tasks. It represents decisions and their...

graphical representation of an isolation forest

Isolation Forest For Anomaly Detection Made Easy & How To Tutorial

What is an Isolation Forest? Isolation Forest, often abbreviated as iForest, is a powerful and efficient algorithm designed explicitly for anomaly detection. Introduced...

Illustration of batch gradient descent

Batch Gradient Descent In Machine Learning Made Simple & How To Tutorial In Python

What is Batch Gradient Descent? Batch gradient descent is a fundamental optimization algorithm in machine learning and numerical optimisation tasks. It is a variation...

Techniques for bias detection in machine learning

Bias Mitigation in Machine Learning [Practical How-To Guide & 12 Strategies]

In machine learning (ML), bias is not just a technical concern—it's a pressing ethical issue with profound implications. As AI systems become increasingly integrated...

text similarity python

Full-Text Search Explained, How To Implement & 6 Powerful Tools

What is Full-Text Search? Full-text search is a technique for efficiently and accurately retrieving textual data from large datasets. Unlike traditional search methods...

the hyperplane in a support vector regression (SVR)

Support Vector Regression (SVR) Simplified & How To Tutorial In Python

What is Support Vector Regression (SVR)? Support Vector Regression (SVR) is a machine learning technique for regression tasks. It extends the principles of Support...

Support vector Machines (SVM) work with decision boundaries

Support Vector Machines (SVM) In Machine Learning Made Simple & How To Tutorial

What are Support Vector Machines? Machine learning algorithms transform raw data into actionable insights. Among these algorithms, Support Vector Machines (SVMs) stand...

underfitting vs overfitting vs optimised fit

Weight Decay In Machine Learning And Deep Learning Explained & How To Tutorial

What is Weight Decay in Machine Learning? Weight decay is a pivotal technique in machine learning, serving as a cornerstone for model regularisation. As algorithms...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

nlp trends

2024 NLP Expert Trend Predictions

Get a FREE PDF with expert predictions for 2024. How will natural language processing (NLP) impact businesses? What can we expect from the state-of-the-art models?

Find out this and more by subscribing* to our NLP newsletter.

You have Successfully Subscribed!