-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClustering - K-means.py
More file actions
51 lines (43 loc) · 1.47 KB
/
Clustering - K-means.py
File metadata and controls
51 lines (43 loc) · 1.47 KB
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
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point({self.x}, {self.y})"
def distance(self, other):
return sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
class Cluster(object):
def __init__(self, x, y):
self.center = Point(x, y)
self.points = []
def __repr__ (self):
return f'[Point({self.x},{self.y})]'
def update(self):
total = [0,0]
for point in self.points:
total[0] = total[0] + point.x
total[1] = total[1] + point.y
csum = total
length = float(len(self.points))
self.center = Point(csum.x/length, csum.y/length)
self.points = []
def add_point(self, point):
self.point.append(point)
def compute_result(points):
points = [Point (*points) for point in points]
a = Cluster(1,0)
b = Cluster(-1,0)
a_old = []
for _ in range(10000):
for point in points:
if point.distance(a.center) < point.distance(b.center):
a.add_point(point)
else:
b.add_point(point)
if a_old == a.points:
break
a_old = [i for i in a.points]
a.update()
b.update()
return ((a.center.x, a.center.y),
(b.center.x, b.center.y))