how to using fitcknn instead of knnclassify?

5 views (last 30 days)
Bayu Ramadhan
Bayu Ramadhan on 25 Jul 2021
Answered: Ganesh on 14 Jun 2024
can someone help me to solve this

Answers (1)

Ganesh
Ganesh on 14 Jun 2024
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!

Categories

Find more on Time Series in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!