cited
http://bbs.sciencenet.cn/blog-41996-450513.html
MATLAB的统计工具箱中的多元统计分析中提供了聚类分析的两种方法:
1.层次聚类
2.K-均值聚类
层次聚类(hierarchical
这里用最简单的实例说明层次聚类原理和应用方法。
层次聚类是基于距离的聚类方法,MATLAB中通过pdist、linkage、dendrogram、cluster等函数来完成。
层次聚类的过程可以分这么几步:
(1)
这在MATLAB中可以通过Y=pdist(X)实现,例如
>>
X
>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~图1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
Y
2.9568
例子中X数据集可以看作包含6个平面数据点,pdist之后的Y是一个行向量,15个元素分别代表X
的第1点与2-6点、第2点与3-6点,......这样的距离。那么对于M个点的数据集X,pdist之后的Y
将是具有M*(M-1)/2个元素的行向量。Y这样的显示虽然节省了内存空间,但对用户来说不是很易
懂,如果需要对这些距离进行特定操作的话,也不太好索引。MATLAB中可以用squareform把Y转
换成方阵形式,方阵中<i,j>位置的数值就是X中第i和第j点之间的距离,显然这个方阵应该是
个对角元素为0的对称阵。
>>
ans
这里需要注意的是,pdist可以使用多种参数,指定不同的距离算法。help
另外,当数据规模很大时,可以想象pdist产生的Y占用内存将是很吓人的,比如X有10k个数据点
,那么X占10k*8*2Bytes=160K,这看起来不算啥,但是pdist后的Y会有10k*10k/2*8Bytes=400M
。怕了把,所以,废话说在前面,用MATLAB的层次聚类来处理大规模数据,大概是很不合适的。
(2)
>>
Z
对于M个元素的X,前面说了Y是1行M*(M-1)/2的行向量,Z则是(M-1)*3的矩阵。
Z数组的前两列是索引下标列,最后一列是距离列。例如上例中表示在产生聚类树的计算过程中
,第3和第4点先聚成一类,他们之间的距离是0.2228,以此类推。要注意的是,为了标记每一个
节点,需要给新产生的聚类也安排一个标识,MATLAB中会将新产生的聚类依次用M+1,M+2,....依
次来标识。比如第3和第4点聚成的类以后就用7来标识,第2和第5点聚成的类用8来标识,依次类
推。
----------------------------------
相异度矩阵(Dissimilarity Matrix)
显然,d(i,j)=d(j,i),d(i,i)=0。
----------------------------------
Cluster information will be returned in the matrix Z with size m-1 by 3, where m is the number of observations in the original data.
通过linkage函数计算之后,实际上二叉树式的聚类已经完成了。Z这个数据数组不太好看,可以
用dendrogram(Z)来可视化聚类树。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~图2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
可以看到,产生的聚类树的每一层都是一个倒置的U型(或者说是个n型,~~),纵轴高度代表了
当前聚类中两个子节点之间的距离。横轴上标记出了各个数据点索引下标。
稍微注意以下的是,dendrogram默认最多画30个最底层节点,当然可是设置参数改变这个限制,
比如dendrogram(Z,0)就会把所有数据点索引下标都标出来,但对于成千上万的数据集合,这样
的结果必然是图形下方非常拥挤。看你的应用目的了,随你玩~
(3)初步的聚类树画完后,还要做很多后期工作的,包括这样的聚类是不是可靠,是不是代表了
实际的对象分化模式,对于具体的应用,应该怎样认识这个完全版的聚类树,产生具有较少分叉
的可供决策参考的分类结果呢?这都是需要考虑的。
MATLAB中提供了cluster,
cluster用于剪裁完全版的聚类树,产生具有一定cutoff的可用于参考的树。
clusterdata可以认为是pdist,linkage,cluster的综合,当然更简易一点。
cophenet和inconsistent用来计算某些系数,前者用于检验一定算法下产生的二叉聚类树和实际
情况的相符程度(就是检测二叉聚类树中各元素间的距离和pdist计算产生的实际的距离之间有
多大的相关性),inconsistent则是量化某个层次的聚类上的节点间的差异性(可用于作为
cluster的剪裁标准)。
后面这些的理解,大概需要对聚类有一个更深刻更数学的认识,我也不是很清楚,就不多说了。
K-均值聚类
方法描述:
Given a set of observations (x1, x2, …, xn), where each observation is a d-dimensional real vector, k-means clustering aims to partition the n observations into k sets (k ≤ n) S
where μi is the mean of points in Si.
In data mining, k-means clustering is a method of cluster analysis which aims to partition n observations into k clusters in which each observation belongs to the cluster with the nearest mean. This results into a partitioning of the data space into Voronoi cells.
The problem is computationally difficult (NP-hard), however there are efficient heuristic algorithms that are commonly employed and converge fast to a local optimum. These are usually similar to the expectation-maximization algorithm for mixtures of Gaussian distributions via an iterative refinement approach employed by both algorithms. Additionally, they both use cluster centers to model the data, however k-means clustering tends to find clusters of comparable spatial extent, while the expectation-maximization mechanism allows clusters to have different shapes.
使用方法:
Idx=Kmeans(X,K)
[Idx,C]=Kmeans(X,K)
[Idx,C,sumD]=Kmeans(X,K)
[Idx,C,sumD,D]=Kmeans(X,K)
[…]=Kmeans(…,’Param1’,Val1,’Param2’,Val2,…)
各输入输出参数介绍:
X N*P的数据矩阵
K 表示将X划分为几类,为整数
Idx N*1的向量,存储的是每个点的聚类标号
C K*P的矩阵,存储的是K个聚类质心位置
sumD 1*K的和向量,存储的是类内所有点与该类质心点距离之和
D N*K的矩阵,存储的是每个点与所有质心的距离
[…]=Kmeans(…,'Param1',Val1,'Param2',Val2,…)
这其中的参数Param1、Param2等,主要可以设置为如下:
1. ‘Distance’(距离测度)
‘sqEuclidean’ 欧式距离(默认时,采用此距离方式)
‘cityblock’ 绝度误差和,又称:L1
‘cosine’ 针对向量
‘correlation’
‘Hamming’ 只针对二进制数据
2. ‘Start’(初始质心位置选择方法)
‘sample’ 从X中随机选取K个质心点
‘uniform’ 根据X的分布范围均匀的随机生成K个质心
‘cluster’ 初始聚类阶段随机选择10%的X的子样本(此方法初始使用’sample’方法)
matrix 提供一K*P的矩阵,作为初始质心位置集合
3. ‘Replicates’(聚类重复次数)
使用案例:
X = [randn(100,2)+ones(100,2);...
opts = statset('Display','final');
[idx,ctrs] = kmeans(X,2,'Distance','city','Replicates',5,'Options',opts);
for i1=1:length(X)if idx(i1) == 1
else
end
end
plot(ctrs(:,1),ctrs(:,2),'rp','markersize',10)
结果如下图:
其中红色代表第一个类,黑色代表第二个类。红色五角星是两个类的中心