Skip to content

Commit e6cc1ca

Browse files
committed
keep implementing
1 parent 3085c5e commit e6cc1ca

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Mapping/kmean_clustering/kmean_clustering.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
import random
1212

1313

14+
class Cluster:
15+
16+
def __init__(self):
17+
self.x = []
18+
self.y = []
19+
self.cx = None
20+
self.cy = None
21+
22+
23+
def kmean_clustering(rx, ry, nc):
24+
25+
minx, maxx = min(rx), max(rx)
26+
miny, maxy = min(ry), max(ry)
27+
28+
clusters = [Cluster() for i in range(nc)]
29+
30+
for c in clusters:
31+
c.cx = random.uniform(minx, maxx)
32+
c.cy = random.uniform(miny, maxy)
33+
34+
return clusters
35+
36+
1437
def calc_raw_data():
1538

1639
rx, ry = [], []
@@ -33,7 +56,14 @@ def main():
3356

3457
rx, ry = calc_raw_data()
3558

36-
plt.plot(rx, ry, "x")
59+
ncluster = 2
60+
clusters = kmean_clustering(rx, ry, ncluster)
61+
62+
for c in clusters:
63+
print(c.cx, c.cy)
64+
plt.plot(c.cx, c.cy, "x")
65+
66+
plt.plot(rx, ry, ".")
3767
plt.show()
3868

3969

0 commit comments

Comments
 (0)