使用scipy进行聚类

近期做图像的时候,突然有个idea,须要进行聚类,事实上算法非常easy,可是当时非常急。就直接使用了scipy的cluster。

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

这是我从stackoverflow看到的一个demo。假设仅仅是简单的使用cluster。这样就能够了。

import scipy
import scipy.cluster.hierarchy as sch
import matplotlib.pylab as plt

scipy.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下来。



posted @ 2016-01-04 18:05  phlsheji  阅读(978)  评论(0编辑  收藏  举报