-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_kmeans_library.R
52 lines (46 loc) · 1.15 KB
/
my_kmeans_library.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
choosecluster <- function(x,centroids)
{
d=c() #list of dist of points wrt to clusters'centroids
#cat("centroids passed",View(centroids))
for( i in 1:nrow(x))
{
dist=c()
for(j in 1:nrow(centroids))
{
dist<-c(dist,calcdist(x[i,],centroids[j,]))
#cat("dist",dist,"\n")
#cat("cluster no",j)
}
x[i,5]=which.min(dist)
d<-c(d,(min(dist))^2) #SSE : sum of square errors
}
#cat("end choose cluster ")
return(list(x,sum(d)))
}
calcdist <- function(x,y)
{
#cat("x[1:8]=",View(x),"y[1:8]=",View(y[1:8]))
#cat("x[1:8]-y[1:8]=")
#x[1:8]-y[1:8]
#cat("\nx[1:8]-y[1:8]^2=")
#(x[1:8]-y[1:8])^2
#cat("entered calcdist",sqrt(rowSums((x[1:8]-y[1:8])^2)),"\n")
return(sqrt(rowSums((x[1:4]-y[1:4])^2)))
}
calcnewcentroids<- function(x,K)
{
#cat("entered calcnewcentroids")
newcentroids=matrix(nrow=K,ncol=5)
for(i in 1:K)
{
temp=x[x[,5]==i,]
A=c()
for( j in 1:ncol(temp))
{
A<-c(A,mean(temp[,j]))
}
newcentroids[i,]=A
}
#cat("end newcentroid")
return(newcentroids)
}