How to perform hierrarchical clustering in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to perform hierrarchical clustering in R?

How to perform hierrarchical clustering in R?

This recipe helps you perform hierrarchical clustering in R

0

Recipe Objective

Organised logical groups of information is preferred over unorganised data by people. For example, anyone finds it easier to remember information when it is clustered together by taking its common characteristics into account.

Likewise, A machine learning technique that provides a way to find groups/clusters of different observations within a dataset is called Clustering. In this technique due to the absence of response variable, it is considered to be an unsupervised method. This implies that the relationships between 'n' number of observations is found without being trained by a response variable. The few applications of Clustering analysis are ​

  1. Customer segmentation: process for dividing customers into groups based on similar characteristics.
  2. Stock Market Clustering based on the performance of the stocks
  3. Reducing Dimensionality

There are two most common Clustering algorithm that is used: ​

  1. KMeans Clustering: commonly used when we have large dataset
  2. Heirarchical Clustering: commonly used when we have small dataset

Heirarchical Clustering is an unsupervised machine learning technique that aims to groups the unlabeled dataset by building a heirarcy of clusters. It is relatively slow compared to heirarchichal clustering. There are two types of Heirarchical clustering algorithm: Divisive (top-down appraoch) and Agglomerative (bottom-up approach). ​

The most commonly used is the agglomerative algorithm. In this algorithm, data is split into n clusters where n is the observations in the dataset initially. The next step is the calculation of euclidean distances between data points. Then, the number of clusters are reduced because the two clusters merges into one in an iterative manner considering the distances between them. This process of merging stops when only one cluster remains. The hierarchy of clusters is represented by Dendrogram (a tree-like structure) ​

This recipe demonstrates Heirarchical Clustering using Agglomerative algorithm on a real-life Mall dataset to carry out customer segmentation in R-language. ​

STEP 1: Importing Necessary Libraries

# For Data Manipulation library(tidyverse) # For Clustering algorithm library(cluster)

STEP 2: Loading the Dataset

Dataset description: It is a basic data about the customers going to the supermarket mall. This can be used for customer segmentation. There are 200 observations(customers) and no missing data.

It consists of four columns ie. measured attrutes: ​

  1. CustomerID is the customer identification number.
  2. Gender is Female and Male.
  3. Age is the age of customers.
  4. Annual Income (k) is the annual income of clients in thousands of dollars.
  5. Spending Score (1-100) is the spending score assigned by the shopping center according to the customer's purchasing behavior
# creating a dataframe customer_seg customer_seg = read.csv('R_241_Mall_Customers.csv') # getting the required information about the dataset glimpse(customer_seg)
Observations: 200
Variables: 5
$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
$ Gender                  Male, Male, Female, Female, Female, Female, ...
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, ...
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, ...
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99,...

For the simplicity of demonstrating heirarchichal Clustering with visualisation, we will only consider two measured attributes (Age and Annual Income). ​

# assigning columns 3 and 4 to a new dataset customer_prep customer_prep = customer_seg[3:4]

STEP 3: Data Preprocessing (Scaling)

This is a pre-modelling step. In this step, the data must be scaled or standardised so that different attributes can be comparable. Standardised data has mean zero and standard deviation one. we do thiis using scale() function

Note: Scaling is an important pre-modelling step which has to be mandatory

# scaling the dataset customer_prep = scale(customer_prep) customer_prep %>% head()
Age		Annual.Income..k..
-1.4210029	-1.734646
-1.2778288	-1.734646
-1.3494159	-1.696572
-1.1346547	-1.696572
-0.5619583	-1.658498
-1.2062418	-1.658498

STEP 4: Calculating the distances between the observations/datapoints

We use the dist() function to carry this out task. It calculates the euclidean distances between the datapoints. We create an object 'distances' to store this information.

distances = dist(customer_prep, method = 'euclidean') distances %>% head()
0.143174096235284 0.0810822191043143 0.288868323164455 0.862412934253182 0.227861432257651 1.15107392658714

STEP 5: Performing Heirarchical clustering

We will use hclust() function in cluster library in R to perform this. The two arguements used below are:

  1. Distances between the point
  2. method of evaluation: Ward's method (Ward.D)
# This is an assignment of random state set.seed(50) # creation of an object km which store the output of the function kmeans h_clust = hclust(distances, method = 'ward.D') # plotting the dendrogram to represent Heirarchical Clustering plot(h_clust, main = paste('Dendrogram'), xlab = 'Customers', ylab = 'Euclidean distances')

STEP 6: Data Visualation using scatter plot with specified number of clusters

We use cutree() function in cluster library to specify the number of clusters to be formed. This function cuts the dendrogram in such a way that only the specified number of clusters are obtained. In our case, we will use 5 number of clusters.

y = cutree(h_clust, 5) # y is a vector of integers that showcases the cluster in which each observation lies.

We use clusplot to plot theclusters in a scatter plot w.r.t Age and Income

# using clusplot() function with various arguements to plot the clusters clusplot(customer_prep, y, shade = TRUE, color = TRUE, span = TRUE, main = paste('Clusters of customers'), xlab = 'Age', ylab = 'Annual Income')

This plot helps us to analyse the different clusters of customers formed so that we can target the respective clusters seperately in our marketing strategy.

Relevant Projects

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.