knnclassify to fitcknn conversion
1 view (last 30 days)
Show older comments
how to i convert this line of code to fitcknn?
knnclassify(test (:,1:27), train (:,1:27), train (:,27+1),k);
test and train are xls files
k = 3
0 Comments
Answers (1)
Ishu
on 8 Sep 2023
Hi Dafni,
As you want to train your model for "k-nearest neighbor classsifier" using 'fircknn()' you can follow these steps:
First you have to load your data from the Excel files using 'xlsread'.
trainData = xlsread('train.xls');
testData = xlsread('test.xls');
Next you can sepearte the features according to the line of code you have provided.
train = trainData(:, 1:27);
trainLabels = trainData(:, 28);
test = testData(:, 1:27);
Now you can train the k-nearest neighbours classifier using 'fitcknn()' with specified value of 'k'. In your case value of 'k' is 3.
knnClassifier = fitcknn(train, trainLabels, 'NumNeighbors', k);
Further you can use 'predict()' to predict for the 'test'.
predictedLabels = predict(knnClassifier, test);
For more information on 'fitcknn()', you can refer this documentation:
Hope it helps!
0 Comments
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!