如何在Matlab中使用knnsearch设置k值

问题描述 投票:0回答:1

我有一个对图像进行分类的代码。

training1 = xlsread('Data Train');

% mentions the location of the training data matrix in the excel file
training = [training1(:,1) training1(:,2) training1(:,3) training1(:,4) training1(:,5) training1(:,6) training1(:,7) training1(:,8) training1(:,9) training1(:,10) training1(:,11) training1(:,12) training1(:,13) training1(:,14) training1(:,15) training1(:,16) training1(:,17) training1(:,18) training1(:,19) training1(:,20) training1(:,21) training1(:,22) training1(:,23) training1(:,24)];

% mentions input data variables
Z=[MeanR MeanG MeanB MeanH MeanS MeanV VarRed VarGreen VarBlue VarH VarS VarV RangeR RangeG RangeB RangeH RangeS RangeV sdR sdG sdB sdH sdS sdV];

%perform knn classification
result = knnsearch(training,Z);

if (result>=1 && result<=20)
    set(handles.EditBox,'string','Raw');
elseif (result>=21 && result<=40)
    set(handles.EditBox,'string','Undercook');
elseif (result>=41 && result<=60)
    set(handles.EditBox,'string','Cook');
elseif (result>=61 && result<=80)
    set(handles.EditBox,'string','Rotten');
end

knnsearch
语法是否仅默认k值为1?

怎样才能让

knnsearch
中的k值为5? 当我尝试将其更改为

k = 5;
result = knnsearch(training,Z,'K',k); 

系统不显示分类结果。

matlab machine-learning knn
1个回答
0
投票

当您将

k
增加到 5 或任何大于 1 的值时,输出
result
将是一个包含
k
最近邻居索引的向量,而不是单个索引。这就是为什么当您设置
k = 5
时您的代码无法按预期工作。

我强烈建议使用调试器,这里有一个帮助中心页面和一个MATLAB视频可以帮助您入门。

© www.soinside.com 2019 - 2024. All rights reserved.