%%%%%%%%%%%%%%%%%%%实现c均值聚类算法(欧式距离)%%%%%%%%%%%%%%%%%%%%%%%%%%%%初始中心选择不当可能会导致聚类失败(已改进)%%%%%%%%%%%%%%%%1改进初始聚类中心的选取,先随机选一个然后选与之距离最大的,%%%然后选与已有中心加起来聚类最大的%%%2加入类内距离的和J的计算closeall;clear,clc;c=4;%聚类数x=[0,0;4,4;5,0;0,1;1,0;4,5;5,4;5,5;6,0;6,1];%%选初始聚类中心ii_julu=0;%记录距离最大值对应的ii、jj的值jj_julu=0;forll=1:c-1juli_old=0;juli=0;ifll==1forii=1:length(x(:,1))forjj=1:length(x(:,1))juli=(norm(x(ii,:)-x(jj,:)))^2;ifjulijuli_oldjuli_old=juli;ii_juli=ii;jj_juli=jj;endendendchushizhongxin(ll,:)=x(ii_juli,:);chushizhongxin(ll+1,:)=x(jj_juli,:);elseforii=1:length(x(:,1))cc=1;forkk=1:ll%排除已经选出的初始中心点cc=(sum(abs((x(ii,:)-chushizhongxin(kk,:))))&cc);endifccforkk=1:lljuli=(norm(x(ii,:)-chushizhongxin(kk,:)))^2+juli;endifjulijuli_oldjuli_old=juli;ii_juli=ii;endjuli=0;endendchushizhongxin(ll+1,:)=x(ii_juli,:);endend%%%马氏距离的一块内容%xm=mean(x);%x列向量相加求均值%v=zeros(length(x(1,:)),length(x(1,:)));%forl=1:length(x(:,1));%v=(x(l,:)-xm).'*(x(l,:)-xm)+v;%end%v=v./(length(x(:,1))-1);%%shuyu=zeros(1,length(x(:,1)));%与样本对应属于哪一类就记为几%z=zeros(c,length(x(1,:)));%%forl=1:c;%初始化聚类中心%%z(l,:)=x(l,:);%%end%z(1,:)=x(1,:);%初始的聚类中心可先随机选取第一个%z(2,:)=x(7,:);%而后选与第一个距离最大的那个%z(3,:)=x(10,:);z=chushizhongxin;yz=z+1;%仅仅是初始化上一次聚类中心forw=1:10;%迭代次数ifsum(sum(yz~=z));%矩阵相等的判断后仍是矩阵yz=z;%进行本次迭代前将记录上次聚类中心%%1聚类%%%%%%%%%%%%%%%%%%%%%%%%%%forll=1:length(x(:,1));%共有x的列向量个数参加聚类p=1;%计数值,属于哪一类就记数字几m=zeros(1,c);%暂存某数与各聚类中心的距离,共c个forl=1:c;m(l)=norm(x(ll,:)-z(l,:));%采用欧式距离%m(l)=(x(ll,:)-z(l,:))*inv(v)*(x(ll,:)-z(l,:)).';%马氏距离pp=0;forkk=1:c-1%排除已经选出的初始中心点cc(kk)=(m(l)m(kk));endpp=sum(cc);ifl1&&~ppp=l;endendshuyu(ll)=p;end%%2更改聚类中心%%%%%%%%%%%%%%%%%%%%%%xi=zeros(1,c);%记录每一类所含样本数%forl=1:length(x(:,1));%forll=1:c;%ifshuyu(l)==ll;%xi(ll)=xi(ll)+1;%此处是按123的正序,而实际中不行%end%end%endpsy=zeros(1,c);%记录123出现的次序pi=1;forl=1:length(x(:,1));if~ismember(shuyu(l),psy);%psy中没有的才记下psy(pi)=shuyu(l);pi=pi+1;endendforll=1:c;forl=1:length(x(:,1));ifshuyu(l)==psy(ll);%按照psy中123值的顺序记录次数xi(ll)=xi(ll)+1;endendendbz=zeros(c,length(x(1,:)));%本次聚类中心forl=1:c;%shuyu()中有c种类型1到cforll=1:length(x(:,1));%shuyu()有x的列向量个元素ifshuyu(ll)==psy(l);bz(l,:)=bz(l,:)+x(ll,:);%属于一类的对应位置相加endendendz=bz;forl=1:c;z(l,:)=z(l,:)./xi(l);%除以本类样本个数end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%elsebreak;end%15行if的end%14行for的%%%计算类内距离J%gg=zeros(1,c);%jj=1;%forii=1:length(x(:,1))%ifii==1%gg(jj)=shuyu(ii);%end%tt=0;%hh=jj;%whilehh0&&ii1%tt(hh)=(shuyu(ii)==gg(hh));%hh=hh-1;%end%pp=sum(tt);%ifii1&&~pp%jj=jj+1;%gg(jj)=shuyu(ii);%end%end%%J=zeros(1,c);%leineijuli=0;%forjj=1:length(x(:,1))%leineijuli(jj)=norm(x(jj,:)-z(gg(shuyu(jj)),:));%end%forii=1:length(gg)%forjj=1:length(x(:,1))%%ifii1&&shuyu(jj)==gg(ii)%%J(ii)=J(ii-1)+leineijuli(jj);%%else%ifshuyu(jj)==gg(ii)%J(ii)=leineijuli(jj)+J(ii);%end%end%end%%sumJ=sum(J);