Kmedoids

来源:互联网 发布:单片机crc校验程序 编辑:程序博客网 时间:2024/05/15 03:42
import Pycluster as pcimport numpy as npimport sysimport matplotlib.pylab as pl#Def our distance function :maximum normdef dist(a,b):    return max(abs(a-b))#Read data filename and desired number of clusters from command linefilename , n =sys.argv[1],int(sys.argv[2])#x and y coordinates,whitespace-separateddata =np.loadtxt(filename,usecols=(0,1))K=len(data)    #Calcuate the distance matrix m=np.zeros(K*K)m.shape =(K,K)#print m.shapefor i in range(0,K):    for j in range(i,K):        d =dist(data[i],data[j])        m[i][j]=d        m[j][i]=dclustermap = pc.kmedoids(m,n,npass=20)[0]#print clustermapmedoids = {}for i in clustermap:    #print medoids.get(i,0)    medoids[i]= medoids.get(i,0) + 1#print medoids.keys()#print points ,grouped by clusterfor 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