# DBSCAN Clustering In Machine Learning

**Introduction**

DBSCAN is an important Clustering technique for Machine Learning (ML) and Data Science in general.

DBSCAN falls under unsupervised learning, thus opening up more possibilities and increasing the range of applying data. This is especially true for information that can be extracted from data.

The importance of using Density-Based Clustering in Data Mining and Data Analytics can be seen throughout major implementations of Machine Learning techniques.

The advantages of DBSCAN in Data Mining, Analytics and ML can be seen throughout various services powered by clustering methodologies.

For instance, DBSCAN is a highly preferred learning methodology that promotes pattern recognition, behavioural analytics, market research, data analysis, and image processing.

Basic clustering algorithms such as K-means and Hierarchical clustering are also helpful, however, DBSCAN is much more effective when dealing with anomalies or trying to detect outliers. Other clustering methods also limit Data scientists in their range of approaches and require the number of clusters to be specified.

The DBSCAN Clustering algorithm is much more assistive and powerful in comparison. To truly understand what DBSCAN is, we must first learn about clustering.

**What is Clustering?**

Clustering methodologies are fundamentally unsupervised learning methods that divide data points from a population into sub-populations (clusters, batches or groups) in accordance with their individual properties. Data points with similar properties or observations are clustered closer to each other, providing a representation for machines or systems to follow.

For instance, let’s take Amazon, YouTube or Netflix’s recommendation systems. They recommend different products or media, however, they operate on the same methodologies. Fundamentally, they use historical data of the users or the previous products (media) they have interacted with in order to recommend similar objects of interest.

Clustering can be done through multiple methods and algorithms, a few of them are DBSCAN clustering, Gaussian Mixture clustering and K-Means clustering. The DBSCAN algorithm is one of the popular methods of using clusters and data points in order to power ML processes and analytics.

**What Exactly is Density-Based Clustering?**

Density-Based clustering groups data points that are similar in nature into a single cluster based on how densely grouped they are. In order for Density-Based Clustering to work, there must be a certain number of data points between the clusters as well or in the ‘neighbourhood’.

Density-Based clustering can not only accurately cluster the data points in a population or dataset but also works well with noise. When compared with K-means or Hierarchical clustering, the DBSCAN algorithm handles noise the best, correctly detecting it inside datasets.

DBSCAN is the acronym for Density-Based Spatial Clustering of Applications with Noise. DBSCAN is extensively used for the identification of clusters in massive spatial populations or datasets by taking the local densities and properties of the data points into account.

DBSCAN is especially useful for working with outliers or anomalies and correctly detecting these outliers and points that stand out. DBSCAN clustering does not need the number of clusters to be specified prior to plotting and only needs two parameters, minPoints and epsilons.

Epsilons are the radiuses of the circles that are built around every data point in order to analyse the density. Meanwhile, minPoints are specified to determine the number of data points that are required inside the plotted circle in order for data points to be identified as core points.

When the circle is represented using 3D or 2D variants, epsilons become the radiuses of these hyperspheres, while minPoints determine the minimum data points demanded inside these hyperspheres. There are three types of data points that we need to know about when working with DBSCAN clustering.

**Core Point:**Core points possess more value than the specified minPoints within the epsilon.**Border Point:**Border points possess a lesser value than minPoints within the epsilon but in the neighbourhood of core points.**Outlier or Noise:**These are points that are neither a border point nor a core point.

**How Does DBSCAN Clustering Function?**

The main reason DBSCAN is used in machine learning is for separating high-density clusters from low-density ones. DBSCAN algorithms are fundamentally density-based clustering algorithms that seek areas or neighbourhoods that are highly dense with observations within the total data as compared to areas with lesser populations.

DBSCAN clustering algorithms can also sort or cluster data that possess certain properties or varying shapes.

DBSCAN clustering functions by dividing datasets into ‘n’ dimensions, with each data point forming ‘n’ dimensional shapes around them. This shape is created for each data point to figure out the cluster density by counting how many data points are falling within these ‘n’ dimensional shapes.

When data points cover more than the specified number of other corresponding data points, the data points of interest are concluded as core points that determine the final plotting of the clusters. These shapes are all clusters and the DBSCAN algorithm iteratively keeps expanding these clusters by working on every data point inside a cluster to reach other data points through the target data point.

This Density-based clustering algorithm starts at a random point and then keeps repeating this process till the desired results are achieved. This is truly very useful in multiple machine learning processes as a highly accurate unsupervised learning methodology.

DBSCAN in data mining and analytics is common due to its immense capability of supporting segmentation, prediction, processing and recognition services. This process allows DBSCAN clustering to be an effective method of estimating the number of data points that are within clusters and the points that are nearby as well. Let us check how we can implement DBSCAN clustering using Python.

First, we must import the required libraries:

```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import math
```

