-
Notifications
You must be signed in to change notification settings - Fork 37
/
Merging Clusters.py
66 lines (66 loc) · 2.28 KB
/
Merging Clusters.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
# Enter code for the ClusterSet class in this box
class ClusterSet(object):
""" A ClusterSet is defined as a list of clusters """
def __init__(self, pointType):
""" Initialize an empty set, without any clusters """
self.members = []
def add(self, c):
""" Append a cluster to the end of the cluster list
only if it doesn't already exist. If it is already in the
cluster set, raise a ValueError """
if c in self.members:
raise ValueError
self.members.append(c)
def getClusters(self):
return self.members[:]
def mergeClusters(self, c1, c2):
points = []
for p in c1.members():
points.append(p)
for p in c2.members():
points.append(p)
newC = Cluster(points, type(p))
self.members.remove(c1)
self.members.remove(c2)
self.add(newC)
return c1, c2
def findClosest(self, metric):
minDistance = metric(self.members[0], self.members[1])
toMerge = (self.members[0], self.members[1])
for c1 in self.members:
for c2 in self.members:
if c1 == c2:
continue
if metric(c1, c2) < minDistance:
minDistance = metric(c1, c2)
toMerge = (c1, c2)
return toMerge
def mergeOne(self, metric, toPrint = False):
if len(self.members) == 1:
return None
if len(self.members) == 2:
return self.mergeClusters(self.members[0],
self.members[1])
toMerge = self.findClosest(metric)
if toPrint:
print 'Merged'
print ' ' + str(toMerge[0])
print 'with'
print ' ' + str(toMerge[1])
self.mergeClusters(toMerge[0], toMerge[1])
return toMerge
def numClusters(self):
return len(self.members)
def toStr(self):
cNames = []
for c in self.members:
cNames.append(c.getNames())
cNames.sort()
result = ''
for i in range(len(cNames)):
names = ''
for n in cNames[i]:
names += n + ', '
names = names[:-2]
result += ' C' + str(i) + ':' + names + '\n'
return result