knnclassify to fitcknn conversion

1 vue (au cours des 30 derniers jours)
Dafni Kalatzi-Pantera
Dafni Kalatzi-Pantera le 6 Juin 2022
Réponse apportée : Ishu le 8 Sep 2023
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

Réponses (1)

Ishu
Ishu le 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!

Catégories

En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by