-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_3_1a.R
54 lines (46 loc) · 1.43 KB
/
ex9_3_1a.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
# exercise 9.1.3
rm(list=ls())
source("setup.R")
# Load data
library(R.matlab)
dat <- readMat(file.path('Data', 'faithful.mat'))
X <- dat$X
N <- dat$N
attributeNames <- as.vector(unlist(dat$attributeNames))
M <- dat$M
y <- dat$y
C <- dat$C
classNames <- as.vector(unlist(dat$classNames))
# substitute spaces with dots to make handling of columns in data matrix easier
attributeNames <- gsub(' ', '.', attributeNames)
Xdf <- data.frame(X)
colnames(Xdf) <- attributeNames
## K-means clustering
# Maximum number of clusters
K = 2;
# Allocate variables
Entropy = rep(NA, times=K)
Purity = rep(NA, times=K)
Rand = rep(NA, times=K)
Jaccard = rep(NA, times=K)
for(k in 1:K){
# Run k-means
kmeansres = kmeans(Xdf, k, iter.max=100);
i <- kmeansres$cluster
# Compute cluster validities
res <- clusterval(y, i);
Entropy[k] <- res$Entropy
Purity[k] <- res$Purity
Rand[k] <- res$Rand
Jaccard[k] <- res$Jaccard
}
## Plot results
cols <- c('blue', 'green', 'red', 'lightblue')
maxy <- max(c(-Entropy, Purity, Rand, Jaccard))
miny <- min(c(-Entropy, Purity, Rand, Jaccard))
plot(c(1,K), c(miny, maxy), type='n', main='Cluster validity', xlab='Number of clusters', ylab='')
lines(1:K, -Entropy, col=cols[1]);
lines(1:K, Purity, col=cols[2]);
lines(1:K, Rand, col=cols[3]);
lines(1:K, Jaccard, col=cols[4]);
legend('bottomright', legend=c('Negative Entropy', 'Purity', 'Rand', 'Jaccard'), fill=cols)