-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmean-Lloyd.R
37 lines (32 loc) · 987 Bytes
/
Kmean-Lloyd.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
require(fields)
Kmean <- function(X,K) {
rand<-sample(1:nrow(X),K)
Ctr<-X[rand,]
#one can combine these two steps into one single step as following.
# Ctr <- X[sample.int(nrow(X),size = K, replace = FALSE),]
cor_new <- rep(0,nrow(X))
cor_old <- rep(-1,nrow(X))
findmean<-function(i){
ave<-apply(subset(X,cor_new==i),2,mean)
ave
}
#the following is the update rule. It gets two cor_old and cor_new
#and update cor_old, and update cor_new with the minimum distances.
while(any(cor_new!=cor_old)) {
cor_old <- cor_new
cor_new <- apply(rdist(X,Ctr),1,which.min)
Ctr <- t(sapply(1:K,findmean))
}
return(list(cluster=cor_new, centers=Ctr))
}
X <- as.matrix(read.csv("cluster.csv",header=F))
dimnames(X)[[2]]<-NULL
nstart <- 200
K <- 5
Allcenters <- c()
for(i in 1:nstart) {
cs <- Kmean(X,K)
Allcenters <- rbind(Allcenters,cs$centers)
}
plot(X,col=8,pch=20,xlab="x_cor",ylab="y_cor",main="Kmean clustering")
points(Allcenters)