how can I convert knnclassify to fitcknn

14 views (last 30 days)
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
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
Frisda Sianipar
Frisda Sianipar on 6 May 2021
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);
Walter Roberson
Walter Roberson on 6 May 2021
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.

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!