Then, we must create a dataset that can be visualised with the help of adding functions. We will also add a function that returns arrays of data points by using the radius and the number of observations or data points. These arrays will form circles when plotted.

```
np.random.seed(36)
def PointsInCircum(r,n=200):
return [(math.cos(2*math.pi/n*x)*r+np.random.normal(-60,60),math.sin(2*math.pi/n*x)*r+np.random.normal(-60,60)) for x in range(1,n+1)]
```

Let us then create circles with different radiuses and add outliers or noise to check how DBSCAN clustering deals with this data.

```
df=pd.DataFrame(PointsInCircum(300,800))
df=df.append(PointsInCircum(200,900))
df=df.append(PointsInCircum(300,600))
df=df.append([(np.random.randint(-300,300),np.random.randint(-300,300)) for i in range(150)])
```

Here is another approach to specifying DBSCAN parameters in Python after importing the initial packages.

```
X, y = make_circles(n_samples=650, factor=0.5, noise=0.2)
X = StandardScaler().fit_transform(X)
y_pred = DBSCAN(eps=0.5, min_samples=12).fit_predict(X)
# Generate sample data
centers = [[3, 2], [1, 4], [2, 4]]
X, labels_true = make_blobs(n_samples=650, centers=centers, cluster_std=0.5,
random_state=0)
```

**Pros and Cons of using DBSCAN in ML or Analytics**

Like any other algorithm for clustering technique, DBSCAN has its very own set of advantages and disadvantages. Let us check them out.

**Advantages**

- DBSCAN clustering does not need the total number or amount of clusters to be specified priorly.
- DBSCAN works great with outliers, anomalies and observations that stand out.
- DBSCAN can easily detect outliers and determine (identify) the noise, thus being able to effectively plot them between dimensional shapes.
- The DBSCAN algorithm is very effective when working with clusters of varying shapes or arbitrary shapes.

**Disadvantages**

- Determining the distance of epsilons might be tricky in certain situations, due to it governing the neighbourhood parameters and the distance between data points.
- Including the epsilon, this clustering technique relies heavily on its two main parameters to derive its functions.
- DBSCAN does not work well with clusters that have varying sub-population densities. If the in-cluster densities are too different, DBSCAN might not be able to define the clusters effectively.
- Cluster characteristics completely rely on eps-minPts parameters and the complementing functions. There is only one combination of eps-minPts that is specified during implementing DBSCAN, thus restricting its effectiveness across various densities.

**Frequently Asked Questions**

**How do I cluster with DBSCAN?**

DBSCAN forms clusters by finding all the core points and identifying the neighbour points. New clusters are created when core points are not assigned to a cluster yet. Then, the density of all the data points that fall inside a cluster is estimated recursively and then assigned to the same clusters.

**What is DBSCAN in machine learning?**

DBSCAN is an impressive density-based clustering technique that is used for unsupervised machine learning processes.

**What are the conditions for DBSCAN clustering?**

The two conditional determinants of DBSCAN clustering are epsilons (eps) and minPoints (minPts). These two components heavily determine the effectiveness of the algorithm, clustering behaviour and cluster characteristics.

**What are the two major components of DBSCAN clustering?**

Other than eps and minPts, neighbourhood and minimum points are two major components of DBSCAN clustering. Neighbourhood (n) is the minimum distance of points from other core points. Minimum points (m) is the minimum number of points needed to build clusters. These two are two important determinants that govern the eligibility of data points to be a part of the clusters.

**How is HDBSCAN better than DBSCAN?**

HDBSCAN is much more effective when working on data with varying densities. DBSCAN is also much slower than HDBSCAN, taking almost twice the amount of time to work on data points.

**What is the difference between DBSCAN and optics?**

Optics function like extensions of DBSCAN. The difference between the two is that Optics do not assign cluster memberships to data points but store the processing order of these points.

**Is DBSCAN better than K-means?**

Yes, it works better with noise and detects outliers much more effectively. DBSCAN also does not require the number of clusters to be specified.

**What does ‘DBSCAN’ stand for?**

DBSCAN stands for Density-Based Spatial Clustering of Applications with Noise.

**What is unsupervised learning?**

Unsupervised learning is a learning methodology that focuses on learning patterns and acquiring information from untagged data. Unsupervised learning then allows machines to build relationships between data points and then build estimations or imaginations based on the compact representations of data.

## Conclusion

Density-based clustering is definitely one of the best clustering techniques out there. With packages such as ‘fpc’ or ‘dbscan’ that are available for Python and R, Data Scientists can readily go ahead with using the DBSCAN algorithm to create clusters from data.

DBSCAN promotes advanced analytics and increases the scope of Machine Learning. DBSCAN clustering is truly a gift to Data Science with its core objective being to identify important information based on the density of observations inside multiple types of datasets.