how can I convert knnclassify to fitcknn

조회 수: 2 (최근 30일)
Rachel Parise
Rachel Parise 2020년 2월 13일
댓글: Walter Roberson 2021년 5월 6일
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?

답변 (1개)

Raunak Gupta
Raunak Gupta 2020년 2월 18일
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
Frisda Sianipar
Frisda Sianipar 2021년 5월 6일
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 2021년 5월 6일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Design of Experiments (DOE)에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by