Borrar filtros
Borrar filtros

knnclassify to fitcknn conversion

4 visualizaciones (últimos 30 días)
Dafni Kalatzi-Pantera
Dafni Kalatzi-Pantera el 6 de Jun. de 2022
Respondida: Ishu el 8 de Sept. de 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

Respuestas (1)

Ishu
Ishu el 8 de Sept. de 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!

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by