how can I convert knnclassify to fitcknn

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)

Hi,
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.

7 Comments

Life saver! thank you so much!
@Raunak Gupta i have an error too sir about knn. Could you help me pleae?
This is the code :
x=readtable("datatraining.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
y=readtable("datatesting.xlsx");
sampel = y;
test = [sampel(:,1) sampel(:,2)];
%sampel = [2.6136 0.1284 1.3017 -0.8089 0.0441 -0,2084];
hasil=knnclassify(test,latih,group);
Mdl=fitcknn(group,'Distance','euclidean','NumNeighbors',8,'Standardize',1,'BreakTies','nearest');
hasil = predict(Mdl,latih);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);
the error i got :
Error using knnclassify (line 79)
'knnclassify' has been removed. Use 'fitcknn' to fit
a KNN classification model and classify data using
'ClassificationKNN.predict'.
Error in KNN2 (line 10)
hasil=knnclassify(test,latih,group);
Remove the line
hasil=knnclassify(test,latih,group);
I've removed it, sir, and here are the results. and i got an error too sir
Please show the complete error message.
I checked, and the options you are using were all supported in your release, so I need the rest of the error.
Ok sir, this is the complete error messege.
Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in
the VariableDescriptions property.
Warning: Variable names were modified to make them valid MATLAB identifiers. The original names are saved in
the VariableDescriptions property.
Error using classreg.learning.internal.table2FitMatrix (line 83)
A table used as a response must contain exactly one variable.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 471)
[X,Y,vrange,wastable,varargin] = classreg.learning.internal.table2FitMatrix(X,Y,varargin{:});
Error in ClassificationKNN.prepareData (line 878)
prepareData@classreg.learning.classif.FullClassificationModel(X,Y,varargin{:},'OrdinalIsCategorical',true);
Error in classreg.learning.FitTemplate/fit (line 213)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in ClassificationKNN.fit (line 863)
this = fit(temp,X,Y);
Error in fitcknn (line 261)
this = ClassificationKNN.fit(X,Y,RemainingArgs{:});
Error in KNNPleaseFix (line 11)
Mdl = fitcknn(test,latih,'NumNeighbors',5,'Standardize',1);
x = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/600525/datatraining.xlsx");
y = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/600520/datatesting.xlsx");
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.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2018a

Tags

Asked:

on 13 Feb 2020

Commented:

on 6 May 2021

Community Treasure Hunt

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

Start Hunting!