forked from sergeio/text_clustering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k_means.py
53 lines (39 loc) · 1.6 KB
/
k_means.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
import random
from similarity import similarity
class KMeans(object):
"""K-Means clustering. Uses cosine similarity as the distance function."""
def __init__(self, k, vectors):
assert len(vectors) >= k
self.centers = random.sample(vectors, k)
self.clusters = [[] for c in self.centers]
self.vectors = vectors
def update_clusters(self):
"""Determine which cluster center each `self.vector` is closest to."""
def closest_center_index(vector):
"""Get the index of the closest cluster center to `self.vector`."""
similarity_to_vector = lambda center: similarity(center,vector)
center = max(self.centers, key=similarity_to_vector)
return self.centers.index(center)
self.clusters = [[] for c in self.centers]
for vector in self.vectors:
index = closest_center_index(vector)
self.clusters[index].append(vector)
def update_centers(self):
"""Move `self.centers` to the centers of `self.clusters`.
Return True if centers moved, else False.
"""
new_centers = []
for cluster in self.clusters:
center = [average(ci) for ci in zip(*cluster)]
new_centers.append(center)
if new_centers == self.centers:
return False
self.centers = new_centers
return True
def main_loop(self):
"""Perform k-means clustering."""
self.update_clusters()
while self.update_centers():
self.update_clusters()
def average(sequence):
return sum(sequence) / len(sequence)