data_kmeans.m文件
%将多分类的letter数据进行聚簇 % load('letter_norm'); % idx=kmeans(letter_data,3);%返回每个数据属于哪个簇 % letter_label=cell2mat(letter_label);%将cell数据类型转化成char类型 % row=size(letter_label,1); % L=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; % mat=zeros(26,3); % for i=1:row % for j=1:26 % if L(j)==letter_label(i,1) % mat(j,idx(i))= mat(j,idx(i))+1; % end % end % end % [max_num,index]=max(mat,[],2);%index将会返回聚簇的结果 % %然后将类别重新划分,划分成为三类 % letter_label_group=[]; % for i=1:row % for j=1:26 % if L(j)==letter_label(i,1) % letter_label_group=[letter_label_group;index(j,1)]; % end % end % end % save letter_3group.mat letter_data letter_label_group; %聚簇shuttle数据: load('shuttle_norm'); idx=kmeans(shuttle_data,3);%返回每个数据属于哪个簇 row=size(shuttle_data,1); mat=zeros(7,3); for i=1:row for j=1:7 if j==shuttle_label(i,1) mat(j,idx(i))= mat(j,idx(i))+1; end end end [max_num,index]=max(mat,[],2);%index将会返回聚簇的结果 %然后将类别重新划分,划分成为三类 shuttle_label_group=[]; for i=1:row for j=1:7 if j==shuttle_label(i,1) shuttle_label_group=[shuttle_label_group;index(j,1)]; end end end save shuttle_3group.mat shuttle_data shuttle_label_group;
%----------------------------------------------------------------------------------------------- %重写magic数据 load('magic_norm'); row=size(magic_data,1); magic_label_group=zeros(row,1); for i=1:row if cell2mat(magic_label(i,1))=='g' magic_label_group(i,1)=1; else magic_label_group(i,1)=-1; end end magic=[magic_data magic_label_group]; rowrank = randperm(size(magic, 1));%z这两行的目的是打乱数据分布 magic = magic(rowrank, :); magic_data=magic(:,1:10); magic_label_group=magic(:,11); save magic_2group.mat magic_data magic_label_group;
test.m文件
%看看重新聚簇的数据的分布情况 % load('shuttle_3group.mat') % r=size(shuttle_data,1); % s=zeros(3,1); % for i=1:r % for j=1:3 % if shuttle_label_group(i,1)==j % s(j,1)=s(j,1)+1; % end % end % end %结果分布s:6791,34251,2458 %----------------------------------------------------------------------------------------- load('letter_3group.mat') r=size(letter_data,1); s=zeros(3,1); for i=1:r for j=1:3 if letter_label_group(i,1)==j s(j,1)=s(j,1)+1; end end end %结果分布s:2326,7019,10655 %----------------------------------------------------------------------------------------- load('magic_norm.mat') r=size(magic_data,1); s=zeros(2,1); for i=1:r if cell2mat(magic_label(i,1))=='g' s(1,1)=s(1,1)+1; else s(2,1)=s(2,1)+1; end end %结果分布s:12332,6688
标签:end,letter,聚簇,label,---,毕业设计,shuttle,magic,group From: https://www.cnblogs.com/kekexxr/p/14173299.html