knnclassify to fitcknn conversion

1 次查看(过去 30 天)
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

回答(1 个)

Ishu
Ishu 2023-9-8
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!

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by