• glorious208
    了解作者
  • matlab
    开发工具
  • 11KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 5
    下载次数
  • 2020-04-30 13:03
    上传日期
环境:matlab 算法:Kmeans 功能:对iris数据进行分类
Kmeans_iris.zip
  • Iris.xlsx
    13.1KB
  • K_means_iris.m
    2.7KB
内容介绍
clear clc iris_data=xlsread('iris','A1:D150'); iris_set=iris_data(1:50,1:4); iris_ver=iris_data(51:100,1:4); iris_vir=iris_data(101:150,1:4); correct_all=0; for j=1:10 rand_set=randperm(50); rand_ver=randperm(50); rand_vir=randperm(50); trainsample_set=iris_set(rand_set(:,1:37),1:4); trainsample_ver=iris_ver(rand_ver(:,1:37),1:4); trainsample_vir=iris_vir(rand_vir(:,1:37),1:4); trainsample=cat(1,trainsample_set,trainsample_ver,trainsample_vir); testsample_set=iris_set(rand_set(:,38:50),1:4); testsample_ver=iris_ver(rand_ver(:,38:50),1:4); testsample_vir=iris_vir(rand_vir(:,38:50),1:4); testsample=cat(1,testsample_set,testsample_ver,testsample_vir); m_1_new=trainsample(20,:); m_2_new=trainsample(55,:); m_3_new=trainsample(95,:); m_1=zeros(1,4); m_2=zeros(1,4); m_3=zeros(1,4); trainsample_new=trainsample; while(~(isequal(m_1,m_1_new)&&isequal(m_2,m_2_new)&&isequal(m_3,m_3_new))) c_1=[]; c_2=[]; c_3=[]; m_1=m_1_new; m_2=m_2_new; m_3=m_3_new; for i=1:111 X=trainsample_new(i,:); distance_1=norm(X-m_1,1); distance_2=norm(X-m_2,1); distance_3=norm(X-m_3,1); if(distance_1<=distance_2&&distance_1<=distance_3) c_1=[c_1;X]; elseif(distance_2<=distance_1&&distance_2<=distance_3) c_2=[c_2;X]; else c_3=[c_3;X]; end end m_1_new=mean(c_1); m_2_new=mean(c_2); m_3_new=mean(c_3); trainsample_new=cat(1,c_1,c_2,c_3); end m_1=belong(trainsample(1:37,:),c_1,c_2,m_1_new,m_2_new,m_3_new); m_2=belong(trainsample(38:74,:),c_1,c_2,m_1_new,m_2_new,m_3_new); m_3=belong(trainsample(75:111,:),c_1,c_2,m_1_new,m_2_new,m_3_new); correct_set=0; correct_ver=0; correct_vir=0; for i=1:39 X=testsample(i,:); distance_1=norm(X-m_1,1); distance_2=norm(X-m_2,1); distance_3=norm(X-m_3,1); if(distance_1<=distance_2&&distance_1<=distance_3) if(i<=13) correct_set=correct_set+1; end elseif(distance_2<=distance_1&&distance_2<=distance_3) if(i>13&&i<=26) correct_ver=correct_ver+1; end else correct_vir=correct_vir+1; end end correct=correct_set+correct_ver+correct_vir; fprintf('第%d次实验正确率:%4.2f%%\n',j,correct/39*100); correct_all=correct_all+correct/39; end fprintf('\n平均正确率为:%4.2f%%',correct_all*10);
评论
    相关推荐