我需要使用KNN搜索对测试数据进行分类并找到分类率。
下面是matlab代码:例如:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load fisheriris
x = meas(:,3:4); % x =all training data
y = [5 1.45;6 2;2.75 .75]; % y =3 testing data
[n,d] = knnsearch(x,y,'k',10); % find the 10 nearest neighbors to three testing data
for b=1:3
tabulate(species(n(b,:)))
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
结果显示在命令窗口中:
tabulate(species(n(1,:)))
Value Count Percent
virginica 2 20.00%
versicolor 8 80.00%
tabulate(species(n(2,:)))
Value Count Percent
virginica 10 100.00%
tabulate(species(n(3,:)))
Value Count Percent
versicolor 7 70.00%
setosa 3 30.00%
如果测试点是'Versicolor',则第一个和第三个测试点的结果分类正确,第二个测试点的结果是错误的,所以分类率为2/3 x100%=66.7%。
是否有任何想法修改matlab代码以自动找到分类率并将结果保存到工作区中?