-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmeans.py
280 lines (240 loc) · 7.92 KB
/
kmeans.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
import csv
import editdistance
from random import randint
import numpy as np
import matplotlib.pyplot as plt
in_file = open("C:/Users/acedon/Desktop/hashtagnew1.csv","r")
e = ""
term = 1
maxIter = 500
def DataForm(in_file):
reader = csv.reader(in_file,delimiter=";")
formatData = []
id = []
hashtags = []
for row in reader:
id.append(row[0])
in_file.close()
in_file = open("C:/Users/acedon/Desktop/hashtagnew1.csv","r")
reader = csv.reader(in_file,delimiter=";")
for row in reader:
hashtags.append(row[1])
in_file.close()
idsneu = []
hashtagsneu = []
i = 0
for i in range(0,len(hashtags),1): #Aufspallten der zusammen auftretenden hashtags bei gleichbleibender id
temp = hashtags[i].partition(",")
while (temp[2] != ""):
idsneu.append(id[i])
hashtagsneu.append(temp[0])
temp = temp[2].partition(",")
if (temp[2]== ""):
idsneu.append(id[i])
hashtagsneu.append(temp[0])
for i in range(0,len(idsneu),1):
formatData.append((idsneu[i],hashtagsneu[i]))
return formatData
def filter(list, value):
for x,y in list:
if x == value:
return x,y
def Init(formatData): # INIT FUNKTION, initialisiert die ersten center und cluster
c1 = formatData[(randint(0,len(formatData)))]
c2 = formatData[(randint(0,len(formatData)))]
c3 = formatData[(randint(0,len(formatData)))]
c4 = formatData[(randint(0,len(formatData)))]
#SafetyCheck damit nicht 2 Center gleich sind
while( (c1[1] == c2[1]) | (c1[1] == c3[1]) | (c1[1] == c4[1]) | (c2[1] == c3[1]) | (c2[1] == c4[1]) | (c3[1] == c4[1]) ):
if(c1[1] == c2[1]):
c2 = formatData[(randint(0,len(formatData)))]
if(c1[1] == c3[1]):
c3 = formatData[(randint(0,len(formatData)))]
if(c1[1] == c4[1]):
c4 = formatData[(randint(0,len(formatData)))]
if(c2[1] == c3[1]):
c3 = formatData[(randint(0,len(formatData)))]
if(c2[1] == c4[1]):
c4 = formatData[(randint(0,len(formatData)))]
if(c3[1] == c4[1]):
c4 = formatData[(randint(0,len(formatData)))]
cluster1 = []
cluster2 = []
cluster3 = []
cluster4 = []
obercluster = []
center = []
t1 = ()
for i in range(0,len(formatData),1):
ecluster = []
t1 = formatData[i]
ecluster.append(editdistance.eval(t1[1],c1[1]))
ecluster.append(editdistance.eval(t1[1],c2[1]))
ecluster.append(editdistance.eval(t1[1],c3[1]))
ecluster.append(editdistance.eval(t1[1],c4[1]))
m = min(ecluster)
tempind = ecluster.index(m)
if tempind == 0:
cluster1.append(formatData[i])
if tempind == 1:
cluster2.append(formatData[i])
if tempind == 2:
cluster3.append(formatData[i])
if tempind == 3:
cluster4.append(formatData[i])
obercluster.append(cluster1)
obercluster.append(cluster2)
obercluster.append(cluster3)
obercluster.append(cluster4)
center.append(c1)
center.append(c2)
center.append(c3)
center.append(c4)
return (obercluster, center, formatData)
def NextCenter(obercluster, center, formatData):
iter = 0
newCenters = []
oldCenter = []
t = ()
for i in range(0,len(center),1):
mitte = 0
tempEdit = []
oldCenter.append(center[i])
cluster = obercluster[i]
if((len(center)) == 1):
cluster = obercluster
found = False
#Mittelwert des aktuellen clusters bestimmen
for i in range(0,len(cluster),1):
elem = cluster[i]
tempEdit.append(editdistance.eval(elem[1],e))
for i in range(0,len(tempEdit),1):
mitte = mitte + tempEdit[i]
mitte = mitte//(len(cluster))
#Gibt es Hashtag mit mittelwert?? falls Ja ist es das neue center
for i in range(0,len(formatData),1):
elem = formatData[i]
if((editdistance.eval(elem[1],e)) == mitte):
newCenters.append(elem)
found = True
break
#Falls Nicht suchen wir nach dem nächst größerem unde dem nächst kleineren und nehmen das bessere von beiden
#Nächst kleinere
if(found == False):
j = mitte
smallerIter = 0
smallerC = ()
while(j >= 0):
j = j-1
for i in range(0,len(formatData),1): # Falls fehler formatdata zu cluster ändern
elem = formatData[i]
if( ( editdistance.eval(elem,e) ) == j ):
smallerC = elem
break
smallerIter = smallerIter -1
if(smallerC != t):
break
#nächst größeres !!!!!!! BIGGERC ERHÄLT NiE EINEN WERT KP WARUM
if(found == False):
j = mitte
tempMaxIter = ( max(tempEdit))
biggerIter = 0
biggerC = ()
while(j <= tempMaxIter ):
j = j + 1
for i in range(0,len(formatData),1): # Falls fehler formatdata zu cluster ändern
elem = formatData[i]
if ( (editdistance.eval(elem,e) == j )):
biggerC = elem
break
biggerIter = biggerIter - 1
if(biggerC != t):
break
#checken welches besser ist
if( (found == False)):
if( (abs(smallerIter) >= biggerIter) & (biggerC != t) & ( (len(newCenters))<4 ) ):
newCenters.append(biggerC)
newCenters.append(smallerC)
else:
newCenters.append(smallerC)
return (newCenters,oldCenter)
def ValidateCenter(newCenters,oldCenter):
# Validieren ob die neu gefundenen Center sich in ihrer editdistanze zum leeren wort weniger als der Terminationswert von der Editdistanze des alten Centers zum leeren unterscheidet
#falls ja wird das neue center als final gesetzt und wird in zuküntigen iterationen nicht mehr neu berechnet
i = 0
workingIndex = [False,False,False,False]
for i in range(0,len(newCenters),1):
elem1 = newCenters[i]
elem2 = oldCenter[i]
test = []
test.append(editdistance.eval(elem1[1],e) )
test.append(editdistance.eval(elem2[1],e) )
print("TEST",test)
if( max(test)- min(test) > term):
workingIndex[i] = False
else:
workingIndex[i] = True
print(workingIndex)
return workingIndex
def recompute(newCenters,obercluster,formatData): #neuberechnen der einzelen Center deren Editdistanzevergleich zum alten Center über dem Terminationswert lag
elem = NextCenter(obercluster,[newCenters],formatData)
reCenter = elem[0]
return reCenter
# Cluster bilden
def clustering(center,formatData):
obercluster = [[],[],[],[]]
for j in range(0,len(formatData),1):
ecluster = []
for i in range(0,len(center),1):
ecluster.append(editdistance.eval(formatData[j],center[i]))
m = min(ecluster)
tempind = ecluster.index(m)
obercluster[tempind].append(formatData[j])
oberlen= len(obercluster)
for i in range(0,oberlen,1):
if(obercluster[i] == []): #!!!!!!!!!!!!!!!!!!!!!PROBLEMpUNKT!!!!!!!!!!!
del(obercluster[i])
return obercluster
# Das plotten der Hashtags als Scatterplott
def plotten(center,obercluster):
e2 = ""
y = []
x = []
y2 = []
x2=[]
print("PLOTT CLUSTER", obercluster)
for i in range(0,len(obercluster),1):
temp = obercluster[i]
for j in range(0,len(obercluster[i]),1):
x.append(temp[j][0])
y.append(editdistance.eval(temp[j][1],e2))
for i in range(0,len(center),1):
x2.append(center[i][0])
y2.append(editdistance.eval(center[i][1],e2))
plt.scatter(x,y,label = "Editdistance der Hashtags zum leeren wort")
plt.scatter(x2,y2, marker = "x", s=150)
plt.xlabel("Hashtag-Id")
plt.ylabel("Editdistance")
plt.legend()
plt.show()
return 0
def kmeans(in_file):
temp = Init(DataForm(in_file))
formatData = temp[2]
obercluster = temp[0]
firstCenters = temp[1]
workingIndex = [False,False,False,False]
temp2 = NextCenter(obercluster,firstCenters,formatData)
newCenters = temp2[0]
oldCenter = temp2[1]
while(workingIndex != [True,True,True,True]):
obercluster = clustering(newCenters, formatData)
for i in range(0,len(workingIndex),1):
if(workingIndex[i] == False):
workingIndex = (ValidateCenter(newCenters,oldCenter))
oldCenter[i] = newCenters[i]
zwischen = recompute(newCenters[i],obercluster[i],formatData) #!!!!!!!!!!!!PROBLEPUNKT!!!!!!!!!!
newCenters[i] = zwischen[0]
plotten(newCenters,obercluster)
return (obercluster,newCenters)
test = kmeans(in_file)