Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3 compatible, k is set in cmd line arguements #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added __pycache__/clustering.cpython-36.pyc
Binary file not shown.
Binary file added __pycache__/point.cpython-36.pyc
Binary file not shown.
51 changes: 26 additions & 25 deletions clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class clustering:
def __init__(self, geo_locs_, k_):
self.geo_locations = geo_locs_
self.k = k_
#self.draw_graph =draw_graph
self.clusters = [] #clusters of nodes
self.means = [] #means of clusters
self.debug = False #debug flag
Expand All @@ -20,23 +21,23 @@ def next_random(self, index, points, clusters):
dist = {}
for point_1 in points:
if self.debug:
print 'point_1: %f %f' % (point_1.latit, point_1.longit)
print('point_1: %f %f' % (point_1.latit, point_1.longit))
#compute this node distance from all other points in cluster
for cluster in clusters.values():
for cluster in list(clusters.values()):
point_2 = cluster[0]
if self.debug:
print 'point_2: %f %f' % (point_2.latit, point_2.longit)
print('point_2: %f %f' % (point_2.latit, point_2.longit))
if point_1 not in dist:
dist[point_1] = math.sqrt(math.pow(point_1.latit - point_2.latit,2.0) + math.pow(point_1.longit - point_2.longit,2.0))
else:
dist[point_1] += math.sqrt(math.pow(point_1.latit - point_2.latit,2.0) + math.pow(point_1.longit - point_2.longit,2.0))
if self.debug:
for key, value in dist.items():
print "(%f, %f) ==> %f" % (key.latit,key.longit,value)
for key, value in list(dist.items()):
print("(%f, %f) ==> %f" % (key.latit,key.longit,value))
#now let's return the point that has the maximum distance from previous nodes
count_ = 0
max_ = 0
for key, value in dist.items():
for key, value in list(dist.items()):
if count_ == 0:
max_ = value
max_point = key
Expand All @@ -51,27 +52,27 @@ def initial_means(self, points):
#pick the first node at random
point_ = rand.choice(points)
if self.debug:
print 'point#0: %f %f' % (point_.latit, point_.longit)
print('point#0: %f %f' % (point_.latit, point_.longit))
clusters = dict()
clusters.setdefault(0, []).append(point_)
points.remove(point_)
#now let's pick k-1 more random points
for i in range(1, self.k):
point_ = self.next_random(i, points, clusters)
if self.debug:
print 'point#%d: %f %f' % (i, point_.latit, point_.longit)
print('point#%d: %f %f' % (i, point_.latit, point_.longit))
#clusters.append([point_])
clusters.setdefault(i, []).append(point_)
points.remove(point_)
#compute mean of clusters
#self.print_clusters(clusters)
self.means = self.compute_mean(clusters)
if self.debug:
print "initial means:"
print("initial means:")
self.print_means(self.means)
def compute_mean(self, clusters):
means = []
for cluster in clusters.values():
for cluster in list(clusters.values()):
mean_point = Point(0.0, 0.0)
cnt = 0.0
for point in cluster:
Expand All @@ -86,18 +87,18 @@ def compute_mean(self, clusters):
#this method assign nodes to the cluster with the smallest mean
def assign_points(self, points):
if self.debug:
print "assign points"
print("assign points")
clusters = dict()
for point in points:
dist = []
if self.debug:
print "point(%f,%f)" % (point.latit, point.longit)
print("point(%f,%f)" % (point.latit, point.longit))
#find the best cluster for this node
for mean in self.means:
dist.append(math.sqrt(math.pow(point.latit - mean.latit,2.0) + math.pow(point.longit - mean.longit,2.0)))
#let's find the smallest mean
if self.debug:
print dist
print(dist)
cnt_ = 0
index = 0
min_ = dist[0]
Expand All @@ -107,7 +108,7 @@ def assign_points(self, points):
index = cnt_
cnt_ += 1
if self.debug:
print "index: %d" % index
print("index: %d" % index)
clusters.setdefault(index, []).append(point)
return clusters
def update_means(self, means, threshold):
Expand All @@ -116,23 +117,23 @@ def update_means(self, means, threshold):
mean_1 = self.means[i]
mean_2 = means[i]
if self.debug:
print "mean_1(%f,%f)" % (mean_1.latit, mean_1.longit)
print "mean_2(%f,%f)" % (mean_2.latit, mean_2.longit)
print("mean_1(%f,%f)" % (mean_1.latit, mean_1.longit))
print("mean_2(%f,%f)" % (mean_2.latit, mean_2.longit))
if math.sqrt(math.pow(mean_1.latit - mean_2.latit,2.0) + math.pow(mean_1.longit - mean_2.longit,2.0)) > threshold:
return False
return True
#debug function: print cluster points
def print_clusters(self, clusters):
cluster_cnt = 1
for cluster in clusters.values():
print "nodes in cluster #%d" % cluster_cnt
for cluster in list(clusters.values()):
print("nodes in cluster #%d" % cluster_cnt)
cluster_cnt += 1
for point in cluster:
print "point(%f,%f)" % (point.latit, point.longit)
print("point(%f,%f)" % (point.latit, point.longit))
#print means
def print_means(self, means):
for point in means:
print "%f %f" % (point.latit, point.longit)
print("%f %f" % (point.latit, point.longit))
#k_means algorithm
def k_means(self, plot_flag):
if len(self.geo_locations) < self.k:
Expand All @@ -149,9 +150,9 @@ def k_means(self, plot_flag):
self.print_clusters(clusters)
means = self.compute_mean(clusters)
if self.debug:
print "means:"
print self.print_means(means)
print "update mean:"
print("means:")
print(self.print_means(means))
print("update mean:")
stop = self.update_means(means, 0.01)
if not stop:
self.means = []
Expand All @@ -164,7 +165,7 @@ def k_means(self, plot_flag):
markers = ['o', 'd', 'x', 'h', 'H', 7, 4, 5, 6, '8', 'p', ',', '+', '.', 's', '*', 3, 0, 1, 2]
colors = ['r', 'k', 'b', [0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]]
cnt = 0
for cluster in clusters.values():
for cluster in list(clusters.values()):
latits = []
longits = []
for point in cluster:
Expand All @@ -173,4 +174,4 @@ def k_means(self, plot_flag):
ax.scatter(longits, latits, s=60, c=colors[cnt], marker=markers[cnt])
cnt += 1
plt.show()
return 0
return 0
238 changes: 238 additions & 0 deletions fountain.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
49.2349470004,-123.027254
49.2758580003,-123.024141
49.2855540004,-123.142392
49.2609799996,-123.184758999
49.2788109997,-123.106194
49.2779509998,-123.103575
49.2621280002,-123.155318
49.2486789998,-123.157991
49.2859189999,-123.120939
49.2456140004,-123.175342
49.2551910002,-123.050071
49.2155210002,-123.056107
49.2474500002,-123.066226
49.2904150002,-123.050483
49.2846537002,-123.101821
49.2569260001,-123.172514
49.2495850001,-123.191688
49.268012,-123.125901001
49.2643720004,-123.083134
49.2616440001,-123.079061001
49.2567460001,-123.073107
49.2706990004,-123.050137
49.2712810004,-123.051686
49.2898710003,-123.125385
49.2369459996,-123.03638
49.2318250001,-123.113005
49.2628690002,-123.159336
49.2617676001,-123.16143
49.2738085999,-123.113057
49.2727980001,-123.114649
49.274263527,-123.103009123
49.2716320002,-123.12572
49.2723940003,-123.123289
49.2949850004,-123.136001001
49.251936,-123.122502
49.2530889996,-123.122316999
49.2054060003,-123.132849001
49.2334839997,-123.163401
49.2769910003,-123.123796001
49.2765358996,-123.124281999
49.2874189999,-123.142573001
49.286027,-123.143034999
49.2789006997,-123.137935
49.2548420001,-123.029979
49.2064199999,-123.150808001
49.2071300002,-123.149336999
49.2082106,-123.045695
49.2112962003,-123.042283001
49.2139346997,-123.051533001
49.2123920998,-123.051395
49.268389,-123.058428
49.2364980002,-123.031411001
49.2124169999,-123.093601999
49.271802,-123.129792999
49.2737420284,-123.069867198
49.2735720072,-123.070810532
49.2671275002,-123.137756
49.2409770001,-123.084994
49.2642200002,-123.095575
49.290001,-123.121694
49.2912279998,-123.12327
49.2905389997,-123.12318
49.2803689997,-123.040624001
49.2803790001,-123.038221
49.2831439999,-123.042959
49.2825629998,-123.034264
49.2837082178,-123.033390575
49.2828920403,-123.034500567
49.2817359504,-123.034387066
49.2826765167,-123.033258474
49.2456626001,-123.106969
49.2445404432,-123.106375175
49.2719084,-123.191736
49.2539290002,-123.061986
49.2641639999,-123.107514001
49.2639539997,-123.045152
49.2381380003,-123.07429
49.233331,-123.157199999
49.2291950003,-123.045343
49.227059,-123.041881999
49.247325,-123.074579
49.2767389996,-123.151743001
49.274681454,-123.153201414
49.274376596,-123.153819692
49.2194056999,-123.106335
49.2230752,-123.116335999
49.2237850998,-123.112543
49.2181671865,-123.111721242
49.2749689996,-123.207164
49.2299700002,-123.098511
49.2788421999,-123.089442
49.2615753,-123.107009
49.22318,-123.159702
49.2235070003,-123.160831
49.2056239998,-123.138957999
49.2679230003,-123.180134
49.2203778002,-123.170894999
49.2209551004,-123.167863
49.2205906996,-123.169628
49.220632856,-123.170739863
49.2364239998,-123.029107
49.2315229253,-123.084772336
49.2313408215,-123.086922341
49.2433580003,-123.187296
49.2153386004,-123.0872
49.2148279997,-123.084599
49.2874290002,-123.142264
49.2731920004,-123.074217001
49.2568650001,-123.106446
49.2282770003,-123.05692
49.2828379999,-123.129981001
49.2892680001,-123.036666
49.2894309996,-123.038691
49.2895900001,-123.035531
49.2381730001,-123.050828
49.2168820001,-123.128155
49.2165849997,-123.127387
49.2166069999,-123.129122
49.282901,-123.094057
49.2823132209,-123.057406084
49.2722364144,-123.10942862
49.2710507,-123.110206
49.2816628999,-123.104505
49.2374200002,-123.105621
49.2422750004,-123.113543
49.2391670004,-123.111542
49.2446200002,-123.148828
49.2521100001,-123.042086
49.2425827262,-123.104807231
49.2057550004,-123.051817001
49.2057390001,-123.050981
49.2577920001,-123.091912
49.2168110003,-123.082434
49.2169470002,-123.082635
49.2711200004,-123.033079
49.2701480001,-123.030627
49.2730710004,-123.030636
49.2709900002,-123.146205
49.2720769018,-123.105753845
49.2195978578,-123.141491315
49.2582542735,-123.049751007
49.2442940002,-123.048373
49.277484,-123.222395999
49.2910335573,-123.145379704
49.2990629752,-123.124291357
49.2996494238,-123.126153454
49.3007690329,-123.117131045
49.2936453068,-123.148721581
49.2925245244,-123.144676748
49.2914730029,-123.146904443
49.2995278046,-123.12130009
49.3029999997,-123.130695001
49.2990240023,-123.13415947
49.3133824271,-123.1424526
49.310617943,-123.1482033
49.3123876933,-123.142730486
49.2930070003,-123.146487
49.2948789945,-123.150027364
49.2954671999,-123.151056
49.2978703483,-123.134986376
49.2994996407,-123.133804936
49.292219621,-123.146012494
49.3031559997,-123.156335
49.2922390003,-123.147
49.2989513836,-123.120690089
49.3015616002,-123.134529
49.2759096215,-123.085577226
49.2748270001,-123.086688999
49.2668930001,-123.035669001
49.2788959998,-123.13802
49.2228493013,-123.096482187
49.2691209998,-123.134031
49.2573559998,-123.09859
49.2296076996,-123.070191
49.2283710002,-123.118266
49.2512730003,-123.163837
49.2749603002,-123.093122
49.2605190936,-123.050134814
49.2394013003,-123.129439
49.2386154001,-123.129938999
49.2751119999,-123.139769
49.2716129673,-123.066762285
49.2818450001,-123.110066
49.2630479999,-123.073108001
49.2851899998,-123.128045
49.2819870001,-123.136052
49.2660210001,-123.205262
49.2668536046,-123.205161207
49.2158390004,-123.113059
49.2784420003,-123.073424
49.2663411,-123.125914
49.257924,-123.041381
49.269566,-123.136177
49.2733345,-123.131579
49.2737142,-123.1527806
49.2730368,-123.098886
49.265329,-123.142081
49.245762,-123.10102
49.237587,-123.127384
49.237446,-123.105647
49.2371952,-123.09728295
49.234752,-123.058912
49.26822777,-123.152706
49.219505,-123.106415
49.2646599,-123.112967
49.2187548,-123.055044
49.262875,-123.105026
49.26423514,-123.1666474
49.262066,-123.065619
49.2792475454,-123.129232101
49.28451988,-123.13955439
49.2678194,-123.06939067
49.2867384,-123.136516
49.29067407,-123.14264686
49.2887857,-123.13969
49.28922483,-123.13854854
49.2365274,-123.084037
49.28977703,-123.14128655
49.29049866,-123.14022516
49.2173117,-123.139586
49.281542,-123.119937
49.281386,-123.097231
49.280998,-123.054448
49.2610594,-123.100784
49.28571023,-123.13333675
49.282385,-123.140508
49.28306599,-123.12229054
49.29345716,-123.13827002
49.2775724,-123.087389
49.262294,-123.114544
49.281882,-123.133952
49.229344,-123.127987
49.271936,-123.179475
49.205331,-123.031131
49.213173,-123.114629
49.279672,-123.123513
49.271674,-123.167863
49.262628,-123.089225