A-Z Clustering.
By: Bob Rupak Roy
Tags: Deep Learning, Clustering, Advanced Analytics, Machine Learning, Data Mining

First let’s start with K-means Clustering, The Widely used isn’t it?

What is clustering? Clustering is a process of grouping set of observations that the same group are similar to each other than to those of others within large population. Example Types of car sub, sedan, sports, economic etc.

Steps for performing clustering.

1st determine the number of clusters i.e. Values of ‘K’

2nd now the algorithm will assign the observations/clusters to the seeds based on their close proximity.

Now how the algorithm will decide which observation is closed to which seed either the left or right seed?

3rd for that draw a perpendicular bisector between the lines from the mid-point.

So any point to the left is closer to seed1(cluster) & any point to the right is closer to seed 2 (cluster)

Similarly when we draw a perpendicular() to seed 3 and any point to the above is closer to seed 3 & any point to the below is closer to seed 1

4th and the algorithm will create groups i.e. the clusters. But how we know these are the most optimal clusters based on the seeds. It calculates the centroid of these clusters i.e. mid-points which is calculated by averaging the midpoints of each clusters. This is how K-means of clustering technique works.

3 Methods to calculate the distance

1.) Single Linkage: The distance between 2 clusters is determined by the distance betweent eh closet members of those 2 clusters.

2.) Complete Linkage: in complete linkage the distance is given by the most distant between the closet members of the 2 clusters.

3.) Centroid Linkage: in centroid linkage the distance between centroids that is the mid-points of the clusters is used to determine the distance between clusters.

Data Preparation for clustering

Scaling: is a concept of bringing together different measures of variables into a one uniform scale of measure. In other words maintaining a uniform identity to understand the variance between them.

Because the algorithm will think it a variance rather than a different scale of measure. So in order to do effective clustering we need to adjust the variable to a common scale

= Scaled between (0–1)

4 Methods of Scaling

Divide each variable by the range after subtracting the lowest Example: Machinery Highest = 93

Lowest = 12

Range = 93–12 = 81

Divide each variable by the range after subtracting the lowest

=> 12–12/81=0.0 => 46–12/81=34/81=0.41

2. Divide each variable by the mean of the variable Example: Machinery Mean= 50.33 

=> 12/50.33= 0.28 

=> 46/50.33= 0.91

3. Normalization / Z scoring of variables (commonly used appropriate method)

— Subtract mean from each variable then divide by standard deviation Example: Machinery 

Variable1 — mean/standard deviation=12–50.33/40.67 = -0.94 

Variable1 — mean/standard deviation=46–50.33/40.67 = -0.11

Weighing: concept of adjusting the values of the variable as per their relative importance.

Frist step before weighing is to scale all the variables & standardize them. Now we can tell the algorithm which variables are more important than the others.

For example if we assign a weight of 2 to a variable product category after having them in a common scale then we will just multiply X-times ( like multiplied by 3) in order generate a new weighted variable.

4. Others: There are many other ways to scale the data without disturbing the variance, once such is Logarithm by converting the values in log which also ranges from 0 to 1

Don‟t worry we won‟t be manually calculating each of them instead we will use functions that will do the scaling for us.

Handling Categorical Data

Clustering algorithms can handle continuous data.

B ut what if it contains Categorical Data Types like

Nominal (no order)

Furniture A VS Furniture B

High vs Med vs Low

Ordinal

Tea VS Coffee Rating: 1 to 5, Yes vs No

So the easiest way to solve this is to create Binary Variables

Binary Variables are also a type of Categorical Variables but it is in 0,1binary format. Therefore it can handled by clustering algorithm.

Evaluating Clusters

Difference in Centroid Average distance between the cluster centroids divided by the average distance between the members and the cluster centroid 2. Profiling Clusters Profiling is an another way to identify each clusters & why it is different the rest. For example take the mean of each variable within the cluster & compare it to the mean of the same variable of the population. Here too i will show you an another way to evaluate the clusters using special packages and functions.

Since there is no target variable in clustering it can be highly used to identify hidden patterns in the data without any hypothesis. It can also be categorized as Unsupervised Machine Learning

Some of the few use cases of clustering are Segmentation like Customer, Product Segmentation, Outlier Detection or Fraud Detection or Improving performance of the other techniques by segmenting the data into smaller groups

Improving Clusters: in case if we have one or few strong clusters, remove the strong cluster and recalculate the cluster from the remaining to detect new clusters.