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
- Kmedoids
- Kmedoids算法Java实现(可处理噪声)
- sql 一对多转一对一,选择多的第一条
- 10个免费学习编程的好助手
- c++字符编码的总结(ANSI、Unicode、UTF、UCS)
- ubuntu下安装与卸载软件方法
- 编译和解释的组合
- Kmedoids
- 程序测试大小端
- Java中的流、字符流和字节流
- java中常用的几个基础概念
- 在用 strtok函数的时候,在linux编译的问题
- Struts的图片的上传(只为未来自己用到的时候可以借鉴)。
- 【MFC】修改过的Excel处理类
- obj-c 自学随笔--- 加载UIBarButtonItem 到UINavigationController
- 如何查看和停止Linux启动的服务