From e6cc1ca5550fbdd6cc8a25bd87564fb1af10836b Mon Sep 17 00:00:00 2001 From: Atsushi Sakai Date: Wed, 2 May 2018 10:05:25 +0900 Subject: [PATCH] keep implementing --- Mapping/kmean_clustering/kmean_clustering.py | 32 +++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Mapping/kmean_clustering/kmean_clustering.py b/Mapping/kmean_clustering/kmean_clustering.py index 4e1f9688..1fb76757 100644 --- a/Mapping/kmean_clustering/kmean_clustering.py +++ b/Mapping/kmean_clustering/kmean_clustering.py @@ -11,6 +11,29 @@ import matplotlib.pyplot as plt import random +class Cluster: + + def __init__(self): + self.x = [] + self.y = [] + self.cx = None + self.cy = None + + +def kmean_clustering(rx, ry, nc): + + minx, maxx = min(rx), max(rx) + miny, maxy = min(ry), max(ry) + + clusters = [Cluster() for i in range(nc)] + + for c in clusters: + c.cx = random.uniform(minx, maxx) + c.cy = random.uniform(miny, maxy) + + return clusters + + def calc_raw_data(): rx, ry = [], [] @@ -33,7 +56,14 @@ def main(): rx, ry = calc_raw_data() - plt.plot(rx, ry, "x") + ncluster = 2 + clusters = kmean_clustering(rx, ry, ncluster) + + for c in clusters: + print(c.cx, c.cy) + plt.plot(c.cx, c.cy, "x") + + plt.plot(rx, ry, ".") plt.show()