-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHierarchical Clustering.R
55 lines (42 loc) · 1.78 KB
/
Hierarchical Clustering.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Hierarchical Clustering
#- There are two types of Hierarchical Clustering;
# 1. Agglomerative
# - It is the bottom up approach.
# 2. Divisive
# ** Steps for Agglomerative HC **
# Step 1 : Make each data point a single point cluster -> That forms N clusters.
# Step 2 : Take the two closest data points and make them one cluster -> That forms (N - 1) clusters.
# Step 3 : Take the two closest clusters and make them one cluster -> That forms (N - 2) clusters.
# Step 4 : Repeat Step 3 until there is only one cluster. Than FIN.
# - **Option to choose Distance between clusters**
# 1. Closest Point
# 2. Furthest Point
# 3. Average Distance
# 4. Distance between Centroids.
# ----------------------------------------------------- Importing Data ------------------------------------------- #
dataset = read.csv('Mall_Customers.csv')
# Selecting particular columns
dataset = dataset[4:5]
#----------------------------- Using the Dendogram to find the optimal number of clusters ----------------------- #
dendrogram = hclust(dist(dataset, method = 'euclidean'), method = 'ward.D')
plot(dendrogram,
main = "Dendrogram",
xlab = "Customer",
ylab = "Eculidean Distance")
# --------------------------------- Fitting Hierarchical Clustering to the Mall dataset -------------------------- #
hc = hclust(dist(dataset, method = 'euclidean'), method = 'ward.D')
y_hc = cutree(hc, 5)
y_hc
# -------------------------------------------- Visualising the Cluster ------------------------------------------- #
library(cluster)
clusplot(dataset,
y_hc,
lines = 0,
shade = TRUE,
color = TRUE,
labels= 2,
plotchar = FALSE,
span = TRUE,
main = paste('Clusters of customers'),
xlab = 'Annual Income',
ylab = 'Spending Score')