-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.py
77 lines (63 loc) · 1.96 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
# Example with a richer dataset.
# See: https://www.datascience.com/blog/introduction-to-k-means-clustering-algorithm-learn-data-science-tutorials
from pprint import pprint
from math import fsum, sqrt
from collections import defaultdict
from functools import partial
from random import sample
def dist(p,q):
return sqrt(fsum( (x1-x2)**2 for x1, x2 in zip(p,q) ) )
def mean(data):
data = list(data) # because people expect generator too
return fsum(data)/len(data)
def transpose(m):
return zip(*m)
def assign_data(centroids, points):
d = defaultdict(list)
for p in points:
c = min(centroids, key = partial(dist, p))
# print(p,'is closest to',c)
d[c].append(p)
return dict(d) # why not just return defaultdict? people knows dict better than defaultdict
def compute_centroids(groups):
return [tuple(map(mean, transpose(g))) for g in groups]
def kmeans(data, k, iterations=100): # with assumption that centroids converge
centroids = sample(data,k)
for _ in range(iterations):
labeled = assign_data(centroids, data)
centroids = compute_centroids(labeled.values())
return centroids
if __name__ == '__main__':
print('Simple example with six 3-D points clustered into two groups')
points = [
(10, 41, 23),
(22, 30, 29),
(11, 42, 5),
(20, 32, 4),
(12, 40, 12),
(21, 36, 23),
]
centroids = kmeans(points, k=2)
pprint(points)
print('centroids',centroids)
# data = [
# (10, 30),
# (12, 50),
# (14, 70),
#
# (9, 150),
# (20, 175),
# (8, 200),
# (14, 240),
#
# (50, 35),
# (40, 50),
# (45, 60),
# (55, 45),
#
# (60, 130),
# (60, 220),
# (70, 150),
# (60, 190),
# (90, 160),
# ]