Home » Python • Unsupervised Machine Learning » How to create clusters using DBSCAN in Python
How to create clusters using DBSCAN in
Python
Python, Unsupervised Machine Learning / 2 Comments / By Farukh Hashmi
Density Based Spatial Clustering of Applications with Noise(DBSCAN) is one of the clustering
algorithms which can find clusters in noisy data. It works even on those datasets where K-
Means fail to find meaningful clusters. More information about it can be found here.
You can learn more about the DBSCAN algorithm in the below video.
How DBSCAN clustering works? | AI ML tutorials by a Data Scientist | …
The below code snippet will help to create clusters in data using DBSCAN.
Creating data for clustering
1 # importing plotting library
2 import matplotlib.pyplot as plt
3 # Create Sample data
4 from sklearn.datasets import make_moons
5 X, y= make_moons(n_samples=500, shuffle=True, noise=0.1, random_state=20)
6 plt.scatter(x= X[:,0], y= X[:,1])
Sample Output:
Moons clustering data for DBCAN
Finding Best hyperparameters for DBSCAN using Silhouette
Coefficient
The Silhouette Coefficient is calculated using the mean intra-cluster distance (a) and the
mean nearest-cluster distance (b) for each sample. The Silhouette Coefficient for a sample is
(b – a) / max(a, b). To clarify, b is the distance between a sample and the nearest cluster that
the sample is not a part of. Note that Silhouette Coefficient is only defined if number of labels
is 2 <= n_labels <= n_samples – 1.
The best value of the Silhouette Coefficient is 1 and the worst value is -1. Values near 0 indicate
overlapping clusters. Negative values generally indicate that a sample has been assigned to
the wrong cluster
1 ## Finding best values of eps and min_samples
2 import numpy as np
3 import pandas as pd
4 from sklearn.metrics import silhouette_score
5 from sklearn.cluster import DBSCAN
6
7 # Defining the list of hyperparameters to try
8 eps_list=np.arange(start=0.1, stop=0.9, step=0.01)
9 min_sample_list=np.arange(start=2, stop=5, step=1)
10
11 # Creating empty data frame to store the silhouette scores for each trials
12 silhouette_scores_data=pd.DataFrame()
13
14 for eps_trial in eps_list:
15 for min_sample_trial in min_sample_list:
16
17 # Generating DBSAN clusters
18 db = DBSCAN(eps=eps_trial, min_samples=min_sample_trial)
19
20 if(len(np.unique(db.fit_predict(X)))>1):
21 sil_score=silhouette_score(X, db.fit_predict(X))
22 else:
23 continue
24 trial_parameters="eps:" + str(eps_trial.round(1)) +" min_sample :" + str(min_
25
26 silhouette_scores_data=silhouette_scores_data.append(pd.DataFrame(data=[[sil_
27
28 # Finding out the best hyperparameters with highest Score
29 silhouette_scores_data.sort_values(by='score', ascending=False).head(1)
Sample Output
Finding best hyperparameters for
DBSCAN
Creating clusters using the best hyperparameters
1 # DBSCAN Clustering
2 from sklearn.cluster import DBSCAN
3 db = DBSCAN(eps=0.18, min_samples=2)
4 # Plotting the clusters
5 plt.scatter(x= X[:,0], y= X[:,1], c=db.fit_predict(X))
DBSCAN clustering in python
AUTHOR DETAILS
Farukh Hashmi
Lead Data Scientist
Farukh is an innovator in solving industry problems using
Artificial intelligence. His expertise is backed with 10 years
of industry experience. Being a senior data scientist he is
responsible for designing the AI/ML solution to provide
maximum gains for the clients. As a thought leader, his
focus is on solving the key business problems of the CPG
Industry. He has worked across different domains like
Telecom, Insurance, and Logistics. He has worked with
global tech leaders including Infosys, IBM, and Persistent
systems. His passion to teach inspired him to create this
website!
https://thinkingneuron.com/
thinkingneuron@gmail.com
← How to create Hierarchical How to find clusters in data using
clustering in Python OPTICS in Python →
2 thoughts on “How to create clusters using DBSCAN in Python”
REBECCA V.
AUGUST 2, 2022 AT 6:02 PM
Hi! Thanks for the code snippet. Just a heads up it appears there may be a rendering error in
line 20:
if(len(np.unique(db.fit_predict(X)))>1):
Reply
REBECCA V.
AUGUST 2, 2022 AT 6:03 PM
Of course it’s rendering properly in my comment lololol. Anyway, thanks again!
Reply
Leave a Reply!
Your email address will not be published. Required fields are marked *
Comment
Name*
Email*
Website
Submit
AI/ML Algorithms and Topics
Adaboost Apriori Artificial Neural Network Classification Clustering CNN
DataFrame Data Frame Data Pre Processing Data Science date
Decision Tree Deep Learning Eclat Feature Selection FP-Growth
Hyperparameter Tuning KNN library LSTM Machine Learning NLP
Pandas POS Programming Python
Python Basics For Machine Learning Python Case Study R
Random Forest Regression Sampling Sampling Theory Sentiment Analysis
Statistics Statistics for Data Science Supervised Machine Learning SVM
T-SNE Text Mining TF-IDF UMAP Unsupervised Machine Learning
Wordcloud Xgboost
Powered by Thinking Neuron