层次聚类,转自http://blog.sina.com.cn/s/blog_62f3c4ef01014uhe.html
Matlab提供系列函数用于聚类分析,归纳起来具体方法有如下:
对于M个点的数据集X,pdist之后的Y将是具有M*(M-1)/2个元素的行向量。
Y这样的显示虽然节省了内存空间,但对用户来说不是很易懂,如果需要对这些距离进行特定操作的话,也不太好索引。MATLAB中可以用squareform把Y转换成方阵形式,方阵中<i,j>位置的数值就是X中第i和第j点之间的距离,显然这个方阵应该是个对角元素为0的对称阵。
CLUSTERDATA Construct clusters from data.
T = CLUSTERDATA(X, CUTOFF) constructs clusters from data X.
X is a matrix of size M by N, treated as M observations of N
variables. CUTOFF is a threshold for cutting the hierarchical
tree generated by LINKAGE into clusters. When 0 < CUTOFF < 2,
clusters are formed when inconsistent values are greater than
CUTOFF (see INCONSISTENT). When CUTOFF is an integer and CUTOFF >= 2,
then CUTOFF is considered as the maximum number of clusters to
keep in the hierarchical tree generated by LINKAGE. The output T is
a vector of size M containing a cluster number for each observation.
When 0 < CUTOFF < 2, T = CLUSTERDATA(X,CUTOFF) is equivalent to:
Y = pdist(X, ‘euclid‘);
Z = linkage(Y, ‘single‘);
T = cluster(Z, ‘cutoff‘, CUTOFF);
When CUTOFF is an integer >= 2, T = CLUSTERDATA(X,CUTOFF) is equivalent
to:
Y = pdist(X,‘euclid‘);
Z = linkage(Y,‘single‘);
T = cluster(Z,‘maxclust‘,CUTOFF)
1.8 Inconsistent
INCONSISTENT Inconsistent values of a cluster tree.
Y = INCONSISTENT(Z) computes the inconsistent value of each non-leaf
node in the hierarchical cluster tree Z. Z is a (M-1)-by-3 matrix
generated by the function LINKAGE. Each inconsistent value is a
measure of separation between the two clusters whose merge is
represented by that node, compared to the separation between
subclusters merged within those clusters.
Y = INCONSISTENT(Z, DEPTH) computes inconsistent values by looking
to a depth DEPTH below each node.
Y is a (M-1)-by-4 matrix, with rows corresponding to each of the
non-leaf nodes represented in Z. INCONSISTENT computes the
inconsistent value for node (M+i) using S_i, the set of nodes less than
DEPTH branches below node (M+i), excluding any leaf nodes.
S_i是除了叶节点外,所有深度低于(M+i)不超过DEPTH的节点(包括M+i节点自身)
而Inconsistent计算的是S_i的距离的平均值。
Then
Y(i,1) = mean(Z(S_i,3)), the mean height of nodes in S_i
Y(i,2) = std(Z(S_i,3)), the standard deviation of node heights in S_i
Y(i,3) = length(S_i), the number of nodes in S_i
Y(i,4) = (Z(i,3) - Y(i,1))/Y(i,2), the inconsistent value
The default value for DEPTH is 2.
计算深度会影响不一致系数的计算结果,计算深度比较大时,不一致系数的增量能反映出当前步引入的新样品与该类中心(涉及该类中所有样品)的距离远近,计算深度比较小时,不一致系数的增量仅能反映出当前步引入的新样品与上几步聚类中涉及的样品的中心的距离远近。
结果:
1
1
表明两个观测属于同一类。
再如下面的例子:
x1=randn(10,1);
x2=randn(10,1)+10;
x3=randn(10,1)+20;
x=[x1;x2;x3];
y=randn(30,1);
T=clusterdata([x,y],3)
temp1=find(T==1)
plot(x(temp1),y(temp1),‘rd‘,‘markersize‘,10,‘markerfacecolor‘,‘r‘)
hold on
temp1=find(T==2)
plot(x(temp1),y(temp1),‘yd‘,‘markersize‘,10,‘markerfacecolor‘,‘y‘)
temp1=find(T==3)
plot(x(temp1),y(temp1),‘kd‘,‘markersize‘,10,‘markerfacecolor‘,‘k‘)
legend(‘cluster 1‘,‘cluster 2‘,‘cluster 3‘)
结果如下图:
ZSCORE Standardized z score.
Z = ZSCORE(X) returns a centered, scaled version of X, the same size as X.
For vector input X, Z is the vector of z-scores (X-MEAN(X)) ./ STD(X). For
matrix X, z-scores are computed using the mean and standard deviation
along each column of X. For higher-dimensional arrays, z-scores are
computed using the mean and standard deviation along the first
non-singleton dimension.
The columns of Z have sample mean zero and sample standard deviation one
(unless a column of X is constant, in which case that column of Z is
constant at 0).
---------------------------------------------------------------------
X2=zscore(X);
Step2
Step3
Step4 创建聚类,并作出谱系图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。