how can I convert knnclassify to fitcknn

6 ビュー (過去 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 Center および File ExchangeDesign 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