From d516acdd2838bf1143e7507233696328b233e0a4 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 27 May 2018 13:22:12 -0500 Subject: [PATCH] python3 compatile k is set in cmd line arguements --- __pycache__/clustering.cpython-36.pyc | Bin 0 -> 5031 bytes __pycache__/point.cpython-36.pyc | Bin 0 -> 423 bytes clustering.py | 51 +++--- fountain.csv | 238 ++++++++++++++++++++++++++ main.py | 18 +- 5 files changed, 275 insertions(+), 32 deletions(-) create mode 100644 __pycache__/clustering.cpython-36.pyc create mode 100644 __pycache__/point.cpython-36.pyc create mode 100644 fountain.csv diff --git a/__pycache__/clustering.cpython-36.pyc b/__pycache__/clustering.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..da98b5b97c12c151096ea01fdd6569ba0199dd85 GIT binary patch literal 5031 zcmbVQO>7&-6`q;>;c`hzvaHDRPaP$WUDI)7M>ZP6a2zK!lGF%<0ygSmn_xk6Ruq>M zmz-TC47*#PmC-}d6lr_up~qed6gl_MLytWKJrz0Zt>{U&+zR;8es6Y3QI0O{O7s6e z?|a{S!yAo;yY_GQM?XEs*uU7^XCQwMA^rf7WRmxp$8{<^fmHO3UDGpp(Pnuz>WqG6 z*YTXPt{S>t?L8(#5wQfPC0*^Tf0FEb(RdC>~)$(ZpJ}>+pCr%V&4-z|Bp=7u->!l*HjcG z*X{>#FNucNI^E>8p^Czwoy6CAujz@d9d3MM_-XF=es|DKe4ob0E+WQlZVLoHIXTyN z+OKU=AAAi2>aS0`!x`(c%pCtC75CXkUuIUyLZ&V!9A~vcfnaQlRkqJL8~-UWQzNwY zQ9|F}XLf2!ahhcnJ?2AUW79RuCLLC;F@bB>SSQp;1Ora|&~( zW@_DGqwi2^vD55-8T$r=Nn;7q_pXTXX%*H}4`npzcjq zRgvV@5Fk!+s}Jxcx!sQjfK6`pyD>^1xB7cQoSVC?WCzF_sU$as(I>gpO@iIH*-(T4 zlm;>w=eFKdtSVGzN=)EY%6;aQ5=zsW&D`h(pXQccz_Z)Y9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$B2ag%fy}Hlf)SU$dT`2UX zO|CtCWb+A%4-sM_7P`TYoD6R4uf+)Dbx0IVk&BJ%$06&PdxzcuWW4WwMibGjHk(|U zQ-faNW^LS|_iI1Ci_CP$yZ_&A)azK~xb3J-H2ijMADa8(+mn*yw2OMX^W&8--uvg| z>01X8y5>AmS9Lnh`A*K?KOjyxAWbRTdEhRga6=bo>F8670q3^w@u2uMiITY)OCu87YZ#W z-Bs5p(k7cWhbLA)@$BtxXHNxQttF)&?>!mzqhyZnF3^yf!SE_EemubM5uORt@JVw~ zGlo~CLH>5X)hXXyv5!|Aei|sOzP2X@*6~DZ>{W_W#Pk~LM=DlVXbJ5W+WK)AdzIpK z7#}B`yiy|J7Z?`5kBGq;H2IQ#blGAFyhLsR#SWZr2Mz$a2qn8CA3Q=-HDgv5!0&i?8_q$K%3NHYKc literal 0 HcmV?d00001 diff --git a/__pycache__/point.cpython-36.pyc b/__pycache__/point.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b8c4cab767fcff3991a4f306ffcf9401ef3e87db GIT binary patch literal 423 zcmYjL!Ait15S^smZYjGW;!hOW?ZKMFVr8oCyJo-Didh!=M zIcag(f%h_bGcS3U%Vo0oNglrd;G1x$fVvIAoDtoD1j+V<4=zCR2S|gG^h0s#HUx7{ zq(CqUf=ed(9z-Am+7I3AURSLx{lmWe>1{Uzb4oO@0T2dGOm?!cJmqdy7q+r4s&)HZ z*)(vWQT3CEMj6F?((x1e8NKm)k@Z^J>{gkr)m=uGRoCkmRa%p6SB)xKlXX7Fx_fgm zV%1g_(dRHB5%7q;pNv7*|HGWJ(`H*V3ehEqjh4IG_Y1_=T~SYyeopo=#1TbaV?-Xt be-pFuo~P4!wCnTt-r(Y^>5J>pMGE`@gw$Js literal 0 HcmV?d00001 diff --git a/clustering.py b/clustering.py index 5f6981e..776a6c7 100644 --- a/clustering.py +++ b/clustering.py @@ -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 @@ -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 @@ -51,7 +52,7 @@ 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_) @@ -59,7 +60,7 @@ def initial_means(self, 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_) @@ -67,11 +68,11 @@ def initial_means(self, points): #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: @@ -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] @@ -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): @@ -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: @@ -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 = [] @@ -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: @@ -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 \ No newline at end of file + return 0 diff --git a/fountain.csv b/fountain.csv new file mode 100644 index 0000000..d89fd0c --- /dev/null +++ b/fountain.csv @@ -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 diff --git a/main.py b/main.py index 010dbd6..68823db 100644 --- a/main.py +++ b/main.py @@ -2,12 +2,13 @@ from clustering import clustering from point import Point import csv - +import sys +''' input 1st parameter to the number of clusters want to generate within 11 ''' geo_locs = [] #loc_ = Point(0.0, 0.0) #tuples for location #geo_locs.append(loc_) #read the fountains location from the csv input file and store each fountain location as a Point(latit,longit) object -f = open('/home/kazem/Downloads/Hackathon/drinkingFountains.csv', 'r') +f = open('fountain.csv', 'r') reader = csv.reader(f, delimiter=",") for line in reader: loc_ = Point(float(line[0]), float(line[1])) #tuples for location @@ -16,11 +17,14 @@ #for p in geo_locs: # print "%f %f" % (p.latit, p.longit) #let's run k_means clustering. the second parameter is the no of clusters -cluster = clustering(geo_locs, 8 ) -flag = cluster.k_means(False) +try: + cluster = clustering(geo_locs, int(sys.argv[1])) +except: + cluster = clustering(geo_locs,5) +flag = cluster.k_means(True) if flag == -1: - print "Error in arguments!" + print("Error in arguments!") else: #the clustering results is a list of lists where each list represents one cluster - print "clustering results:" - cluster.print_clusters(cluster.clusters) \ No newline at end of file + print("clustering results:") + cluster.print_clusters(cluster.clusters)