matlab实现的的knn分类器 有训练样本和测试样本我要分享

MATLAB implementation of knn classifier training a

matlab 分类 测试 knn 训练 实现 样本

关注次数: 228

下载次数: 0

文件大小: 1.71 kB

代码分类: 其他

开发平台: matlab

下载需要积分: 2积分

版权声明:如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

代码描述

中文说明:clear;clc;ticload data.txt;a=data(1:30,1:4);aa=data(31:50,1:4);b=data(51:80,1:4);bb=data(81:100,1:4);c=data(101:130,1:4);cc=data(131:150,1:4);train_sample=cat(1,a,b,c);%组成训练样本(90*4)test_sample=cat(1,aa,bb,cc);%组成测试样本(60*4)k=5;cha=zeros(1,90);sum=0;[i,j]=size(train_sample);[u,v]=size(test_sample);for x=1:u    for y=1:i        result=sqrt((test_sample(x,1)-train_sample(y,1)).^2+(test_sample(x,2)-train_sample(y,2)).^2+(test_sample(x,3)-train_sample(y,3)).^2+(test_sample(x,4)-train_sample(y,4)).^2);        cha(1,y)=result;    end;    [z,ind]=sort(cha);    m1=0;    m2=0;    m3=0;    for n=1:k        if ind(1,n)<+30           


English Description:

clear;clc;ticload data.txt;a=data(1:30,1:4);aa=data(31:50,1:4);b=data(51:80,1:4);bb=data(81:100,1:4);c=data(101:130,1:4);cc=data(131:150,1:4);Train_sample=cat (1,a,b,c);% composed of training samples (90*4)Test_sample=cat (1,AA,BB,CC);% test sample (60*4)k=5;cha=zeros(1,90);sum=0;[i,j]=size(train_sample);[u,v]=size(test_sample);for x=1:ufor y=1:iresult=sqrt((test_sample(x,1)-train_sample(y,1)).^2+(test_sample(x,2)-train_sample(y,2)).^2+(test_sample(x,3)-train_sample(y,3)).^2+(test_sample(x,4)-train_sample(y,4 )).^2);cha(1,y)=result;end;[z,ind]=sort(cha);m1=0;m2=0;m3=0;for n=1:kif ind(1,n)<+30m1=m1+1;elseif ind(1,n)>30&&ind(1,n)<+60m2=m2+1;elsem3=m3+1;endendL=[m1,m2,m3];if m1==max(L)m=1;elseif m2==max(L)<


代码预览