convert knnclassify to fitcknn

please help me to convert knnclassify to fitcknn
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);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);

7 Comments

Do not read files inside a loop -- not unless you are reading a different file each time.
The body of your loop does not use i, so you are doing the same thing each iteration.
Can you give the example sir in my code?
x=readtable("datatraining.xlsx");
y=readtable("datatesting.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
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);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);
except rewritten to use fitcknn as discussed in your other postings.
I've tried sir, but still get an error
i have tried use fitcknn but still have error sir

Sign in to comment.

 Accepted Answer

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

9 Comments

it still got an error sir
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");
will it be the same if I replace it with
x = readtable("datatraining.xlsx")
y = readtable("datatesting.xlsx")
x = readtable("datatraining.xlsx", 'readvariablenames', false);
y = readtable("datatesting.xlsx", 'readvariablenames', false);
Still have an error sir.
Error using classreg.learning.FullClassificationRegressionModel.prepareDataCR (line 192)
X must be a numeric matrix.
Error in classreg.learning.classif.FullClassificationModel.prepareData (line 487)
classreg.learning.FullClassificationRegressionModel.prepareDataCR(...
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 7)
Mdl = fitcknn(traindata,
traingroup,'Distance','euclidean','NumNeighbors',8,'Standardize',1,'BreakTies','nearest');
filename = "datatraining.xlsx";
opt = detectImportOptions(filename);
x = readtable(filename, opt);
filename = "datatesting.xlsx";
opt = detectImportOptions(filename);
y = readtable(filename, opt);
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
I tested this in you release to be sure it would work.
Thankyou so much sir, its works !!
Once again thankyou so much sir
There are several lessons here:
  1. You had testing and training reversed
  2. Predict all the data at one time instead of one at a time
  3. You were working with all of the y data in every iteration of the loop, instead of extracting one y value
  4. Do not re-read the data inside a loop if you can avoid doing so!
  5. you were extracting the wrong data to use in the fitting
  6. Use meaningful variable names
  7. use writetable() instead of xlswrite()
@Walter Roberson sir can you help me with a problem it is also same,the question link is given below
kindly check this and if you can please give a solution

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!