python 2.7 - Percentage of proximity to cluster in kmeans -


i trying clustering in python.

this script follows:

import scipy import numpy np scipy.cluster.vq import kmeans2 collections import counter  def dist(a,b): return np.linalg.norm(a-b)  x = scipy.randn(100, 2)  k=int(raw_input("give value of k: "))  pts = 100*np.random.random((12,2)) centroids, assigned_clusters = kmeans2(x, k) pt in pts:     #print tuple(pt)     distperxy=dict()     i=1     cent in centroids:         distperxy[i]=(float(dist(pt,list(cent))))         i+=1     mnmm=min(distperxy.iterkeys(), key=(lambda key: distperxy[key]))     tot = sum(distperxy.values())     perc=distperxy[mnmm]/tot     print mnmm,perc 

basically trying determine nearest cluster each point , percentage of proximity cluster. example if point near centroid1 in k-means k=3. percentage calculation described in code above,i.e, dividing distance closest cluster sum of distances clusters?


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -