Data Mining Using Python Lab
Data Mining Using Python Lab
LAB MANUAL
DEPARTMENT OF CSE
(DATA SCIENCE)
Prepared by
ASSISTANT PROFESSOR
Lab Manual
Department : Computer Science & Engineering(AI & ML) and Data Science
Course Objective:
● Practical exposure on implementation of well-known data mining algorithms
●Learning performance evaluation of data mining algorithms in a supervised and an
unsupervised setting.
Course Outcomes:
Upon successful completion of the course, the student will be able to:
● Apply preprocessing techniques on real world datasets
● Apply apriori algorithm to generate frequent itemsets.
● Apply Classification and clustering algorithms on diferent datasets.
Note: Use python library scikit-learn wherever necessary
LIST OF PROGRAMS
1. Demonstrate the following data preprocessing tasks using python libraries. a) Loading the
dataset b) Identifying the dependent and independent variables c) Dealing with missing
data
Importing the
pandas import
pandas as pd
dataset = pd.read_excel("age_salary.xls")
Having seen the data we can clearly identify the dependent and independent
[Link] we just have 2 factors, age and [Link] is the dependent factor
that changes with the independent factor [Link] let’s classify them
programmatically.
We have already noticed the missing fields in the data denoted by “nan”. Machine
learning models cannot accommodate missing fields in the data they are provided
[Link] the missing fields must be filled with values that will not affect the variance
of the data or make it more noisy.
The scikit-learn library’s SimpleImputer Class allows us to impute the missing fields
in a dataset with valid data. In the above code, we have used the default strategy
for filling missing values which is the mean. The imputer can not be applied on 1D
arrays and since Y is a 1D array, it needs to be converted to a compatible
[Link] reshape functions allows us to reshape any [Link] fit_transform()
method will fit the imputer object and then transforms the arrays.
Output
2 Demonstrate the following data preprocessing tasks using python library
a) Dealing with categorical data b) Scaling the features
c) Splitting dataset into Training and Testing Sets
When dealing with large and real-world datasets, categorical data is almost
[Link] variables represent types of data which may be
divided into groups. Examples of categorical variables are race, sex, age
group, educational level etc. These variables often has letters or words as
its values. Since machine learning models are all about numbers and
calculations , these categorical variables need to be coded in to numbers.
Having coded the categorical variable into numbers may just not be enough.
For example, consider the dataset below with 2 categorical features nation
and purchased_item. Let us assume that the dataset is a record of how age,
salary and country of a person determine if an item is purchased or [Link]
purchased_item is the dependent factor and age, salary and nation are the
independent factors.
actor and age, salary and nation are the independent factors.
It has 3 countries listed. In a larger dataset, these may be large groups of
data. Since countries don’t have a mathematical relation between them
unless we are considering some known factors such as size or population
etc , coding them in numbers will not work, as a number may be less than or
greater than another number. Dummy variables are the solution. Using one
hot encoding we will create a dummy variable for each of the category in the
column. And uses binary encoding for each dummy variable. We do not need
to create dummy variables for the feature purchased_item as it has only 2
categories either yes or no.
dataset = pd.read_csv("[Link]")
X = [Link][:,[0,2,3]].values Y
= [Link][:,1].values
from [Link] import LabelEncoder,OneHotEncoder
le_X = LabelEncoder()
X[:,0] = le_X.fit_transform(X[:,0])
columnTransformer = ColumnTransformer([('encoder', OneHotEncoder(),
[0])], remainder='passthrough')
X=[Link](columnTransformer.fit_transform(X),dtype=[Link])
print(X)
Output
The the first 3 columns are the dummy features representing Germany,India
and Russia [Link] 1’s in each column represent that the person
belongs to that specific country.
Y = le_X.fit_transform(Y)
Output:
All machine learning models require us to provide a training set for the
machine so that the model can train from that data to understand the
relations between features and can predict for new [Link] we
are provided a single huge dataset with too much of observations ,it is a good
idea to split the dataset into to two, a training_set and a test_set, so that we
can test our model after its been trained with the training_set.
Scikit-learn comes with a method called train_test_split to help us with this task.
The StandardScaler class from the scikit-learn library can help us scale the dataset.
sc_y = StandardScaler()
Y_train = Y_train.reshape((len(Y_train), 1))
Y_train = sc_y.fit_transform(Y_train)
Y_train = Y_train.ravel()
Output
X_train before scaling :
X_train after scaling :
s
Similarity based methods determine the most similar objects with the
highest values as it implies they live in closer neighborhoods.
Pearson’s Correlation
where
The Pearson’s correlation can take a range of values from -1 to +1. Only
having an increase or decrease that are directly related will not lead to a
Pearson’s correlation of 1 or -1.
Implementation in Python:
Pearsons correlation: 0.810
Cosine Similarity
The cosine similarity calculates the cosine of the angle between two
vectors. In order to calculate the cosine similarity we use the following
formula:
Recall the cosine function: on the left the red vectors point at different
angles and the graph on the right shows the resulting function.
Accordingly, the cosine similarity can take on values between -1 and +1.
If the vectors point in the exact same direction, the cosine similarity is
+1. If the vectors point in opposite directions, the cosine similarity is -1.
The cosine similarity is very popular in text analysis. It is used to
determine how similar documents are to one another irrespective of their
size. The TF-IDF text analysis technique helps convert the documents
into vectors where each value in the vector corresponds to the TF-IDF
score of a word in the document. Each word has its own axis, the cosine
similarity then determines how similar the documents are.
Implementation in Python
accard Similarity
We can see that the Jaccard similarity divides the size of the intersection
by the size of the union of the sample sets.
Both Cosine similarity and Jaccard similarity are common metrics for
calculating text similarity. Calculating the Jaccard similarity is
computationally more expensive as it matches all the terms of one
document to another document. The Jaccard similarity turns out to be
useful by detecting duplicates.
Implementation in Python
Euclidean Distance
Implementation in Python
Manhattan Distance
Implementation in Python
Import pandas as pd
Import numpy as np
%matplotlib inline
housing = pd.read_csv('[Link]')
[Link]
If you wanted to print out from the bottom upwards, you would use the
“tail” function instead.
By default will print out 5 rows. For this particular data set, this means
rows 20635 to 20639.
Next, we should try and plot the data. We can do so with the following command:
Now, it is time to actually start to analyze the data. We can start this off
by running a particular directive.
The overall data will be split up into 80% as train and 20% as test. The
“y-values” will be the “median_house_value,” and the “x-values” will be
the “median_income.”
Next, impose a linear regression. This can be done with the following.
regr = LinearRegression()
This will call LinearRegression(), and then allow us to use our own data to predict.
[Link]([Link](x_train).reshape(-1,1), y_train)
This will shape the model using one predictor. Reshape is being applied to
change it from pandas to NumPy, and finally into a vector. (Reshape
transverses it from a single dimension matrix to a vertical shape.)
We can compare our predictions with the actual values. This can be
done with the code that follows.
Compare the first values. For the actual, it is equal to 252,900. Our
prediction, on the other hand, guesses approximately 180,156. (That is
not bad, but that is not great!)
This will show how far off the values are. This is showing the predicted
value minus the actual test value for all the data points.
Then, we should plot with a histogram to see how “off” each value is. This
can be done with the following command.
Lastly, we should use root mean squared error to find the error. This
can be done as follows:
mean_squared_error(y_test, preds) ** 0.5
Usually, a decision tree is drawn upside down, with the root node at the top
and the leaf nodes at the bottom. A decision tree usually contains 3 types of
nodes.
1. Root node: The very top node that represents the entire population or sample.
2. Decision nodes: Sub-nodes that split from the root node.
3. Leaf nodes: Nodes with no children, also known as terminal nodes.
Decision trees work in a step-wise manner, meaning that they perform a step-
by-step process instead of following a continuous process. Decision trees follow
a tree-like structure, where the nodes of a tree are split using the features
based on defined criteria. The main criteria based on which decision trees split
are:
Dataset to apply decision tree algorithms in Python. You can follow the steps
below to create a feasible and useful decision tree:
import pandas as pd
import numpy as np
from [Link] import load_iris
from [Link] import accuracy_score
• In lines 1 to 4, we import the necessary libraries to read and analyze the dataset.
• In line 7, we store the IRIS dataset in the variable data. Since the
sklearn library contains the IRIS dataset by default, you do not need
to upload it again.
• From lines 22 to 24, we create a decision tree classifier and fit it against
the training dataset. By default, the criterion parameter is set to gini.
From lines 27 to 30, we import the “accuracy_score” module and
implement the same to find the accuracy of
both the training and test data.
• In lines 28 and 29, we get the output as 1, i.e., 100% for training
data and 0.947, which is approximately 95%, for the test dataset
6. Apply Naive Bayes classification algorithm on any dataset?
Naive Bayes is the most straightforward and fast classification algorithm, which is suitable
for a large chunk of data. Naive Bayes classifier is successfully used in various applications
such as spam filtering, text classification, sentiment analysis, and recommender systems. It
uses Bayes theorem of probability for prediction of unknown class.
Whenever you perform classification, the first step is to understand the problem and identify
potential features and label. Features are those characteristics or attributes which affect the
results of the label. For example, in the case of a loan distribution, bank manager's identify
customer’s occupation, income, age, location, previous loan history, transaction history, and
credit score. These characteristics are known as features which help the model classify
customers.
The classification has two phases, a learning phase, and the evaluation phase. In the learning
phase, classifier trains its model on a given dataset and in the evaluation phase, it tests the
classifier performance. Performance is evaluated on the basis of various parameters such as
accuracy, error, precision, and recall.
Naive Bayes is a statistical classification technique based on Bayes Theorem. It is one of the
simplest supervised learning algorithms. Naive Bayes classifier is the fast, accurate and
reliable algorithm. Naive Bayes classifiers have high accuracy and speed on large datasets.
Naive Bayes classifier assumes that the effect of a particular feature in a class is independent
of other features. For example, a loan applicant is desirable or not depending on his/her
income, previous loan and transaction history, age, and location. Even if these features are
interdependent, these features are still considered independently. This assumption simplifies
computation, and that's why it is considered as naive. This assumption is called class
conditional independence.
• P(h): the probability of hypothesis h being true (regardless of the data). This is known
as the prior probability of h.
• P(D): the probability of the data (regardless of the hypothesis). This is known as the
prior probability.
• P(h|D): the probability of hypothesis h given the data D. This is known as posterior
probability.
• P(D|h): the probability of data d given that the hypothesis h was true. This is known as
posterior probability.
Defining Dataset
In this example, you can use the dummy dataset with three columns: weather, temperature,
and play. The first two are features(weather, temperature) and the other is the label.
# Assigning features and label variables
weather=['Sunny','Sunny','Overcast','Rainy','Rainy','Rainy','Overcast','Sunny','Sunny',
'Rainy','Sunny','Overcast','Overcast','Rainy']
temp=['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild','Cool','Mild','Mild','Mild','
Hot','Mild']
play=['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']
Encoding Features
First, you need to convert these string labels into numbers. for example: 'Overcast', 'Rainy',
'Sunny' as 0, 1, 2. This is known as label encoding. Scikit-learn provides LabelEncoder
library for encoding labels with a value between 0 and one less than the number of discrete
classes.
# Import LabelEncoder
from sklearn import preprocessing
#creating labelEncoder
le = [Link]()
# Converting string labels into numbers.
weather_encoded=le.fit_transform(weather)
print(weather_encoded)
[2 2 0 1 1 1 0 2 2 1 2 0 0 1]
Temp: [1 1 1 2 0 0 0 2 0 2 2 2 1 2]
Play: [0 0 1 1 1 0 1 0 1 1 1 1 1 0]
Now combine both the features (weather and temp) in a single variable (list of tuples).
#Combinig weather and temp into single listof tuples
features=zip(weather_encoded,temp_encoded)
print features
Generating Model
• Perform prediction
• #Import Gaussian Naive Bayes model
• from sklearn.naive_bayes import GaussianNB
•
• #Create a Gaussian Classifier
• model = GaussianNB()
•
• # Train the model using the training sets
• [Link](features,label)
•
• #Predict Output
• predicted= [Link]([[0,2]]) # 0:Overcast, 2:Mild
• print("Predicted Value:", predicted)
Predicted value:1
7. Generate frequent itemsets using Apriori Algorithm in python and also generate
association rules for any market basket data
Apriori is an algorithm for frequent item set mining and association rule learning over
relational databases. It proceeds by identifying the frequent individual items in the database
and extending them to larger and larger item sets as long as those item sets appear
sufficiently often in the database. The frequent item sets determined by Apriori can be used
to determine association rules which highlight general trends in the database: this has
applications in domains such
as market basket analysis.
Apriori algorithm is the perfect algorithm to start with association analysis as it is not just
easy to understand and interpret but also to implement.
Python has many libraries for apriori implementation. One can also implement the algorithm
from scratch. But wait, there is mlxtend for the rescue. This library has beautiful
implementation of apriori and it also allows to extract association rules from the result.
Required Library
import pandas as pd
import numpy as np
from mlxtend.frequent_patterns import apriori, association_rules
import [Link] as plt
df = pd.read_csv('retail_dataset.csv')
## Print first 10 rows
[Link](10)
Each row of the dataset represents items that were purchased together
on the same day at the same [Link] dataset is a sparse dataset as
relatively high percentage of data is NA or NaN or equivalent.
These NaNs make it hard to read the table. Let’s find out how many
unique items are actually there in the table.
There are only 9 items in total that make up the entire dataset.
Data Preprocessing
Applying Apriori
apriori module from mlxtend library provides fast and efficient apriori
implementation.
Parameters
• low_memory :
• If True, uses an iterator to search for combinations above min_support. Note that
while low_memory=True should only be used for large dataset if memory
resources are limited, because this implementation is approx. 3–6x slower
than the default.
1. Support vs Confidence
[Link] vs Lift
Lift vs Confidence
8. Apply K-Means Clustering algorithm on any dataset
Step-3: Assign each data point, based on their distance from the randomly
selected points (Centroid), to the nearest/closest centroid which will form the
predefined clusters.
Step-5: Repeat step no.3, which reassign each datapoint to the new closest
centroid of each cluster.
Step-7: FINISH
STEP 1:Let’s choose number k of clusters, i.e., K=2, to segregate the dataset
and to put them into different respective clusters. We will choose some random
2 points which will act as centroid to form the cluster.
STEP 2: Now we will assign each data point to a scatter plot based on its
distance from the closest K-point or centroid. It will be done by drawing a
median between both the centroids. Consider the below image:
STEP 3: points left side of the line is near to blue centroid, and points to the
right of the line are close to the yellow centroid. The left one Form cluster
with blue centroid and the right one with the yellow centroid.
STEP 4:repeat the process by choosing a new centroid. To choose the new
centroids, we will find the new center of gravity of these centroids, which is
depicted below :
STEP 5: Next, we will reassign each datapoint to the new centroid. We will repeat
the same process as above (using a median line). The yellow data point on the
blue side of the median line will be included in the blue cluster
STEP 6: As reassignment has taken place, so we will repeat the above step of
finding new centroids.
STEP 7: We will repeat the above process of finding the center of gravity of
centroids, as being depicted below
STEP 8: After Finding the new centroids we will again draw the median line and
reassign the data points, like the above steps.
STEP 9: We will finally segregate points based on the median line, such that two
groups are being formed and no dissimilar point to be included in a single
group
The number of clusters that we choose for the algorithm shouldn’t be random. Each
and Every cluster is formed by calculating and comparing the mean distances of each
data points within a cluster from its centroid.
We Can Choose the right number of clusters with the help of the Within-Cluster-
Sum-of- Squares (WCSS) method.
WCSS Stands for the sum of the squares of distances of the data points in each
and every cluster from its centroid.
The main idea is to minimize the distance between the data points and the
centroid of the clusters. The process is iterated until we reach a minimum
value for the sum of distances.
To find the optimal value of clusters, the elbow method follows the below
steps:
4 The sharp point of bend or a point( looking like an elbow joint ) of the plot like
an arm, will be considered as the best/optimal value of K
Python Implementation
Clustering
kmeans =
KMeans(3)
[Link](x)
Clustering Results
identified_clusters = kmeans.fit_predict(x)
identified_clusters
array([1, 1, 0, 0, 0, 2])
data_with_clusters = [Link]()
data_with_clusters['Clusters'] = identified_clusters
[Link](data_with_clusters['Longitude'],data_with_clusters['Latitude'],c=data_
with_clusters['Clusters'],cmap='rainbow')
Trying different method ( to find no .of clusters to be selected)
number_clusters = range(1,7)
[Link](number_clusters,wcss)
[Link]('The Elbow title')
[Link]('Number of clusters')
[Link]('WCSS')
we can choose 3 as no. of clusters, this method shows what is the good number of
clusters.
9. Apply Hierarchical Clustering algorithm on any dataset
Let’s say we have the below points and we want to cluster them
cluster:
Now, based on the similarity of these clusters, we can combine the most similar
clusters together and repeat this process until only a single cluster is left:
clustering Agglomerative
Hierarchical Clustering
Then, at each iteration, we merge the closest pair of clusters and repeat this
step until only a single cluster is left:
We are merging (or adding) the clusters at each step, right? Hence, this type of
clustering is also known as additive hierarchical clustering.
So, it doesn’t matter if we have 10 or 1000 data points. All these points will
belong to the same cluster at the beginning:
Now, at each iteration, we split the farthest point in the cluster and repeat this
process until each cluster only contains a single point:
We are splitting (or dividing) the clusters at each step, hence the name divisive
hierarchical clustering.
Agglomerative Clustering is widely used in the industry and that will be the
focus in this article. Divisive hierarchical clustering will be a piece of cake once
we have a handle on the agglomerative type.
Here’s one way to calculate similarity – Take the distance between the
centroids of these clusters. The points having the least distance are referred to
as similar points and we can merge them. We can refer to this as a distance-
based algorithm as well (since we are calculating the distances between the
clusters).
In hierarchical clustering, we have a concept called a proximity matrix. This stores
the distances between each point. Let’s take an example to understand this
matrix as well as the steps to perform hierarchical clustering.
Suppose a teacher wants to divide her students into different groups. She has
the marks scored by each student in an assignment and based on these marks,
she wants to segment them into groups. There’s no fixed target here as to how
many groups to have. Since the teacher does not know what type of students
should be assigned to which group, it cannot be solved as a supervised learning
problem. So, we will try to apply hierarchical clustering here and segment the
students into different groups.
First, we will create a proximity matrix which will tell us the distance between
each of these points. Since we are calculating the distance of each point from
each of the other points, we will get a square matrix of shape n X n (where n is
the number of observations).
The diagonal elements of this matrix will always be 0 as the distance of a point
with itself is always 0. We will use the Euclidean distance formula to calculate
the rest of the distances. So, let’s say we want to calculate the distance
between point 1 and 2:
√(10-7)^2 = √9 = 3
Similarly, we can calculate all the distances and fill the proximity matrix.
Steps to Perform Hierarchical Clustering
Different colors here represent different clusters. You can see that we have 5
different clusters for the 5 points in our data.
Step 2: Next, we will look at the smallest distance in the proximity matrix and
merge the points with the smallest distance. We then update the proximity
matrix:
Here, the smallest distance is 3 and hence we will merge point 1 and 2:
Let’s look at the updated clusters and accordingly update the proximity matrix:
Here, we have taken the maximum of the two marks (7, 10) to replace the marks
for this cluster. Instead of the maximum, we can also take the minimum value or
the average values as well. Now, we will again calculate the proximity matrix for
these clusters:
So, we will first look at the minimum distance in the proximity matrix and then
merge the closest pair of clusters. We will get the merged clusters as shown
below after repeating these steps:
We started with 5 clusters and finally have a single cluster. This is how
agglomerative hierarchical clustering works. But the burning question still
remains – how do we decide the number of clusters? Let’s understand that in
the next section.
Ready to finally answer this question that’s been hanging around since we
started learning? To get the number of clusters for hierarchical clustering, we
make use of an awesome concept called a Dendrogram.
Here, we can see that we have merged sample 1 and 2. The vertical line
represents the distance between these samples. Similarly, we plot all the steps
where we merged the clusters and finally, we get a dendrogram like this:
We can clearly visualize the steps of hierarchical clustering. More the distance of
the vertical lines in the dendrogram, more the distance between those clusters.
Now, we can set a threshold distance and draw a horizontal line (Generally, we try
to set the threshold in such a way that it cuts the tallest vertical line). Let’s set this
threshold as 12 and draw a horizontal line:
The number of clusters will be the number of vertical lines which are being
intersected by the line drawn using the threshold. In the above example, since the
red line intersects 2 vertical lines, we will have 2 clusters. One cluster will have
a sample (1,2,4) and the other will have a sample (3,5). Pretty straightforward,
right?
Let’s explore the data first and then apply Hierarchical Clustering to segment
import pandas as pd
import numpy as np
%matplotlib inline
data =
pd.read_csv('Wholes
al e customers
[Link]')
[Link]()
There are multiple product categories – Fresh, Milk, Grocery, etc. The values
represent the number of units purchased by each client for each product. Our
aim is to make clusters from this data that can segment similar clients together.
We will, of course, use Hierarchical Clustering for this problem.
So, let’s first normalize the data and bring all the variables to the same scale:
data_scaled = [Link](data_scaled,
columns=[Link])
Here, we can see that the scale of all the variables is almost similar. Now, we
are good to go. Let’s first draw the dendrogram to help us decide the number
of clusters for this particular problem:
import [Link] as
[Link]("Dendrograms")
dend =
[Link]([Link](data_scaled,
method='ward'))
The x-axis contains the samples and y-axis represents the distance between
these samples. The vertical line with maximum distance is the blue line and
hence we can
decide a threshold of 6 and cut the dendrogram:
[Link](figsize=(10,
7))
[Link]("Dendrograms")
dend =
[Link]([Link](data_scaled,
method='ward'))
We have two clusters as this line cuts the dendrogram at two points. Let’s
now apply hierarchical clustering for 2 clusters:
cluster = AgglomerativeClustering(n_clusters=2,
affinity='euclidean', linkage='ward')
cluster.fit_predict(data_scaled)
We can see the values of 0s and 1s in the output since we defined 2 clusters.
0 represents the points that belong to the first cluster and 1 represents points
in the second cluster. Let’s now visualize the two clusters:
[Link](figsize=(10, 7))
[Link](data_scaled['Milk'],
data_scaled['Grocery'], c=cluster.labels_)
Awesome! We can clearly visualize the two clusters here. This is how we can
implement hierarchical clustering in Python.
[Link] DBSCAN clustering algorithm on any dataset.
We will be using the Deepnote notebook to run the example. It comes with pre-installed Python
packages, so we just have to import NumPy, pandas, seaborn, matplotlib, and sklearn.
import numpy as np
import pandas as pd
import seaborn as sns
import [Link] as plt
from [Link] import DBSCAN
We are using Mall Customer Segmentation Data from Kaggle. It contains customers' age, gender,
income, and spending score. We will be using these features to create various clusters.
First, we will load the dataset using pandas `read_csv`. Then, we will select three columns (‘Age',
'Annual Income (k$)', 'Spending Score (1-100)') to create the X_train dataframe.
df = pd.read_csv('Mall_Customers.csv')
X_train = df[['Age', 'Annual Income (k$)', 'Spending Score (1-100)']]
We will fit X_train on the DBSCAN algorithm with eps 12.5 and min_sample 4. After that, we will
create a DBSCAN_dataset from X_train and create a ‘Cluster’ column using clustering.labels_.
clustering = DBSCAN(eps=12.5, min_samples=4).fit(X_train)
DBSCAN_dataset = X_train.copy()
DBSCAN_dataset.loc[:,'Cluster'] = clustering.labels_
To visualize the distribution of clusters, we will use value_counts() and convert it into a dataframe.
As you can see, we have 5 clusters and 1 outlier. The `0` cluster has the largest size with 112 rows.
DBSCAN_dataset.Cluster.value_counts().to_frame()
In this section, we will use the above information and visualize the scatter plot.
There are two plots: “Annual Income vs. Spending Score” and “Annual Income vs. Age.” The clusters
are defined by colors, and the outliers are defined as small black dots.
The visualization clearly shows how each customer is part of one of the 5 clusters, and we can use this
information to give high-end offers to customers with purple clusters and cheaper offers to customers
with dark green clusters.
outliers = DBSCAN_dataset[DBSCAN_dataset['Cluster']==-1]
fig2, (axes) = [Link](1,2,figsize=(12,5))
data=DBSCAN_dataset[DBSCAN_dataset['Cluster']!=-1],
data=DBSCAN_dataset[DBSCAN_dataset['Cluster']!=-1],
[Link](axes[0].get_legend().get_texts(), fontsize='12')
[Link](axes[1].get_legend().get_texts(), fontsize='12')
[Link]()
Conclusion
DBSCAN is one of the many algorithms that is used for customer segmentation. You can use K-means
or Hierarchical clustering to get even better results. The clustering algorithms are generally used for
recommendation engines, market and customer segmentation, social network Analysis, and document
analysis.
In this blog, we have learned the basics of the density-based algorithm DBCAN and how we can use it
to create customer segmentation using scikit-learn. You can improve the algorithm by finding
optimal eps and min_samples using silhouette score and heatmap.