-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuclidean Distance.py
365 lines (316 loc) · 10.1 KB
/
Euclidean Distance.py
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import string
import math
def EuclideanDistance (centroid,TFIDF):
temp=0;temp2=[];jarak=[]
for i in range(0,len(centroid)):
for j in range(0,len(TFIDF)):
for k in range(0,len(TFIDF[0])):
temp = temp + math.pow(centroid[i][k]-TFIDF[j][k],2)
temp2.append(math.sqrt(temp))
temp=0
jarak.append(temp2)
temp2=[]
euclid=[]
temp = []
for i in range(0,len(jarak[0])):
for j in range(0,len(jarak)):
temp.append(jarak[j][i])
euclid.append(temp)
temp=[]
return euclid
def CosineSimilarity (centroid,TFIDF):
temp4=[];Cosine=[]
for i in range(0,len(TFIDF)):
for j in range (0,len(centroid)):
temp1=sum([TFIDF[i][k]*centroid[j][k] for k in range(0,len(TFIDF[0]))])
temp2=math.sqrt(sum([math.pow(TFIDF[i][k],2) for k in range(0,len(TFIDF[0]))]))
temp3=math.sqrt(sum([math.pow(centroid[j][k],2) for k in range(0,len(TFIDF[0]))]))
temp4.append(temp1/(temp2*temp3))
Cosine.append(temp4)
temp4=[]
return Cosine
def getIndeks(X,Similar,TFIDF):
indeks=[]
for i in range(0,len(TFIDF)):
if X == 0:
indeks.append(Similar[i].index(min(Similar[i])))
else:
indeks.append(Similar[i].index(max(Similar[i])))
return indeks
def readDocument(a,b):
doc=[]
for i in range(a,b):
wordList = [str(i+1),".txt"]
sentence = ""
for i in wordList:
sentence += i
temp2=open(sentence, "r").read()
doc.append(temp2)
return doc
def readDocumentm(a,b):
doc=[]
for i in range(a,b):
wordList = [str(i+1),".txt"]
sentence = "m"
for i in wordList:
sentence += i
temp2=open(sentence, "r").read()
doc.append(temp2)
return doc
def PreProcessing(corpus):
doc=[];token=[];
stemmer=PorterStemmer()
stop_words=stopwords.words('english')+list(string.punctuation)
count=0
for i in corpus:
count = count+1
temp=word_tokenize(i)#Tokenizing
for j in temp:
if j not in stop_words: #Hilangkan Stop Words
token.append(stemmer.stem(j)) #Stemming&CaseFolding
doc.append(token)
token=[]
return doc
def getDictionary(doc):
dictionary=[]
for i in doc:
for j in i:
if not j in dictionary: #Hilangkan Duplikasi
dictionary.append(j)
return dictionary
def getNewCentroid(indeks,TFIDF,K):
jmlclus=[]; temp2=[]
for i in range(0,K):
jmlclus.append(indeks.count(i))
idx=[]
for i in range(0,len(indeks)):
idx.append(indeks[i])
temp=[0]*len(TFIDF[0])
centroid=[]
for i in range(0,K):
for j in range(0,jmlclus[i]):
temp=[x + y for x, y in zip(temp,TFIDF[idx.index(i)][:])]
idx[idx.index(i)]=-1
print(' Finding Centroid',i+1,end='')
if jmlclus[i]!=0:
temp2=[z/jmlclus[i] for z in temp]
centroid.append(temp2)
temp=[0]*len(TFIDF[0])
print('...Done!')
return centroid
def printCluster(indeks,K):
print('')
for i in range(0,K):
print('CLUSTER',i+1,':',end='')
for j in range(0,len(indeks)):
if indeks[j] == i:
print(' Doc',j+1,end='')
print('')
print('')
def printme(A,B):
if B == 1:
print(' [',A[0],A[1],A[2],'...',A[len(A)-1],']')
print('')
else:
print(' [',A[0][0],A[0][1],A[0][2],'...',A[0][len(A[0])-1],']')
for i in range(0,3): print(' .')
print(' [',A[0][B-1],A[1][B-1],A[2][B-1],'...',A[B-1][len(A[0])-1],']\n')
rawcont = readDocument(0,500)
rawmeta = readDocumentm(0,500)
print('==============')
print('Term Weightned')
print('==============')
print('1.Pre-Processing',end='')
content = PreProcessing(rawcont)
auxiliary = PreProcessing(rawmeta)
print('...Done')
print('2.Finding Dictionary')
dictcont = getDictionary(content)
print(' Content Dictionary = Vector 1 x',len(dictcont))
printme(A=dictcont,B=1)
dictaux = getDictionary(auxiliary)
print(' Auxiliary Dictionary = Vector 1 x',len(dictaux))
printme(A=dictaux,B=1)
print('3.Finding Term Frequency (TF)',end='')
TF=[];temp=[];count=0
for i in content:
for j in dictcont:
for k in i:
if j==k:
count=count+1
temp.append(count)
count=0
TF.append(temp)
temp=[]
print('...Done')
print(' TF = Matrix',len(TF),'x',len(TF[0]))
printme(A=TF,B=len(TF))
print('4.Finding Document Fequency(DF)',end='')
Freq=[];count=0
for i in range(0,len(TF[0])):
for j in range(0,len(TF)):
if TF[j][i]>0:
count=count+1
Freq.append(count)
count=0
print('...Done')
print(' DF = Vector 1 x',len(Freq))
printme(A=Freq,B=1)
print('5.Finding Inverse Document Frequency (IDF)',end='')
IDF=[]
for i in Freq:
x = math.log10(len(content)/i)
IDF.append(x)
print('...Done')
print(' IDF = Vector 1 x',len(IDF))
printme(A=IDF,B=1)
print('6.Finding Weight(TF*IDF)',end='')
TFIDF=[];temp=[]
for i in range(0,len(TF)):
for j in range(0,len(TF[i])):
temp.append(TF[i][j]*IDF[j])
TFIDF.append(temp)
temp=[]
print('...Done')
print(' TFIDF = Matrix',len(TFIDF),'x',len(TFIDF[0]))
printme(A=TFIDF,B=len(TFIDF))
hold = input('Input any value to continue...')
print('\n=====================================')
print('Content Based Algorithm Using K-Means')
print('=====================================')
Iterasi=1
print('ITERATION ',Iterasi)
K = 3
print('K =',K)
print('1.Initialize Random Centroid',end='')
centroid=[]
temp = [0,440,25]
for i in range(0,K):
centroid.append(TFIDF[temp[i]-1])
print('...Done')
print('2.Compute Euclidean Distance',end='')
Euclid = EuclideanDistance(centroid,TFIDF)
print('...Done')
print('3.Assigned Document to Closest Centroid',end='')
indeksKmeans1 = getIndeks(0,Euclid,TFIDF)
print('...Done')
ulang=True
Iterasi=Iterasi+1
while(ulang==True):
print('\nITERATION ',Iterasi)
print('1.Finding New Centroid')
centroid1 = getNewCentroid(indeksKmeans1,TFIDF,K)
print('2.Compute Euclidian Distance Document to New Centroid',end='')
Euclid = EuclideanDistance(centroid1,TFIDF)
print('...Done')
print('3.Assigned Document to New Closest Centroid',end='')
print(' Done!')
indeksKmeans2 = getIndeks (0,Euclid,TFIDF)
if indeksKmeans1 == indeksKmeans2:
print('\nAnggota Cluster Tidak Berubah')
print('Proses Berhenti')
printCluster(indeksKmeans2,K)
ulang=False
else:
print('Anggota Cluster Berubah')
print('Repeat the Process from Step 2')
print('Centroid = New Centroid')
print('')
centroid = centroid1
Iterasi = Iterasi+1
dictclust=[]
for i in range(0,K):
temp=[]
for j in range(0,len(indeksKmeans2)):
if indeksKmeans2[j]==i:
for k in content[j]:
if k not in temp:
temp.append(k)
dictclust.append(temp)
print('=====================')
print('First Minor Iteration')
print('=====================')
print('1.Finding Euclidean Distance',end='')
Euclid = EuclideanDistance(centroid1,TFIDF)
print('...Done!')
print('2.Assign Document to Closest Cluster',end='')
indeksFirstIteration = getIndeks(0,Euclid,TFIDF)
print('...Done!')
print('3.Update Cluster Centroid Fisrt Minor Iteration')
centroidFirstIteration = getNewCentroid(indeksFirstIteration,TFIDF,K)
printCluster(indeksFirstIteration,K)
hold = input('Input any value to continue...')
print('======================')
print('Second Minor Iteration')
print('======================')
print('1.Compute Giny Index',end='')
Frj=[];Frm=[];temp=[]
for i in range(0,len(auxiliary)):
for j in range(0,K):
count=0
for k in auxiliary[i]:
if k not in dictclust[j]:
count=count+1
temp.append(count)
count=0
Frj.append(temp)
Frm.append(sum(Frj[i]))
temp=[]
Prj=[];temp=[]
for i in range(0,len(Frj)):
for j in range(0,len(Frj[0])):
temp.append(Frj[i][j]/Frm[i])
Prj.append(temp)
temp=[]
Giny=[];temp=[];temp2=[]
for i in range(0,len(Prj)):
for j in range(0,K):
temp.append(math.pow(Prj[i][j],2))
Giny.append(sum(temp))
temp2.append(temp)
temp=[]
print('...Done!')
print('2.Compute Average of Giny Index',end='')
temp=0
for i in range(0,len(Giny)):
temp=temp+Giny[i]
Avg = temp/len(Giny)
print('...Done!')
print('3.Mark Attribute as Discrimanatory',end='')
Disc=[];idxdisc=[];Ri=[]
for i in range(0,len(Giny)):
if Giny[i]<=Avg:
Disc.append(content[i])
Ri.append(i)
print('...Done!')
print('4.Compute Probability of Discriminatory Attribute',end='')
count=0;temp=[];temp2=[]
for i in range(len(Ri)):
for j in range(0,K):
for k in auxiliary[Ri[i]]:
if k in dictclust[j]:
count=count+1
temp.append(count)
count=0
temp2.append(temp)
temp=[]
print('...Done!')
print('5.Assigned Discriminatory Attribute to Cluster',end='')
idx=[]
for i in range(0,len(indeksFirstIteration)):
idx.append(indeksFirstIteration[i])
indeksSecondIteration=idx
for j in range(0,len(temp2)):
x = max(temp2[j]) #identify if discriminatory attribute assigned to other cluster
idxx = temp2[j].index(x)
indeksSecondIteration[Ri[j]]=idxx
print('...Done!')
print('6.Update Cluster Centroid')
centroidSecondIteration = getNewCentroid(indeksSecondIteration,TFIDF,K)
printCluster(indeksSecondIteration,K)
for i in range(0,len(content)):
if indeksFirstIteration[i] != indeksSecondIteration[i]:
print('Doc',i+1,'Move from Cluster',indeksFirstIteration[i]+1,'to Cluster',indeksSecondIteration[i]+1)