使用scipy进行聚类

来源:互联网 发布:linux内核开发教程 编辑:程序博客网 时间:2024/06/05 17:16

最近做图像的时候,突然有个idea,需要进行聚类,其实算法很简单,但是当时很急,就直接使用了scipy的cluster。

使用起来其实很简单,但是中文的文章很少,所以就简单的介绍一下,感兴趣的也可以自己去看一下英文的文档:http://docs.scipy.org/doc/scipy/reference/cluster.html。

这是我从stackoverflow看到的一个demo,如果只是简单的使用cluster,这样就可以了。

import scipyimport scipy.cluster.hierarchy as schimport matplotlib.pylab as pltscipy.randn(100,2)d = sch.distance.pdist(X)Z= sch.linkage(d,method='complete')P =sch.dendrogram(Z)plt.savefig('plot_dendrogram.png')T = sch.fcluster(Z, 0.5*d.max(), 'distance')#array([4, 5, 3, 2, 2, 3, 5, 2, 2, 5, 2, 2, 2, 3, 2, 3, 2, 5, 4, 5, 2, 5, 2,#       3, 3, 3, 1, 3, 4, 2, 2, 4, 2, 4, 3, 3, 2, 5, 5, 5, 3, 2, 2, 2, 5, 4,#       2, 4, 2, 2, 5, 5, 1, 2, 3, 2, 2, 5, 4, 2, 5, 4, 3, 5, 4, 4, 2, 2, 2,#       4, 2, 5, 2, 2, 3, 3, 2, 4, 5, 3, 4, 4, 2, 1, 5, 4, 2, 2, 5, 5, 2, 2,#       5, 5, 5, 4, 3, 3, 2, 4], dtype=int32)sch.leaders(Z,T)

如果想要了解更多一些的话,可以参考 http://nbviewer.ipython.org/github/herrfz/dataanalysis/blob/master/week4/clustering_example.ipynb

这篇文章其实写的已经非常棒了,虽然文件我没有能够成功down下来。



0 0
原创粉丝点击