how can I convert knnclassify to fitcknn

41 views (last 30 days)
Rachel Parise
Rachel Parise on 13 Feb 2020
Commented: Walter Roberson on 6 May 2021
I have a script from a past graduate student that needs to be updated! i dont know matlab!
How do I swtich from knnclassify to fitcknn?

Answers (1)

Raunak Gupta
Raunak Gupta on 18 Feb 2020
You can replace the bottommost line of the code that is
class = knnclassify(TestingMatrix,SCORE,group,8,'euclidean','nearest');
With the following code
Mdl = fitcknn(SCORE,group,'Distance','euclidean','NumNeighbors',8,'Standardize',1,'BreakTies','nearest');
class = predict(Mdl,TestingMatrix);
For more information or insights for the functions you may look to the following documentations.
Walter Roberson
Walter Roberson on 6 May 2021
x = readtable("");
y = readtable("");
traindata = x{:,2};
traingroup = x{:,3};
testdata = y{:,2};
testgroup = y{:,3};
Mdl = fitcknn(traindata, traingroup,'Distance','euclidean','NumNeighbors',8,'Standardize',1,'BreakTies','nearest');
hasil = predict(Mdl, testdata);
nama = "hasil KNN.xlsx";
y.hasil = hasil;
writetable(y, nama)
does_it_match = strcmp(hasil, testgroup);
correct_percent = mean(does_it_match) * 100
correct_percent = 73.7500

Sign in to comment.





Community Treasure Hunt

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

Start Hunting!