File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change 1111import 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+
1437def 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
You can’t perform that action at this time.
0 commit comments