matlab 聚类

原网址:http://blog.sina.com.cn/s/blog_62f3c4ef01014wz1.html

cited from:
cited from:http://hi.baidu.com/coralliu/blog/item/dbde033b168fedeb15cecbe5.html

http://bbs.sciencenet.cn/blog-41996-450513.html


MATLAB的统计工具箱中的多元统计分析中提供了聚类分析的两种方法:
1.层次聚类 hierarchical clustering

2.K-均值聚类


层次聚类(hierarchical clustering

这里用最简单的实例说明层次聚类原理和应用方法。

层次聚类是基于距离的聚类方法,MATLAB中通过pdist、linkage、dendrogram、cluster等函数来完成。

层次聚类的过程可以分这么几步:

(1) 确定对象(实际上就是数据集中的每个数据点)之间的相似性,实际上就是定义一个表征对象之间差异的距离,例如最简单的平面上点的聚类中,最经常使用的就是欧几里得距离。

这在MATLAB中可以通过Y=pdist(X)实现,例如
>> X=randn(6,2)
=
    -0.4326     1.1892
    -1.6656    -0.0376
     0.1253     0.3273
     0.2877     0.1746
    -1.1465    -0.1867
     1.1909     0.7258
>> plot(X(:,1),X(:,2),'bo')    %给个图,将来对照聚类结果把

MATLAB的层次聚类及k-均值聚类应用简述zz

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~图1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

>> Y=pdist(X)
=
   Columns through 14
     1.7394     1.0267     1.2442     1.5501     1.6883     1.8277     1.9648     0.5401    

2.9568     0.2228     1.3717     1.1377     1.4790     1.0581
   Column 15
     2.5092
例子中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的对称阵。
>> squareform(Y)
ans =
              1.7394     1.0267     1.2442     1.5501     1.6883
     1.7394              1.8277     1.9648     0.5401     2.9568
     1.0267     1.8277              0.2228     1.3717     1.1377
     1.2442     1.9648     0.2228              1.4790     1.0581
     1.5501     0.5401     1.3717     1.4790              2.5092
     1.6883     2.9568     1.1377     1.0581     2.5092          0
这里需要注意的是,pdist可以使用多种参数,指定不同的距离算法。help pdist把。
另外,当数据规模很大时,可以想象pdist产生的Y占用内存将是很吓人的,比如X有10k个数据点

,那么X占10k*8*2Bytes=160K,这看起来不算啥,但是pdist后的Y会有10k*10k/2*8Bytes=400M

。怕了把,所以,废话说在前面,用MATLAB的层次聚类来处理大规模数据,大概是很不合适的。
(2) 确定好了对象间的差异度(距离)后,就可以用Z=linkage(Y)来产生层次聚类树了。
>> Z=linkage(Y)
=
     3.0000     4.0000     0.2228
     2.0000     5.0000     0.5401
     1.0000     7.0000     1.0267
     6.0000     9.0000     1.0581
     8.0000    10.0000     1.3717
对于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来标识,依次类

推。

 Z =LINKAGE(Y) creates a hierarchical cluster tree, using thesingle
    linkagealgorithm.  The input Y is a distance matrix suchas is
    generated byPDIST.  Y may also be a more general dissimilarity
   matrix conforming to the output format of PDIST.

----------------------------------

相异度矩阵(DissimilarityMatrix)
  按n个数据对象两两间的相异度构建n阶矩阵(因为相异度矩阵是对称的,只需写出上三角或下三角即可).其中d(i,j)表示数据对象i与j的相异度,是一个非负的数值。当对象i和j越相似或“接近”时,d(i,j)值越接近0;而对象i和j越不相同或相距“越远”时,d(i,j)值越大。

显然,d(i,j)=d(j,i),d(i,i)=0。
----------------------------------

Cluster information will bereturned in the matrix Z with size m-1 by 3, where m is the numberof observations in the original data.
    Column 1 and 2 of Z contain clusterindices linked in pairs
   to form a binary tree. The leaf nodes are numbered from 1to
   m. They are the singleton clusters from which all higherclusters
   are built. Each newly-formed cluster, corresponding toZ(i,:), is
    assigned theindex m+i, where m is the total number of initial
    leaves.Z(i,1:2) contains theindices of the two component
   clusters which form cluster m+i. There are m-1 higherclusters
    whichcorrespond to the interior nodes of the output clustering
    tree.Z(i,3) contains thecorresponding linkage distances between
   the two clusters which are merged in Z(i,:), e.g. if thereare
    total of 30initial nodes, and at step 12, cluster 5 and cluster 7
    are combinedand their distance at this time is 1.5, then row 12
    of Z will be(5,7,1.5). The newly formed cluster will have an
    index12+30=42. If cluster 42 shows up in a latter row, that means
    this newlyformed cluster is being combined again into some bigger
   cluster.

通过linkage函数计算之后,实际上二叉树式的聚类已经完成了。Z这个数据数组不太好看,可以

用dendrogram(Z)来可视化聚类树。

MATLAB的层次聚类及k-均值聚类应用简述zz

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~图2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

可以看到,产生的聚类树的每一层都是一个倒置的U型(或者说是个n型,~~),纵轴高度代表了

当前聚类中两个子节点之间的距离。横轴上标记出了各个数据点索引下标。
稍微注意以下的是,dendrogram默认最多画30个最底层节点,当然可是设置参数改变这个限制,

比如dendrogram(Z,0)就会把所有数据点索引下标都标出来,但对于成千上万的数据集合,这样

的结果必然是图形下方非常拥挤。看你的应用目的了,随你玩~

(3)初步的聚类树画完后,还要做很多后期工作的,包括这样的聚类是不是可靠,是不是代表了

实际的对象分化模式,对于具体的应用,应该怎样认识这个完全版的聚类树,产生具有较少分叉

的可供决策参考的分类结果呢?这都是需要考虑的。

MATLAB中提供了cluster, clusterdata, cophenet, inconsistent等相关函数。

cluster用于剪裁完全版的聚类树,产生具有一定cutoff的可用于参考的树。
clusterdata可以认为是pdist,linkage,cluster的综合,当然更简易一点。
cophenet和inconsistent用来计算某些系数,前者用于检验一定算法下产生的二叉聚类树和实际

情况的相符程度(就是检测二叉聚类树中各元素间的距离和pdist计算产生的实际的距离之间有

多大的相关性),inconsistent则是量化某个层次的聚类上的节点间的差异性(可用于作为

cluster的剪裁标准)。

后面这些的理解,大概需要对聚类有一个更深刻更数学的认识,我也不是很清楚,就不多说了。


K-均值聚类

K-means聚类算法采用的是将N*P的矩阵X划分为K个类,使得所有类内对象与该类中心点之间的距离和最小。
 
IDX = KMEANS(X, K) partitions the points in theN-by-P data matrix X
      into Kclusters.  This partition minimizes the sum, overall clusters, of
      thewithin-cluster sums of point-to-cluster-centroid distances.
方法描述:

Given a set of observations (x1,x2, …, xn), where eachobservation is a d-dimensional real vector, k-means clustering aims to partitionthe n observations into k sets (k n) S {S1S2…, Sk}so as to minimize the within-cluster sum of squares(WCSS):

\underset{\mathbf{S}} {\operatorname{arg\,min}} \sum_{i=1}^{k} \sum_{\mathbf x_j \in S_i} \left\| \mathbf x_j - \boldsymbol\mu_i \right\|^2

where μi is the mean of pointsin Si.


In data mining, k-meansclustering is a method of cluster analysis which aims to partitionn observations into k clusters in which each observation belongs tothe cluster with the nearest mean. This results into apartitioning of the data space into Voronoi cells.
The problem is computationally difficult (NP-hard), however thereare efficient heuristic algorithms that are commonly employed andconverge fast to a local optimum. These are usually similar to theexpectation-maximization algorithm for mixtures of Gaussiandistributions via an iterative refinement approach employed by bothalgorithms. Additionally, they both use cluster centers to modelthe data, however k-means clustering tends to find clusters ofcomparable spatial extent, while the expectation-maximizationmechanism 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);...

    randn(100,2)-ones(100,2)]; 产生200个样本点,行指向每个样本,列是维变量值。

opts = statset('Display','final');

[idx,ctrs] =kmeans(X,2,'Distance','city','Replicates',5,'Options',opts);

for i1=1:length(X)
if idx(i1) == 1
      plot(X(i1,1),X(i1,2),'ro')
else
      plot(X(i1,1),X(i1,2),'ko')
end
end
plot(ctrs(:,1),ctrs(:,2),'rp','markersize',10)

结果如下图:
MATLAB的层次聚类及k-均值聚类应用简述zz
其中红色代表第一个类,黑色代表第二个类。红色五角星是两个类的中心
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值