-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmeans.py
40 lines (33 loc) · 1.04 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
import numpy as np
import sys
def kmeans(centroids, arr):
while(True):
groups = assignPointsToClusters(centroids, arr)
new_centroids = updateClusters(groups)
if(np.array_equal(new_centroids, centroids)):
return centroids
else: centroids = new_centroids
def assignPointsToClusters(centroids, arr):
groups = []
for c in centroids:
groups.append([c, []])
for p in arr:
min_distance = sys.maxsize
cent = []
for c in centroids:
dist = np.linalg.norm(p - c)
if(dist < min_distance):
cent = c
min_distance = dist
for x in groups:
if(all([x[0][i]==cent[i] for i in range(len(cent))]) ):
x[1].append(p)
return groups
def updateClusters(groups):
newCentroids = []
for x in groups:
new_x = x[1][0]
for point in x[1][1:]:
new_x = new_x + point
newCentroids.append(np.true_divide(new_x, len(x[1])))
return newCentroids