如何提高knn分类器的准确性?

时间:2016-12-15 19:03:43

标签: matlab cross-validation knn

我的作业是在Matlab中创建一个代码来计算knn分类器的准确性,如果我的数据如下所示训练数据 数据长度:6秒,3个通道,768个样品/试验,140个测试,fs = 128 Hz测试数据:3个通道,1152个样品/试验,140个实验。
我已经写了部分代码,但后来我不知道在哪里使用交叉验证,准确度非常低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);

0 个答案:

没有答案