-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainSVM.m
29 lines (23 loc) · 941 Bytes
/
trainSVM.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function [SVMModel, score, acc_max, boundary] = trainSVM(datasetTrain, labelsTrain, datasetTest, labelsTest, nu, OCC)
% Train an SVM model and returns its score, maximal accuracy and
% the optimal decission boundary
if OCC
SVMModel = fitcsvm(datasetTrain, labelsTrain,'Standardize',true,'KernelFunction','gaussian',...
'KernelScale','auto', 'Nu', nu);
else
SVMModel = fitcsvm(datasetTrain, labelsTrain,'Standardize',true,'KernelFunction','gaussian',...
'KernelScale','auto');
end
[label, score] = predict(SVMModel, datasetTest);
acc = zeros(200, 1);
if size(score, 2) == 2
score = score(:, 2);
end
for i = 1:200
label(score <= (-1 + i/100)) = -1;
label(score >= (-1 + i/100)) = 1;
acc(i) = size(find(label == labelsTest), 1)/size(labelsTest, 1);
end
[acc_max, index] = max(acc);
boundary = (-1 + index/100);
end