【转】使用scipy进行层次聚类和k-means聚类
scipy cluster库简介
scipy.cluster是scipy下的一个做聚类的package, 共包含了两类聚类方法:
1. 矢量量化(scipy.cluster.vq):支持vector quantization 和 k-means 聚类方法
2. 层次聚类(scipy.cluster.hierarchy):支持hierarchical clustering 和 agglomerative clustering(凝聚聚类)
聚类方法实现:k-means和hierarchical clustering.
###cluster.py
#导入相应的包
import scipy
import scipy.cluster.hierarchy as sch
from scipy.cluster.vq import vq,kmeans,whiten
import numpy as np
import matplotlib.pylab as plt #生成待聚类的数据点,这里生成了20个点,每个点4维:
points=scipy.randn(20,4) #1. 层次聚类
#生成点与点之间的距离矩阵,这里用的欧氏距离:
disMat = sch.distance.pdist(points,'euclidean')
#进行层次聚类:
Z=sch.linkage(disMat,method='average')
#将层级聚类结果以树状图表示出来并保存为plot_dendrogram.png
P=sch.dendrogram(Z)
plt.savefig('plot_dendrogram.png')
#根据linkage matrix Z得到聚类结果:
cluster= sch.fcluster(Z, t=1, 'inconsistent') print "Original cluster by hierarchy clustering:\n",cluster #2. k-means聚类
#将原始数据做归一化处理
data=whiten(points) #使用kmeans函数进行聚类,输入第一维为数据,第二维为聚类个数k.
#有些时候我们可能不知道最终究竟聚成多少类,一个办法是用层次聚类的结果进行初始化.当然也可以直接输入某个数值.
#k-means最后输出的结果其实是两维的,第一维是聚类中心,第二维是损失distortion,我们在这里只取第一维,所以最后有个[0]
centroid=kmeans(data,max(cluster))[0] #使用vq函数根据聚类中心对所有数据进行分类,vq的输出也是两维的,[0]表示的是所有数据的label
label=vq(data,centroid)[0] print "Final clustering by k-means:\n",label
在Terminal中输入:python cluster.py
输出:
Original cluster by hierarchy clustering:
[4 3 3 1 3 3 2 3 2 3 2 3 3 2 3 1 3 3 2 2]
Final clustering by k-means:
[1 2 1 3 1 2 0 2 0 0 0 2 1 0 1 3 2 2 0 0]
数值是随机标的,不用看,只需要关注同类的是哪些.可以看出层次聚类的结果和k-means还是有区别的.
补充:一些函数的用法
1.linkage(y, method=’single’, metric=’euclidean’)
共包含3个参数:
y是距离矩阵,由pdist得到;method是指计算类间距离的方法,比较常用的有3种:
(1)single:最近邻,把类与类间距离最近的作为类间距
(2)complete:最远邻,把类与类间距离最远的作为类间距
(3)average:平均距离,类与类间所有pairs距离的平均
其他的method还有如weighted,centroid等等,具体可以参考: http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html#scipy.cluster.hierarchy.linkage
2.fcluster(Z, t, criterion=’inconsistent’, depth=2, R=None, monocrit=None)
第一个参数Z是linkage得到的矩阵,记录了层次聚类的层次信息; t是一个聚类的阈值-“The threshold to apply when forming flat clusters”,在实际中,感觉这个阈值的选取还是蛮重要的.另外,scipy提供了多种实施阈值的方法(criterion):
inconsistent : If a cluster node and all its descendants have an inconsistent value less than or equal to t then all its leaf descendants belong to the same flat cluster. When no non-singleton cluster meets this criterion, every node is assigned to its own cluster. (Default)
distance : Forms flat clusters so that the original observations in each flat cluster have no greater a cophenetic distance than t.
……
其他的参数我用的是默认的,具体可以参考:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.fcluster.html#scipy.cluster.hierarchy.fcluster
3.kmeans(obs, k_or_guess, iter=20, thresh=1e-05, check_finite=True)
输入obs是数据矩阵,行代表数据数目,列代表特征维度; k_or_guess表示聚类数目;iter表示循环次数,最终返回损失最小的那一次的聚类中心;
输出有两个,第一个是聚类中心(codebook),第二个是损失distortion,即聚类后各数据点到其聚类中心的距离的加和.
4.vq(obs, code_book, check_finite=True)
根据聚类中心将所有数据进行分类.obs为数据,code_book则是kmeans产生的聚类中心.
输出同样有两个:第一个是各个数据属于哪一类的label,第二个和kmeans的第二个输出是一样的,都是distortion
【转】使用scipy进行层次聚类和k-means聚类的更多相关文章
- ML: 聚类算法-K均值聚类
基于划分方法聚类算法R包: K-均值聚类(K-means) stats::kmeans().fpc::kmeansruns() K-中心点聚类(K-Medoids) ...
- 聚类之K均值聚类和EM算法
这篇博客整理K均值聚类的内容,包括: 1.K均值聚类的原理: 2.初始类中心的选择和类别数K的确定: 3.K均值聚类和EM算法.高斯混合模型的关系. 一.K均值聚类的原理 K均值聚类(K-means) ...
- 软件——机器学习与Python,聚类,K——means
K-means是一种聚类算法: 这里运用k-means进行31个城市的分类 城市的数据保存在city.txt文件中,内容如下: BJ,2959.19,730.79,749.41,513.34,467. ...
- 机器学习理论与实战(十)K均值聚类和二分K均值聚类
接下来就要说下无监督机器学习方法,所谓无监督机器学习前面也说过,就是没有标签的情况,对样本数据进行聚类分析.关联性分析等.主要包括K均值聚类(K-means clustering)和关联分析,这两大类 ...
- 机器学习之K均值聚类
聚类的核心概念是相似度或距离,有很多相似度或距离的方法,比如欧式距离.马氏距离.相关系数.余弦定理.层次聚类和K均值聚类等 1. K均值聚类思想 K均值聚类的基本思想是,通过迭代的方法寻找K个 ...
- ML: 聚类算法R包-K中心点聚类
K-medodis与K-means比较相似,但是K-medoids和K-means是有区别的,不一样的地方在于中心点的选取,在K-means中,我们将中心点取为当前cluster中所有数据点的平均值, ...
- 100天搞定机器学习|day44 k均值聚类数学推导与python实现
[如何正确使用「K均值聚类」? 1.k均值聚类模型 给定样本,每个样本都是m为特征向量,模型目标是将n个样本分到k个不停的类或簇中,每个样本到其所属类的中心的距离最小,每个样本只能属于一个类.用C表示 ...
- SciPy k均值聚类
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- K均值聚类的失效性分析
K均值聚类是一种应用广泛的聚类技术,特别是它不依赖于任何对数据所做的假设,比如说,给定一个数据集合及对应的类数目,就可以运用K均值方法,通过最小化均方误差,来进行聚类分析. 因此,K均值实际上是一个最 ...
随机推荐
- MongoDB 学习笔记(8)---$type 操作符
$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果. MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1 String 2 Object 3 ...
- MySQL复制的管理和维护
1.查看主库 mysql> show master status; mysql> show master status; +------------------+-----------+- ...
- Inondb中的checkpoint
checkpoint主要是为了解决一下问题: 1.缩短数据库的恢复时间 2.缓冲池不够用时,将脏页刷新到磁盘 3.重做日志不可用时,刷新脏页 Innodb引擎使用LSN(log sequence nu ...
- excel中对数据进行分类求和
我们在用excel处理数据时,常常需要按不同的类别分别汇总数据.例如下图中需要求出每个业务员的总销售金额等. 通常情况下我们的数据量很大,而且需要较快的统计出来结果,所以我们要用一定的技巧才能计算出来 ...
- 关于 IOS code signe 和 Provisioning Files 机制 浅析
可以先读下这个译文. http://www.cnblogs.com/zilongshanren/archive/2011/08/30/2159086.html 读后,有以下疑惑. 在mac 机上生成的 ...
- 关于iReport-5.6.0之前版本 “无法启动”或者“一开就关闭”的 某些原因
在经过Google搜索后.外国的大神说: We don't support Java 8 right now. Support will be added in next versions. 大概意思 ...
- PCIe简介及引脚定义
参考文章:http://www.2cto.com/os/201607/523581.html http://blog.csdn.net/michaelcao1980/article/details/4 ...
- centos7配置网桥
在Centos7上玩KVM的话一定要配置网桥的: [root@localhost nb]# ls /etc/sysconfig/network-scripts ifcfg-8866 ifdown if ...
- mongoose修改数组中某个特定的值
写博客的时候有一个这样的业务,一个标签集合和一个文章集合,它们是多对多的关系,文章集合中tags字段包含它对应的标签,现在修改标签集合中某条标签记录的名字,文章集合中所有包含这个标签的tags字段的值 ...
- [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5
.NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...