-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkm.py
43 lines (35 loc) · 1005 Bytes
/
km.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
global data
<init data>
def kmeans(k):
nochange=False
#init means
means={}
for i in range(k):
m=randrange(len(data))
while means.get(m)!=None:
m=randrange(len(data))
means[data[m]]=[]
while nochange==False:
#assigning points
for i in range (len(data)):
best=100000
for m in means.keys():
dist=distance(data[i],m)
if dist<best:
best=dist
mean=m
means[mean].append(i)
#calculate means
newmeans={}
for m,objects in means:
mean=calc_mean(objects)
newmeans[mean]=[]
if newmeans.keys()!=means.keys():
means=newmeans
else:
nochange=True
clusters={}
for m,objects in means:
cluster=[data[o] for o in objects]
clusters[m]=cluster
return clusters