Professional Documents
Culture Documents
DBSCAN Python Example - The Optimal Value For Epsilon (EPS) - by Cory Maklin - Towards Data Science
DBSCAN Python Example - The Optimal Value For Epsilon (EPS) - by Cory Maklin - Towards Data Science
DBSCAN Python Example - The Optimal Value For Epsilon (EPS) - by Cory Maklin - Towards Data Science
Open in app
You have 2 free member-only stories left this month. Upgrade for unlimited access.
If you’re interested in reading up on DBSCAN, the original paper can be found here.
Algorithm
As is the case in most machine learning algorithms, the model’s behaviour is dictated
by several parameters. In the proceeding article, we’ll touch on three.
eps: Two points are considered neighbors if the distance between the two points is
below the threshold epsilon.
metric: The metric to use when calculating distance between instances in a feature
array (i.e. euclidean distance).
The algorithm works by computing the distance between every point and all other
points. We then place the points into one of three categories.
Core point: A point with at least min_samples points whose distance with respect to
the point is below the threshold defined by epsilon.
Border point: A point that isn’t in close proximity to at least min_samples points but is
close enough to one or more core point. Border points are included in the cluster of the
closest core point.
Noise point: Points that aren’t close enough to core points to be considered border
points. Noise points are ignored. That is to say, they aren’t part of any cluster.
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 2/7
09/07/2021 DBSCAN Python Example: The Optimal Value For Epsilon (EPS) | by Cory Maklin | Towards Data Science
Code
Let’s take a look at how we could go about implementing DBSCAN in python. To get
started, import the following libraries.
import numpy as np
sns.set()
plt.scatter(X[:,0], X[:,1])
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 3/7
09/07/2021 DBSCAN Python Example: The Optimal Value For Epsilon (EPS) | by Cory Maklin | Towards Data Science
As mentioned previously, we must provide a value for epsilon which defines the
maximum distance between two points. The following paper, describes an approach
for automatically determining the optimal value for Eps.
https://iopscience.iop.org/article/10.1088/1755-1315/31/1/012012/pdf
In layman’s terms, we find a suitable value for epsilon by calculating the distance to the
nearest n points for each point, sorting and plotting the results. Then we look to see
where the change is most pronounced (think of the angle between your arm and
forearm) and select that as epsilon.
We can calculate the distance from each point to its closest neighbour using the
NearestNeighbors . The point itself is included in n_neighbors . The kneighbors method
returns two arrays, one which contains the distance to the closest n_neighbors points
and the other which contains the index for each of those points.
neigh = NearestNeighbors(n_neighbors=2)
nbrs = neigh.fit(X)
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 4/7
09/07/2021 DBSCAN Python Example: The Optimal Value For Epsilon (EPS) | by Cory Maklin | Towards Data Science
distances = distances[:,1]
plt.plot(distances)
The optimal value for epsilon will be found at the point of maximum curvature.
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 5/7
09/07/2021 DBSCAN Python Example: The Optimal Value For Epsilon (EPS) | by Cory Maklin | Towards Data Science
We train our model, selecting 0.3 for eps and setting min_samples to 5.
m = DBSCAN(eps=0.3, min_samples=5)
m.fit(X)
The labels_ property contains the list of clusters and their respective points.
clusters = m.labels_
The model classified the densely populated areas. As we can see, all the dark blue
points were categorized as noise.
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 6/7
09/07/2021 DBSCAN Python Example: The Optimal Value For Epsilon (EPS) | by Cory Maklin | Towards Data Science
Final Thoughts
Unlike k-means, DBSCAN will figure out the number of clusters. DBSCAN works by
determining whether the minimum number of points are close enough to one another
to be considered part of a single cluster. DBSCAN is very sensitive to scale since epsilon
is a fixed value for the maximum distance between two points.
Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials
and cutting-edge research to original features you don't want to miss. Take a look.
Artificial Intelligence Machine Learning Programming Towards Data Science Data Science
https://towardsdatascience.com/machine-learning-clustering-dbscan-determine-the-optimal-value-for-epsilon-eps-python-example-3100091cfbc 7/7