필터 지우기
필터 지우기

knnclassify to fitcknn conversion

조회 수: 4 (최근 30일)
Dafni Kalatzi-Pantera
Dafni Kalatzi-Pantera 2022년 6월 6일
답변: Ishu 2023년 9월 8일
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!

Community Treasure Hunt

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

Start Hunting!

Translated by