-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCA_clustering.R
75 lines (53 loc) · 1.91 KB
/
PCA_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
wine<-read.csv("D:/Assignment/PCA(15th june'20)-20200627T023135Z-001/PCA(15th june_20)/Assignment/wine.csv")
str(wine)
my_data <- scale(wine[,-1])
pca<-princomp(my_data, scores = TRUE)
summary(pca)
pca$scores
new_data<-pca$scores[,1:3]
new_data
# Hierarchial Clustering with PCA
mydata1 <- (new_data)
d <- dist(mydata1, method = "euclidean") #Computing the distance natrix
as.matrix(d)[1:3, 1:3]
fit <- hclust(d, method="ward.D2") # Building the algorithm # try with 'centroid'
plot(fit)
clusters <- cutree(fit, k=3) # cut tree into 4 clusters
# draw dendogram with red borders around the 4 clusters
rect.hclust(fit, k=3, border="red")
#Attach the cluster numbers to Uni
Final_output=data.frame('Type'=wine[,1],'Cluster' =clusters)
#original Data clustering
mydata2 <-my_data
d1 <- dist(mydata2, method = "euclidean") #Computing the distance natrix
as.matrix(d)[1:3, 1:3]
fit1 <- hclust(d1, method="ward.D2") # Building the algorithm # try with 'centroid'
plot(fit1)
clusters1 <- cutree(fit1, k=3) # cut tree into 4 clusters
# draw dendogram with red borders around the 4 clusters
rect.hclust(fit1, k=3, border="red")
#Attach the cluster numbers to Uni
Final_output1=data.frame('Type'=wine[,1],'Cluster' =clusters)
View(Final_output1)
# K-Means Clustering with PCA
install.packages('factoextra')
library(factoextra)
fviz_nbclust(new_data, kmeans, method = "wss") +
labs(subtitle = "Elbow method")
###Cluster algorithm building
km <- kmeans(new_data,3)
km$centers
km$cluster
clust<-data.frame('Type'=wine[,1],'Cluster' =km$cluster)
view(clust)
table(km$cluster)
# K-Means Clustering with original
fviz_nbclust(my_data, kmeans, method = "wss") +
labs(subtitle = "Elbow method")
###Cluster algorithm building
km1 <- kmeans(my_data,3)
km1$centers
km1$cluster
clust1<-data.frame('Type'=wine[,1],'Cluster' =km1$cluster)
view(clust1)
table(km1$cluster)