-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering.py
490 lines (393 loc) · 14.8 KB
/
clustering.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import random
import matplotlib.pyplot as plt
from cluster import *
# The following variables are for different k_means algorithms
K_MEANS_PLUS_PLUS = 100
LLOYD = 200
class Clustering(object):
""" The clustering method class. Different clustering methods are implemented.
"""
def __init__(self,points):
""" Init Clustering instance.
Args:
points (list): a list of Point instances.
"""
self.__points = [p for p in points]
if len(points) == 0:
print "Should not initialize clustering without points!"
def hierarchical_cluster(self,k,linkage = DISTANCE_MEAN_CENTER):
""" Return the hierarchical clusters.
Note:
The algorithm is O(n^3) while the best one is O(n^2logn).
I will re-write this soon.
Args:
k (int): the number of clusters you want to have.
linkage (int): how to define the distance between clusters.
See more information in Cluster.py
Returns:
clusters (list): list of Cluster instances.
"""
points = self.__points[:]
if k < 1 or k > len(points):
return None
clusters = []
for p in points:
clusters.append(Cluster([p],linkage=linkage))
while len(clusters) > k:
index1 = -1
index2 = -1
distance = -1
for i in xrange(len(clusters)-1):
for j in xrange(i+1,len(clusters)):
d = clusters[i].distance(clusters[j])
if distance == -1:
distance = d
index1 = i
index2 = j
elif d < distance:
distance = d
index1 = i
index2 = j
clusters[index1].union(clusters.pop(index2))
return clusters
def k_center(self,k,init_centers=[]):
""" Return clusters.
Node:
This function uses the Gonzalez Algorithm.
Args:
k (int): the number of clusters wanted.
init_centers (list,optional): the initial centers.
Returns:
clusters (list): list of the clusters
cost (float): the cost of the entire clustering. In this case, it's the maximum distance of
any (point, center) pair.
"""
points = self.__points[:]
centers = init_centers[:]
if len(centers) == 0:
centers.append(points.pop(0))
else:
for p in init_centers:
if p in points:
points.remove(p)
cost = -1
""" TODO
The algorithm works but it is not efficient because distance between p and c does NOT
need to be calculated every time!
Update the distance when a new c is added to the centers
"""
# Find k centers
while len(centers) < k:
max_distance = -1
index = -1
for i in xrange(len(points)):
min_distance = -1
p = points[i]
for c in centers:
d = p.distance(c)
cost = d if d > cost else cost
if min_distance == -1:
min_distance = d
elif d < min_distance:
min_distance = d
if min_distance > max_distance:
max_distance = min_distance
index = i
centers.append(points.pop(index))
# Construct the clusters.
clusters = []
for c in centers:
new_cluster = Cluster([c])
new_cluster.set_center(c)
clusters.append(new_cluster)
""" TODO
I don't think this is efficient. Is there another way?
"""
cost = -1
while(len(points) != 0):
p = points.pop(0)
distance = -1
for i in xrange(len(clusters)):
c = clusters[i].center
d = p.distance(c)
if distance == -1:
distance = d
index = i
elif d < distance:
distance = d
index = i
cost = distance if distance > cost else cost
clusters[index].add_points([p])
return clusters,cost
def k_means(self,k,method=K_MEANS_PLUS_PLUS,init_centers = []):
""" Return clusters.
Args:
k (int): the number of clusters as a result.
method (int): the algorithm used to cluster.
K_MEANS_PLUS_PLUS: the k-means++ algorithm
LLOYD: the Lloyd algorithm
init_centers (list): list of initial center.
Note:
The length of init_centers CAN'T exceed k.
Returns:
clusters (list): list of the clusters
cost (float): the cost of the entire clustering. In this case, it's the sum of the square distance
of any (point, center) pair.
"""
points = self.__points[:]
centers = init_centers[:]
for p in init_centers:
if p in points:
points.remove(p)
if method == K_MEANS_PLUS_PLUS:
if len(centers) == 0:
centers.append(points.pop(0))
while len(centers) < k:
weights = {}
for i in xrange(len(points)):
p = points[i]
distance = -1
for c in centers:
d = p.distance(c)**2
if distance == -1:
distance = d
elif d < distance:
distance = d
weights[i] = distance
s = sum(weights.values())
for key in weights:
weights[key] /= s
chozen = False
chozen_point = 0
while not chozen:
for key in weights:
if weights[key] > random.random():
chozen = True
chozen_point = key
break
centers.append(points.pop(chozen_point))
# Construct the clusters
clusters = []
for c in centers:
new_cluster = Cluster([c])
new_cluster.set_center(c)
clusters.append(new_cluster)
cost = 0
while(len(points) != 0):
p = points.pop(0)
distance = -1
index = -1
for i in xrange(len(centers)):
c = centers[i]
d = p.distance(c)**2
if distance == -1:
distance = d
index = i
elif d < distance:
distance = d
index = i
cost += distance
clusters[index].add_points([p])
return clusters,cost
elif method == LLOYD:
"""TODO
Set some repetion rounds. currently, it stops when it converges
"""
# Init the centers
for i in xrange(k-len(centers)):
centers.append(points[random.randint(0,len(points)-1)])
subsets = {}
for c in centers:
subsets[c] = []
centers_to_add = centers
cost = 0
while len(centers_to_add) > 0:
cost = 0
centers_to_add = []
centers_to_delete = []
for c in subsets:
subsets[c] = []
# Find the sets for each center
for p in points:
center = None
distance = -1
for c in subsets:
d = p.distance(c)**2
if distance == -1:
distance = d
center = c
elif d < distance:
distance = d
center = c
cost += distance
subsets[center].append(p)
# Check if the center needs to be updated
for c in subsets:
subset = subsets[c]
if len(subset) > 0:
m = [0 for i in subset[0].coordinates]
for p in subset:
coordinates = p.coordinates
for i in xrange(len(coordinates)):
m[i] += coordinates[i]
m = map(lambda x:x/len(subset),m)
new_center = Point(m)
if not c.equal(new_center):
centers_to_add.append(new_center)
centers_to_delete.append(c)
for c in centers_to_add:
subsets[c] = []
for c in centers_to_delete:
if c.index != -1:
points.append(c)
del subsets[c]
clusters = []
for c,cl in subsets.items():
new_cluster = Cluster(cl)
new_cluster.set_center(c)
clusters.append(new_cluster)
return clusters,cost
def k_median(self,k,init_centers=[]):
""" Return clusters.
Note:
The idea of the algorithm is, to find the point in the subset,
which minimize the sum of distances to other points in the same subset and
use it as the new center. we keep doing this until it converges.
Args:
k (int): the number of clusters wanted.
init_centers (list): list of initial centers.
Note:
The length of init_centers CAN'T exceed k.
Returns:
clusters (list): list of the clusters
cost (float): the cost of the entire clustering. In this case, it is the sum of distance of any (point, center) pair.
"""
points = self.__points[:]
# If don't copy the list, run k_median will have the same result
centers = init_centers[:]
for p in init_centers:
if p in points:
points.remove(p)
for i in xrange(k-len(centers)):
centers.append(points.pop(random.randint(0,len(points)-1)))
subsets = {}
for c in centers:
subsets[c] = []
centers_to_add = centers
cost = 0
while len(centers_to_add) > 0:
cost = 0
centers_to_add = []
centers_to_delete = []
for c in subsets:
subsets[c] = []
# Find the sets for each center
for p in points:
center = None
distance = -1
for c in subsets:
d = p.distance(c)
if distance == -1:
distance = d
center = c
elif d < distance:
distance = d
center = c
cost += distance
subsets[center].append(p)
# Check if the center needs to be updated
for c in subsets:
subset = subsets[c]
subset.append(c)
distance_sum = -1
new_center = None
for p in subset:
d = 0
for q in subset:
d += p.distance(q)
if distance_sum == -1:
distance_sum = d
new_center = p
elif d < distance_sum:
distance_sum = d
new_center = p
if not c.equal(new_center):
centers_to_add.append(new_center)
centers_to_delete.append(c)
for c in centers_to_add:
subsets[c] = []
points.remove(c)
for c in centers_to_delete:
del subsets[c]
points.append(c)
clusters = []
for c,cl in subsets.items():
new_cluster = Cluster(cl)
new_cluster.set_center(c)
clusters.append(new_cluster)
return clusters, cost
def draw_2_dimentional_graph(self,clusters):
""" Return a graph for given clusters.
Note:
It only works for 2 dimentional clusters. Also, when the number of clusters exceeds 8, I can't tell clusters with the same color.
Args:
clusters (list): list of clusters.
"""
colors = ['b','r','g','c','m','y','k','w']
clen = 8
"""TODO
Actually, if the number of clusters is over 8, I can't tell the difference between clusters with the same color.
Is there a way to solve this?
"""
for i in xrange(len(clusters)):
cluster = clusters[i]
x = []
y = []
for p in cluster.points:
coordinates = p.coordinates
x.append(coordinates[0])
y.append(coordinates[1])
style = 'o'+colors[i%clen]
plt.plot(x,y,style)
plt.show()
@property
def points(self):
return self.__points[:]
if __name__ == '__main__':
print "Example"
# Create list of points
f = open("data.txt")
points = []
for line in f.readlines():
l = line.split()
index = int(l[0])
x = float(l[1])
y = float(l[2])
p = Point([x,y],index)
points.append(p)
# init Clustering instance
cl = Clustering(points)
"""Note
hierarchical_cluster on the example data set may be very slow. It works for smaller data set.
"""
#clusters = cl.hierarchical_cluster(2,DISTANCE_MAX)
#clusters = cl.hierarchical_cluster(2,DISTANCE_MIN)
#clusters = cl.hierarchical_cluster(2,DISTANCE_MEAN_CENTER)
#clusters,cost= cl.k_center(3)
#clusters,cost= cl.k_means(3,method=LLOYD)
#clusters,cost= cl.k_means(3,method=K_MEANS_PLUS_PLUS)
clusters,cost= cl.k_means(3,method=K_MEANS_PLUS_PLUS,init_centers=[])
#clusters,cost= cl.k_median(3)
# Report points in a cluster
cluster_index = 0
for c in clusters:
print "cluster %d" % cluster_index
print "the center is ",c.center
for p in c.points:
print p
cluster_index += 1
print "the cost is %f" % cost
# Draw the graph for the clustering
# If the len(clusters) > 8, you may not be able to tell the difference because
# there are only 8 different colors...
cl.draw_2_dimentional_graph(clusters)