-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKmeans.R
More file actions
132 lines (110 loc) · 3.5 KB
/
Kmeans.R
File metadata and controls
132 lines (110 loc) · 3.5 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
################################################################
#
# K means clustering algorithm
#
################################################################
find.d <- function(p,center)
{
t = matrix( rep(center,nrow(p)), byrow = TRUE,ncol = ncol(p) )
return( rowSums((p-t)^2) )
}
km.cl <-function(p,n = 6)
{ # start km.l
start = sample(1:nrow(p),n, replace = FALSE)
cntr = p[start,]
old.cntr = 0.9*cntr
while ( any(cntr != old.cntr) )
{ # update center points
# allocate matrix contining squared distance
# of p points from cntr points.
# column i has sq. dist. from p to ith cntr point
old.cntr = cntr
d = matrix( rep(0,nrow(p)*n), ncol = n)
for ( i in 1:n )
{
d[,i] = find.d(p,cntr[i,])
}
closest = apply(d,1,which.min)
if ( length(unique(closest)) < n )
{
cat('Note: we have a center containing no points.\n')
}
for ( j in unique(closest) )
{
cntr[j,] = colMeans(p[which(closest == j),])
}
} # update center points
for ( i in 1:n )
{
d[,i] = find.d(p,cntr[i,])
}
closest = apply(d,1,which.min)
return( list("cluster.num" = closest, "centers" = cntr) )
} # end km.cl
#####################################################################
#
# K Means Clustering Algorithm
#
# Plots Progress of Algorithm
#
####################################################################
km.cl.steps <-function(p,n = 6)
{ # start km.l
start = sample(1:nrow(p),n, replace = FALSE)
cntr = p[start,]
old.cntr = 0.9*cntr
############################# display code ###############
dev.new()
plot(p, type = 'n')
points(cntr, cex = 2)
##########################################################
while ( any(cntr != old.cntr) )
{ # update center points
# allocate matrix contining squared distance
# of p points from cntr points.
# column i has sq. dist. from p to ith cntr point
old.cntr = cntr
d = matrix( rep(0,nrow(p)*n), ncol = n)
for ( i in 1:n )
{
d[,i] = find.d(p,cntr[i,])
}
closest = apply(d,1,which.min)
####################### display code #####################
for ( k in 1:n )
{
points(p[which(closest == k),], col = 11*k, pch = k)
}
Sys.sleep(3)
###########################################################
if ( length(unique(closest)) < n )
{
cat('Note: we have a center containing no points.\n')
}
for ( j in unique(closest) )
{
cntr[j,] = colMeans(p[which(closest == j),])
}
######################## display code ########################
dev.new()
plot(p, type = 'n')
points(cntr,cex = 2)
##############################################################
} # update center points
for ( i in 1:n )
{
d[,i] = find.d(p,cntr[i,])
}
closest = apply(d,1,which.min)
#################### display code ####################
dev.new()
plot(p,type = 'n', main = 'Final Result')
for ( i in 1:n )
{
points(p[which(closest == i),], col = 11*i, pch = i)
}
points(cntr, cex = 2)
#####################################################
return( list("cluster.num" = closest, "centers" = cntr) )
} # end km.cl
###