0

my homework is to make a code in Matlab to calculate the accuracy of the knn classifier if my data as the following Training data Data length: 6 seconds, 3 channels, 768 samples / trial, 140 tests, fs = 128 Hz Test data: 3 channels, 1152 samples / trial, 140 experiments.
I have written part of the code, but then I do not know where to use the cross-validation and the accuracy was very low 65%..

clear all
close all
clc

load('LabelTest.mat');
load('LabelTrain.mat');
load('TestData.mat');
load('TrainData.mat');

LabelTest=LabelTest;
LabelTrain=LabelTrain;
TestData=TestData;
TrainData=TrainData;

%Extracting feature from the training set
ndx1=find(LabelTrain==1);
ndx2=find(LabelTrain==2);
TrainClass1=TrainData(:,:,ndx1);
TrainClass2=TrainData(:,:,ndx2);

K1=1;
K2=2;
for i=1:size(TrainClass1,3)
    FVclass1(i,:)=[kurtosis(TrainClass1(:,K1,i)) std(TrainClass1(:,K1,i)) sum(TrainClass1(:,K2,i))];
    FVclass2(i,:)=[kurtosis(TrainClass2(:,K1,i)) std(TrainClass2(:,K1,i)) sum(TrainClass2(:,K2,i))];
end

FVTrain=[FVclass1;FVclass2]; 

%Test data feature extraction 
for j=1:size(TestData,3)
    FVTest(j,:)=[kurtosis(TestData(:,K1,j)) mean(TestData(:,K1,j)) sum(TestData(:,K2,j))];
end

TR_Label=[ones(1,size(TrainClass1,3)) 2*ones(1,size(TrainClass2,3))];

for k=35:-1:1
    PredictedClass=knnclassify(FVTest,FVTrain,TR_Label,k); %classification predic
    PERF=classperf(LabelTest,PredictedClass);
    SD (k)=PERF.CorrectRate ; %Test the accuracy 
end 

figure
plot(1:35,SD);
mpaskov
  • 1,234
  • 2
  • 11
  • 21
lavender
  • 369
  • 1
  • 3
  • 9
  • `knn` is usually not used in practice for high dimensionality datasets and especially if there are no clear clusters of points. You need to try something else. – rayryeng Dec 16 '16 at 16:09
  • thank you for your comment , but the teacher asked as to do with knn so we have to do it with it – lavender Dec 17 '16 at 17:42

0 Answers0