forked from Introtocs/Week9_Lec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_student.py
61 lines (50 loc) · 1.79 KB
/
cluster_student.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
import sample_student as sample
import util
import helper
# Cluster class
class Cluster(object):
def __init__(self, samples):
"""Assumes samples is a list"""
self.samples = samples
""" centrold is also an instance of Sample class """
self.centroid = self.computeCentroid()
def size(self):
return len(self.samples)
def getCentroid(self):
return self.centroid
def getMembers(self):
return self.samples
#### Implement the centroid computing function here!
def computeCentroid(self):
'''
return an instance of Sample, its features should be
the center of all the samples in the cluster
'''
return helper.computeCentroid(self)
#### Implement the centroid updating function here!
def update(self, samples):
"""Replace the samples in the cluster by new samples
Return: how much the centroid has changed"""
return helper.update(self, samples)
def __str__(self):
names = []
for e in self.samples:
names.append(e.getName())
names.sort()
result = 'Cluster with centroid '\
+ str(self.centroid.getFeatures()) + ' contains:\n '
for e in names:
result = result + e + ', '
return result[:-2]
if __name__ == "__main__":
test_samples = util.genDistribution()
c = Cluster(test_samples)
print(c.centroid)
print("cluster center: ", c.centroid.features)
util.plot_cluster([c])
# now assign the cluster new samples, and move it
test_samples2 = util.genDistribution(1, 1, 1, 1, 30)
diff = c.update(test_samples2)
print("center moved: ", diff)
# plot_cluster expects an array of cluster...
util.plot_cluster([c])