c均值聚类算法实例

更新时间:2024-04-19 22:00:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

close all clear all clc

dataset=load('F:\\experience1.mat'); center1=[1 1 1;-1 1 -1]; center2=[0 0 0;1 1 -1]; center3=[0 0 0;1 1 1;-1 0 2];

center4=[-0.1 0 0.1;0 -0.1 0.1;-0.1 -0.1 0.1]; %%%%%%%%kmeans [k_class1,C1,sumd1,D1]=kmeans(dataset.data,2,'start',center1); one1=dataset.data(find(k_class1==1),:); second1=dataset.data(find(k_class1==2),:); scatter3(one1(:,1),one1(:,2),one1(:,3),'*','r') hold on

scatter3(second1(:,1),second1(:,2),second1(:,3),'o','g') hold off

xlabel('X1','Fontsize',15); ylabel('X2','Fontsize',15); zlabel('X3','Fontsize',15);

title('the first results of kmeans','Fontsize',15) %%%%%%%%%%%%%

[k_class2,C2,sumd2,D2]=kmeans(dataset.data,2,'start',center2); one2=dataset.data(find(k_class2==1),:);

second2=dataset.data(find(k_class2==2),:); figure,scatter3(one2(:,1),one2(:,2),one2(:,3),'*','r') hold on

scatter3(second2(:,1),second2(:,2),second2(:,3),'o','g') hold off

xlabel('X1','Fontsize',15);ylabel('X2','Fontsize',15);zlabel('X3','Fontsize',15);title('the second results of kmeans','Fontsize',15) %%%%%%%%%%%%%%%%%%%%

[k_class3,C3,sumd3,D3]=kmeans(dataset.data,3,'start',center3); one3=dataset.data(find(k_class3==1),:); second3=dataset.data(find(k_class3==2),:); third3=dataset.data(find(k_class3==3),:); figure,scatter3(one3(:,1),one3(:,2),one3(:,3),'*','r') hold on

scatter3(second3(:,1),second3(:,2),second3(:,3),'o','g') hold on

scatter3(third3(:,1),third3(:,2),third3(:,3),'x','b') hold off

xlabel('X1','Fontsize',15);ylabel('X2','Fontsize',15);zlabel('X3','Fontsize',15);title('the third results of kmeans','Fontsize',15)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [k_class4,C4,sumd4,D4]=kmeans(dataset.data,3,'start',center4);

one4=dataset.data(find(k_class4==1),:); second4=dataset.data(find(k_class4==2),:); third4=dataset.data(find(k_class4==3),:); figure,scatter3(one4(:,1),one4(:,2),one4(:,3),'*','r') hold on

scatter3(second4(:,1),second4(:,2),second4(:,3),'o','g') hold on

scatter3(third4(:,1),third4(:,2),third4(:,3),'x','b') hold off

xlabel('X1','Fontsize',15);ylabel('X2','Fontsize',15);zlabel('X3','Fontsize',15);title('the fourth results of kmeans','Fontsize',15) %%üm

[class_jwei1,fuzzy_jwei1]=fcm_jwei(dataset.sample,2,center1',2); [class_jwei2,fuzzy_jwei2]=fcm_jwei(dataset.sample,2,center2',2); [class_jwei3,fuzzy_jwei3]=fcm_jwei(dataset.sample,2,center3',3); [class_jwei4,fuzzy_jwei4]=fcm_jwei(dataset.sample,2,center4',3);

子函数fcm_jwei部分 function

[class_center,fuzzy,time]=fcm_jwei(sample,b,initial_center,class_num) % close all % clear all

% clc

% dataset=load('F:\\?£ê?ê?±e2010\\experience1.mat'); % initial_center=[0 0 0;1 1 1;-1 0 2]'; % b=2; % class_num=3;

% sample=dataset.sample; %%%%%%%%%%%%

%sample is a two demention matrix,each row is a feature [feature_num,sample_num]=size(sample);

fuzzy=zeros(class_num,sample_num); class_center=initial_center; class_cen=initial_center; time=0; while(1)

for i=1:sample_num for j=1:class_num

fuzzy(j,i)=(sample(:,i)-class_center(:,j))'*...

(sample(:,i)-class_center(:,j))+eps; fuzzy(j,i)=fuzzy(j,i).^(-(1/(b-1))); end end

normal_fuzzy=sum(fuzzy,1);

for j=1:class_num

fuzzy(j,:)=fuzzy(j,:)./normal_fuzzy; end

for j=1:class_num

sum_fuzzy=sum((fuzzy(j,:).^b),2)+eps; for k=1:feature_num

class_center(k,j)=((fuzzy(j,:).^b)*sample(k,:)')/(sum_fuzzy); end end

time=time+1; if

sum(sum((class_center-class_cen).*(class_center-class_cen)))<0.001; break; end

class_cen=class_center; end

本文来源:https://www.bwwdw.com/article/g62p.html

Top