Kmedoids

import Pycluster as pc
import numpy as np
import sys
import matplotlib.pylab as pl
#Def our distance function :maximum norm
def dist(a,b):
    return max(abs(a-b))
#Read data filename and desired number of clusters from command line
filename , n =sys.argv[1],int(sys.argv[2])


#x and y coordinates,whitespace-separated
data =np.loadtxt(filename,usecols=(0,1))
K=len(data)    


#Calcuate the distance matrix 
m=np.zeros(K*K)
m.shape =(K,K)
#print m.shape
for i in range(0,K):
    for j in range(i,K):
        d =dist(data[i],data[j])
        m[i][j]=d
        m[j][i]=d
clustermap = pc.kmedoids(m,n,npass=20)[0]
#print clustermap
medoids = {}
for i in clustermap:
    #print medoids.get(i,0)
    medoids[i]= medoids.get(i,0) + 1
#print medoids.keys()
#print points ,grouped by cluster


for i in medoids.keys():
    print "Cluster = ",i,"Mass=",medoids[i],"Centroid: ",data[i][0],data[i][1]
    pl.plot(data[i][0],data[i][1],"k*")
    for j in range(0,len(data)):
        if clustermap[j] == i:
            print "\t",data[j]


x ,y = [d[0] for d in list(data)],[d[1] for d in list(data)]
pl.xlim(0,10)
pl.ylim(0,10)
pl.plot(x,y,".y")


#pl.show()
pl.savefig("D:\\figure.png")


数据1 2.6
2 1
2 1.5
3 4
2.7 3.5
2.4 3.2
5.5 9.2
6 9
5.8 9
3 2
1 2.8
2 1.6
7 8
7.3 8.2
6.9 8.5

你可能感兴趣的:(Python,学习之Data,analysis)