我正在尝试使用 SVM 对步行周期进行分类。我正在使用类似于 RBF 内核的预计算内核。K(X,X') = exp(-sigma*dtw(X,X')^2)。我正在尝试一种针对多类分类的所有策略。目前,我正在使用概率来预测班级我也想使用多数票但有点卡住了,我需要考虑如何使用多数票来做出班级决定,我想测试模型上的所有示例并预测班级更多投票,这是可以接受的方法吗?我有以下代码。我
%# Walk cycles dataset
clear
clc
close all
% addpath to the libsvm toolbox
%addpath('../libsvm-3.12/matlab');
% addpath to the data
dirData = './data';
addpath(dirData);
% Load/read the seprated datasets
load(fullfile(dirData,'myTrainData.mat'));
trainData = myTrainData (:,5:104); clear data;
trainLabel = myTrainData(:,1); clear label;
load(fullfile(dirData,'myTestData.mat'));
testData = myTestData(:,5:104); clear data;
testLabel = myTestData(:,1); clear label;
% Extract important information
labelList = unique(trainLabel);
NClass = length(labelList);
[Ntrain D] = size(trainData);
[Ntest D] = size(testData);
[~,~,labels] = unique(trainLabel); %# labels: 1/2/3/4...../51
%data = zscore(meas); % Currently I am not scaling features
numInst = size(trainData,1);
numLabels = max(trainLabel);
%# split training/testing
% I have already seprated train and test Dataset
idx = randperm(numInst);
numTrain = 100;
numTest = size(testData,1);
%trainData = data(idx(1:numTrain),:); testData = data(idx(numTrain+1:end),:);
%trainLabel = labels(idx(1:numTrain)); testLabel = labels(idx(numTrain+1:end));
%# train one-against-all models
preCompKernel = @(X,Y) (distanceMat2(X,Y));
K_preCompKernel = [(1:numTrain)' , preCompKernel(trainData,trainData)];
K_preCompKernelTest = [(1:numTest)' ,preCompKernel(testData,trainData)];
k_preCompNum = K_preCompKernel(:,1);
k_preCompData = K_preCompKernel(:,2:end);
K_preCompKernelTestNum = K_preCompKernelTest(:,1);
K_preCompKernelTestData = K_preCompKernelTest (:,2:end);
kernalMat = exp((-1/sigma).* k_preCompData.^2);
kernelMatTest = exp((-1/sigma).* K_preCompKernelTestData.^2);
kernelFinalMat = [k_preCompNum k_preCompData];
kernelFinalMatTest = [K_preCompKernelTestNum K_preCompKernelTestData];
model = cell(numLabels,1);
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), kernelFinalMat, '-c 1 -t 4 -b 1');
end
%# get probability estimates of test instances using each model
prob = zeros(numTest,numLabels);
for k=1:numLabels
[~,~,p] = svmpredict(double(testLabel==k), kernelFinalMatTest, model{k}, '-b 1');
prob(:,k) = p(:,model{k}.Label==1); %# probability of class==k
end
%# predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel) %# accuracy
C = confusionmat(testLabel, pred)