how to using fitcknn instead of knnclassify?
5 次查看(过去 30 天)
显示 更早的评论
回答(1 个)
Ganesh
2024-6-14
To use the "fitcknn" in place of "knnclassify" you would have to follow the same procudure as using "knnclassify()", except, you are now training a "model" instead of directly predicting the results. The added advantage to this is that, your model can now be used to predict on unknown data sets too. As a final step after training the model, you have to use the "predict()" function to predict the values on your test dataset. The following code has been modified to accomodate "fitcknn" in place of "knnclassify".
data{1,3} = num2str(RangeR); data{2,3} = num2str(RangeG); data{3,3} = num2str(RangeB); data{4,3} = num2str(RangeH); data{5,3} = num2str(RangeS); data{6,3} = num2str(RangeI);
set(handles.uitable2, 'Data', data, 'ForegroundColor', [0 0 0]);
training1 = xlsread('Data Training');
group = training1(:, 25);
training = training1(:, 1:9);
Z = [MeanR MeanG MeanB MeanH MeanS MeanI VarRed VarGreen VarBlue VarH VarS VarI RangeR RangeG RangeB RangeH RangeS RangeI];
Mdl = fitcknn(training, group, 'NumNeighbors', 3); % example using 3 neighbors, adjust as needed
hasil1 = predict(Mdl, Z);
if hasil1 == 1
x = 'MATURE';
elseif hasil1 == 2
x = 'HALF-MATURE';
elseif hasil1 == 3
x = 'IMMATURE';
end
set(handles.edit2, 'string', x);
You may refer to the documentation below for more information on the mentioned functions:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!