-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans_ringnorm.r
173 lines (129 loc) · 3.55 KB
/
kmeans_ringnorm.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#add_data
MyData_raw <- read.csv(file="ringnorm_data.csv",header=FALSE, sep=",")
class(MyData_raw)
#setwd("D:/R-progrms")
b <-dim(MyData_raw)
#b
?subset
new_mydata<-subset(MyData_raw[1])
#q=b[2]-1
#q
MyData<-subset(MyData_raw[2:b[2]])
#MyData
avg_error<-matrix(0,nrow=5,ncol=1)
for(w in 2:5)
{
#set_data
k=w
stop_crit=0.001
#initialize_cetroids
set.seed(3)
a <-dim(MyData)
clust1 <- rnorm(k*a[2])
centroid_mat <-matrix(clust1,nrow =k, ncol =a[2])
#initialize label matrix
label_matrix<-matrix('n',nrow=a[1],ncol=1)
#initialize distance matrix
dist_matrix <- matrix(-1,nrow=a[1],ncol=k)
#initialize centroid_mat 2
centroid_mat2 <-matrix(0,nrow=k,ncol = a[2])
#initialize diff_matrix
diff_matrix <-matrix(-10,nrow=k,ncol = 1)
#initialize gob
gob<-matrix('n',nrow =k,ncol=2)
#initialize error_dat
error_dat<-matrix(0,nrow = k,ncol=1)
#initialize avg_error
#avg_error<-matrix(0,nrow=1,ncol=1)
for(o in 1:20)
{
#fill distance matrix
for(i in 1:a[1])# no.of obs
{
for(j in 1:k)#no.cluster
{
#print(dist(rbind(MyData[i:i,],centroid_mat[j:j,])))
dist_matrix[i:i,j:j]<-dist(rbind(MyData[i:i,],centroid_mat[j:j,]))
}
}
#labeling the clusters
for(s in 1:a[1])
{
for(t in 1:k)
{
if(dist_matrix[s:s,t:t]==min(dist_matrix[s:s,]))
{
label_matrix[s:s,1]=t
}
}
}
#copy to a new matrix
for(x in 1:k)
{
for(y in 1:a[2])
{
centroid_mat2[x:x,y:y]=centroid_mat[x:x,y:y]
}
}
#re-calculating new clusters
for(v in 1:k) # no. of clusters
{
count=1
for(u in 1:a[1])#no. of observations
{
if(label_matrix[u:u,1]==v) #check label value
{
centroid_mat[v:v,]=colSums(rbind(MyData[u:u,],centroid_mat[v:v,]),na.rm = TRUE)
count=count+1
}
}
centroid_mat[v:v,]=centroid_mat[v:v,]/count
}
#calculating dif
for(e in 1:k)
{
diff_matrix[e:e]=dist(rbind(centroid_mat2[e:e,],centroid_mat[e:e,]))
}
#stopping criteria
stop_criteria=sum(diff_matrix)/dim(diff_matrix)[1]
print(stop_criteria)
if(stop_criteria<stop_crit)
{
print("Reached stopping criteria.Exiting Now..")
break;
}
}
#finding out good and bad for each frequency
dat<-cbind(label_matrix,new_mydata)
gob<-as.data.frame(table(dat))
c<-dim(gob)
good<-subset(gob[c[2]],gob[,2]=='1')
names(good)[1]<-"good_freq"
bad<-subset(gob[c[2]],gob[,2]=='-1')
names(bad)[1]<-"bad_freq"
gob_new<-as.data.frame(rbind(cbind(bad,good)))
newlab<-matrix('n',nrow =k,ncol=1)
#error_dat<-matrix(0,nrow = k,ncol=1)
print("check")
for(l in 1:k)
{
if(gob_new[l:l,1]>gob_new[l:l,2])
{
newlab[l:l,1]='g'
error_dat[l:l,1]<-gob_new[l:l,1]/sum(cbind(gob_new[l:l,1],gob_new[l:l,2]))
}
else
{
newlab[l:l,1]='b'
error_dat[l:l,1]<-gob_new[l:l,1]/sum(cbind(gob_new[l:l,1],gob_new[l:l,2]))
}
}
d<-dim(error_dat)
p=0
for(p in 1:d[1])
{
avg_error[k:k,1]<-colSums(rbind(error_dat[p:p,],avg_error[k:k,1]),na.rm= TRUE)
}
print("avg error")
}
plot(avg_error[,1],pch=20,xlab ="K" ,ylab = "Total Error",type='b',cex=2,main="K-Means on Ringnorm dataset")