Customer Clustering using DBSCAN. In this blog, we will learn about one... | by Tarique Akhtar | Towards Data Science

In this blog, we will learn about one of my favorite clustering algorithm and that is the DBSCAN algorithm. We will first understand the theory then I will demonstrate the working of the DBSCAN with the help of a very simple example.
The DBSCAN stands for density based spatial clustering of applications with noise.
It was introduced around 1996 and has gained significant popularity in the community due to its effective clustering ability of recognizing different types of clustering shapes and also the ability to identify the clusters correctly even in the presence of noise.
Let’s jump to some key terminologies that will help us understand the basic working of the algorithm.
There are two parameters that play a vital role in the algorithm. 1) Min points and 2) Epsilon.
The algorithm works by processing each and every data point individually in particular for each point. It will construct a kind of a circle with the point being in the center and having the radius equal to the Epsilon.
MinPoints are the number of points that must exist within ɛ distance from the point.
It is the distance or radius around each object.
Considering below diagram, one circle is drawn whose center is the Query point and radius is ɛ. The query point will become Core point if number of points inside circle is more than MinPoints (in this case MinPoints = 2). So the query point has qualified to become Core point.
If a query point has lesser than MinPoints within ɛ but has a core point within ɛ distance, then it is a Border point.
A point which is neither a Core nor a Border point is called as Noise point. See below diagram.
The DBSCAN will process each and every object/points in this fashion and at the end it will obtain categorization of all the points as either core, border or noise points.
Once the categorization of the points are obtained, the next step is to use them to construct the clusters. DBSCAN take up a core point and then look at the points which are inside its Epsilon radius circle and assign a Cluster label to those points.
So the key idea is to give the same label to all the points inside the circle of a core point.
Multiple iterations will be run for different core points to assign Cluster label, please note algorithm will not assign new Cluster label to those points which have already be considered in earlier iteration.
Problem Statement:You own the mall and want to understand the customers based their past purchasing data. This analysis will help marketing team to target customers with some strategies.
Your data consist of columns like Customer ID, age, gender, annual income and spending score. Spending Score is something you assign to the customer based on your defined parameters like customer behavior and purchasing data.
You can get this dataset from Kaggle.com.
https://www.kaggle.com/vjchoudhary7/customer-segmentation-tutorial-in-python
# DBSCAN Clustering# Importing the librariesimport numpy as npimport matplotlib.pyplot as pltimport pandas as pd# Importing the datasetdataset = pd.read_csv(Mall_customers.csv')data = dataset.iloc[:, [3, 4]].valuesdataset.head()
# visualizing the datasetplt.scatter(data[:, 0], data[:, 1], s = 10, c = 'black')
# Fitting DBSCAN to the dataset and predict the Cluster labelfrom sklearn.cluster import DBSCANdbscan = DBSCAN(eps=5.5, min_samples=4)labels = dbscan.fit_predict(data) np.unique(labels)
# Visualising the clustersplt.scatter(data[labels == -1, 0], data[labels == -1, 1], s = 10, c = 'black')plt.scatter(data[labels == 0, 0], data[labels == 0, 1], s = 10, c = 'blue')plt.scatter(data[labels == 1, 0], data[labels == 1, 1], s = 10, c = 'red')plt.scatter(data[labels == 2, 0], data[labels == 2, 1], s = 10, c = 'green')plt.scatter(data[labels == 3, 0], data[labels == 3, 1], s = 10, c = 'brown')plt.scatter(data[labels == 4, 0], data[labels == 4, 1], s = 10, c = 'pink')plt.scatter(data[labels == 5, 0], data[labels == 5, 1], s = 10, c = 'yellow')      plt.scatter(data[labels == 6, 0], data[labels == 6, 1], s = 10, c = 'silver')plt.xlabel('Annual Income')plt.ylabel('Spending Score')plt.show()
Congratulation, your first iteration for Customer clustering is completed.
As clustering is unsupervised learning, you need to analyze each cluster and have a definition with respect to business data because Clustering is always guided by some business rules. Once your clusters are close to business rules, your model will make sense.
We can also change the value for eps and Min_sample to tune the model and make clusters better in shapes.
Thank you for reading.