-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzzy_rough.py
More file actions
268 lines (259 loc) · 7.62 KB
/
fuzzy_rough.py
File metadata and controls
268 lines (259 loc) · 7.62 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import numpy as np
import csv
import operator
class cluster:
w_low = 0.7
w_up = 0.3
def __init__(self):
self.lower=[]
self.upper=[]
self.boundary=[]
self.lower_extra=[]
self.boundary_extra=[]
self.sum_low_membership=0
self.sum_up_membership=0
self.upper_membership=[]
def add_lower(self,val,row=None):
self.lower.append(row)
row =[i * val for i in row]
self.lower_extra.append(row)
self.sum_low_membership=self.sum_low_membership+val
def add_boundary(self,val, row=None):
self.boundary.append(row)
self.upper_membership.append(val)
row =[i * val for i in row]
self.boundary_extra.append(row)
self.sum_up_membership=self.sum_up_membership+val
def show(self):
print "lower bound"
print len(self.lower)
print "boundary bound "
print len(self.boundary)
def find_new_cluster(self):
# self.boundary = [row for row in self.upper if row not in self.lower]
sum1 = map(sum, zip(*self.lower_extra))
term1=[]
len1 = len(self.lower)
len2 = len(self.boundary)
print "length of lower",len1
print "length of boundary",len2
if len1 == 0:
kaka=raw_input()
if len2 > 0:
term2=[]
sum2 = map(sum, zip(*self.boundary_extra))
term1 = calculation(cluster.w_low,self.sum_low_membership,sum1)
term2 = calculation(cluster.w_up,self.sum_up_membership,sum2)
term1=np.array(term1)
term2=np.array(term2)
sum_total = map(sum,zip(term1,term2))
return sum_total
else:
new_w_low = 1
term1 = calculation(new_w_low,self.sum_low_membership,sum1)
return term1
def delete(self):
del self.lower[:]
del self.upper[:]
del self.boundary[:]
del self.boundary_extra[:]
del self.lower_extra[:]
del self.upper_membership[:]
self.sum_low_membership=0
self.sum_up_membership=0
def add_up_low(self,x=None):
self.lower.append(x)
self.upper.append(x)
def calculate_S(self,D=None):
len1=len(self.lower)
len2=len(self.boundary)
g=[]
for l in self.lower:
distances =(l - D)**2
distances=distances.sum(axis=-1)
g.append(distances)
g=np.array(g)
summ1=np.sum(g)
if len1 > 0 and len2 > 0:
term1 = float(cluster.w_low*summ1)/float(len1)
g1=[]
for l in self.boundary:
distances =(l - D)**2
for ab in self.upper_membership:
distances =[pop * ab for pop in distances]
distances=distances.sum(axis=-1)
g1.append(distances)
g1=np.array(g1)
summ2=np.sum(g1)
term2 =float(cluster.w_up * summ2)/float(self.sum_up_membership)
self.s=np.add(term1,term2)
else:
term1 = float(summ1)/float(len1)
self.s=term1
def calculation(weight,leng1,summ = None):
final_cal = []
for val in summ:
final_cal.append((weight*val)/leng1)
return final_cal
def checkforequal(a=None,b=None):
a=np.array(a)
b=np.array(b)
if (a==b).all():
return 1
else:
return 0
def change_precision(k,a=None):
m=[]
for row in a:
y=[]
for val in row:
val=float(val)
val=round(val,5)
y.append(val)
m.append(y)
return m
def distancee(A=None,B=None):
distances =(A-B)**2
distances=distances.sum(axis=-1)
distances=np.sqrt(distances)
return distances
def summ_cluster_data_distances(**dist):
return sum(dist.values())
if __name__ =='__main__':
threshold = 0.01
count=1
reader=np.array(list(csv.reader(open("soya.txt","r"),delimiter=',')))
X=reader[:,:35]
X=np.array(X).astype('float')
rows=len(X)
cols=len(X[0])
print "Enter the number of clusters you want"
k = input()
V=np.zeros((k,cols))
V=np.array(X).astype('float')
V=X[:k,:]
c=[]
for i in xrange(0,k):
c.append(cluster())
first=10
second=10+k
print len(X)
for i in xrange(0,k):
new_x=X[first:second,:]
for x in new_x:
x=x.tolist()
c[i].add_lower(1,x)
first=second+1
second=second+k+1
for i in xrange(0,k):
c[i].show()
#calculating the distance between the cluster centers and the data points
dist={}
U={}
flag = 1
while flag == 1:
if count > 1:
for xx in xrange(0,k):
c[xx].delete()
print "count is"
print count
for i in xrange(0,rows):
dist.clear() #you can use dist = {} also
U.clear()
deno=0
for j in xrange(0,k):
distances =distancee(X[i,:],V[j,:])
dist[j]=distances**2
# print dist
# sum_val=summ_cluster_data_distances(dist)
# print "distances are",dist
mini_key=min(dist.iteritems(), key=operator.itemgetter(1))[0]
if dist[mini_key] == 0:
c[int(mini_key)].add_lower(1,X[i,:].tolist())
else:
for pp in xrange(0,k):
for ij in xrange(0,k):
deno = deno+((dist[pp]/dist[ij])**2)
U[pp]=1/deno
deno=0
# print "U is",U
# print "sum of U is ",sum(U.values())
#kka=raw_input()
first_max_key=max(U.iteritems(), key=operator.itemgetter(1))[0]
first_max_value=U[first_max_key]
del U[first_max_key] # can also use U.pop('key')
second_max_key=max(U.iteritems(), key=operator.itemgetter(1))[0]
second_max_value=U[second_max_key]
diff=(first_max_value-second_max_value)
# print "first_max_value :",first_max_value
# print "second_max_value :",second_max_value
print "diff",diff
if diff < threshold :
c[int(first_max_key)].add_boundary(first_max_value,X[i,:].tolist())
c[int(second_max_key)].add_boundary(second_max_value,X[i,:].tolist())
else:
c[int(first_max_key)].add_lower(1,X[i,:].tolist())
V_new = []
del V_new[:] #for future, delete all elements
for i2 in xrange(0,k):
V_new.append(c[i2].find_new_cluster())
#checking cluster distance
V_new=np.array(V_new)
for i3 in xrange(0,k):
for j in xrange(0,k):
if i3 == j:
continue
print "distance between %d cluster and %d cluster is" %(i3+1,j+1)
print distancee(V_new[i3,:],V_new[j,:])
V_new=V_new.tolist()
# end
V=V.tolist()
V_new=change_precision(k,V_new)
print "comparing clusters result"
print checkforequal(V,V_new)
if checkforequal(V,V_new):
flag = 0
else:
flag = 1
if flag == 1:
V = V_new
count=count+1
V=np.array(V)
print "final clusters are :"
print V
print "Final answer is \n"
for i in xrange(0,k):
print "The lower bound and upper bound of %s cluster is " %(i+1)
c[i].show()
#calculate DB index
for i in xrange(0,k):
c[i].calculate_S(V[i,:])
p=[]
total_sum=[]
for i in xrange(0,k):
del p[:]
for j in xrange(0,k):
if j == i:
continue
p.append(float(np.add(c[j].s,c[i].s))/float(distancee(V[j,:],V[i,:])))
total_sum.append(max(p))
total_sum=np.array(total_sum)
DB=float(np.sum(total_sum))/float(k)
print "DB index is" , DB
#finish DB index
total_sum=total_sum.tolist()
del total_sum[:]
#calculate D index
S_list=[]
for i in xrange(0,k):
S_list.append(float(c[i].s))
max_s=float(max(S_list))
for i in xrange(0,k):
del p[:]
for j in xrange(0,k):
if j ==i:
continue
p.append(float(distancee(V[j,:],V[i,:]))/max_s)
total_sum.append(min(p))
D_index=min(total_sum)
print "DUNN or D index is ", D_index