function [y]=kmeans_C(m,k,rep)
%%%%%%%%%%%%%%%%
%
% kMeansCluster - Simple k means clustering algorithm
% Author: Kardi Teknomo, Ph.D.
%
% Purpose: classify the objects in data matrix based on the attributes
% Criteria: minimize Euclidean distance between centroids and object points
% For more explanation of the algorithm, see http://people.revoledu.com/kardi/tutorial/kMean/index.html
% Output: matrix data plus an additional column represent the group of each object
%
% Example: m = [ 1 1; 2 1; 4 3; 5 4] or in a nice form
% m = [ 1 1;
% 2 1;
% 4 3;
% 5 4]
% k = 2
% kMeansCluster(m,k) produces m = [ 1 1 1;
% 2 1 1;
% 4 3 2;
% 5 4 2]
% Input:
% m - required, matrix data: objects in rows and attributes in columns
% k - optional, number of groups (default = 1)
% isRand - optional, if using random initialization isRand=1, otherwise input any number (default)
% it will assign the first k data as initial centroids
%
% Local Variables
% f - row number of data that belong to group i
% c - centroid coordinate size (1:k, 1:maxCol)
% g - current iteration group matrix size (1:maxRow)
% i - scalar iterator
% maxCol - scalar number of rows in the data matrix m = number of attributes
% maxRow - scalar number of columns in the data matrix m = number of objects
% temp - previous iteration group matrix size (1:maxRow)
% z - minimum value (not needed)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin<2, k=1; end
[maxRow, maxCol]=size(m) %返回矩阵m的行数和列数
u=0;
avg=0;
avg1=0;
for i=1:rep
q=i;
if maxRow<=k, %如果聚类的数量大于等于最大行数
y=[m,(1:maxRow)'] %把矩阵的每一行作为一个聚类中心,在最后一列添加一个矩阵标识。例如:X=[1 2 3;1 3 5;12 13 1]
%kMeansCluster(X,4) 则产生y =
% 1 2 3 1
% 1 3 5 2
% 12 13 1 3
else
p = randperm(size(m,1)); % random initialization 用randperm产生一个行矩阵 数组元素是1到m的行数之间的整数。
for i=1:k
p(i)
c(i,:)=m(p(i),:); % 随机选择m中的K行作为聚类中心
end
% initial value of centroid 初始化聚类中心
Z=c%用z来存储随机产生的聚类中心。
temp=zeros(maxRow,1); % initialize as zero vector 产生一个maxrow行1列的全0矩阵
count=0;
tic
while 1,
d=DistMatrix(m,c); % calculate objcets-centroid distances 调用DistMatrix函数求矩阵m中的每行和聚类中心的距离
[z,g]=min(d,[],2); % find group matrix g 求矩阵d中每列的最小值作为一个行矩阵。例如:d=[11 2 24;14 23 4;1 26 8]则返回[1 2 4]
%存储在z中并用g标识
if g==temp,
break; % stop the iteration
else
temp=g; % copy group matrix to temporary variable
end
g;
e=0;
e1=0;
count=count+1
for i=1:k
f=find(g==i);
if f % only compute centroid if f is not empty
n=m(find(g==i),:);
c(i,:)=mean(m(find(g==i),:),1); %求m的符合条件f的 每一列的平均值,产生一个行矩阵 作为聚类中心。例如: Example: If X = [0 1 2; 3 4 5]
%每次迭代完成后的平均误差总和
end
end
v=c
d=DistMatrix(m,v);
[z,g]=min(d,[],2);
for i=1:k
w=m(find(g==i),:);
%then mean(X,1) is [1.5 2.5 3.5] and mean(X,2) is [1;4] 求每一次找到的属于簇Ci的所有点
[a,b]=size(w);
for j=1:a
h=w(j,:)-v(i,:);
l=h.*h;
e1=e1+sum(l);
end
end
e=e1
end
u=u+e;
avg1=u/rep;
end
toc
g
end
function d=DistMatrix(A,B)
%%%%%%%%%%%%%%%%%%%%%%%%%
% DISTMATRIX return distance matrix between points in A=[x1 y1 ... w1] and in B=[x2 y2 ... w2]
% Copyright (c) 2005 by Kardi Teknomo, http://people.revoledu.com/kardi/
%
% Numbers of rows (represent points) in A and B are not necessarily the same.
% It can be use for distance-in-a-slice (Spacing) or distance-between-slice (Headway),
%
% A and B must contain the same number of columns (represent variables of n dimensions),
% first column is the X coordinates, second column is the Y coordinates, and so on.
% The distance matrix is distance between points in A as rows
% and points in B as columns.
% example: Spacing= dist(A,A)
% Headway = dist(A,B), with hA ~= hB or hA=hB
% A=[1 2 3; 4 5 6; 2 4 6; 1 2 3]; B=[4 5 1; 6 2 0]
% dist(A,B)= [ 4.69 5.83;
% 5.00 7.00;
% 5.48 7.48;
% 4.69 5.83]
%
% dist(B,A)= [ 4.69 5.00 5.48 4.69;
% 5.83 7.00 7.48 5.83]
%repmat(A(:,k),1,hB)表示产生的矩阵是一行 hB列的行矩阵 形式如[A(:,k) A(:,k)。。。。]
%例如k=1 则C{k}=[1 1;4 4;2 2;1 1]
%欧氏距离:(∑(Xi-Yi)2)1/2,即两项间的差是每个变量值差的平方和再平方根,目的是计算其间的整体距离即不相似性。
%在二维和三维空间中的欧式距离的就是两点之间的距离,二维的公式是
%d = sqrt((x1-x2)^+(y1-y2)^)
%三维的公式是
%d=sqrt(x1-x2)^+(y1-y2)^+z1-z2)^)
% 推广到n维空间,欧式距离的公式是
%d=sqrt( ∑(xi1-xi2)^ ) 这里i=1,2..n
%xi1表示第一个点的第i维坐标,xi2表示第二个点的第i维坐标
%例如A=[x1 y1 z1; B=[x5 y5 z5;
% x2 y2 z2; x6 y5 z5]
% x3 y3 z3;
% x4 y4 z4]
%C{1}=[x1 x1; D{1}'=[x5 x6;
% x2 x2; x5 x6;
% x3 x3; x5 x6;
% x4 x4] x5 x6]
%(C{1}-D{1}').^2+(C{2}-D{2}').^2+(C{3}-D{3}').^2这个结果和
%(x1-x2).^2+(y1-y2).^2+(z1-z2).^2结果是一样的
%%%%%%%%%%%%%%%%%%%%%%%%%%%
pujulei.rar_k值聚类_pujulei_site:www.pudn.com_谱聚类_谱聚类k均值
版权申诉
105 浏览量
2022-09-21
17:31:54
上传
评论
收藏 3KB RAR 举报
weixin_42653672
- 粉丝: 94
- 资源: 1万+
最新资源
- travel 基于协同过滤算法的景点推荐系统.zip
- light room9.1.apk
- (单片机Protues入门笔记)单片机Proteus仿真软件入门笔记约80多页,内容图像并茂
- (单片机Proteus案例)基于80c51单片机的比赛计分器电路仿真实现
- 增强型51单片机(stc15系列)驱动ws2812彩灯模块库文件
- (单片机Protues案例)基于51单片机交通灯仿真实现
- 23种设计模式-C++实现.zip
- (单片机Protues案例)基于51单片机的频率计仿真实现
- (单片机Proteus案例)Proteus单片机仿真实例之键盘系列
- (单片机Proteus案例)Proteus单片机仿真实例之电机系列
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