-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameterTuning.m
47 lines (41 loc) · 1.39 KB
/
parameterTuning.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function accuracy = parameterTuning( kernel_function, sigma, c )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
net = svm(size(normal_features, 2), kernel_function, [sigma], c);
pos_class = ones(size(normal_train_sunset, 1), 1);
neg_class = ones(size(normal_train_nonsunset, 1), 1);
neg_class(:) = -1;
yTrain = vertcat(pos_class, neg_class);
xTrain = vertcat(normal_train_sunset, normal_train_nonsunset);
net = svmtrain(net, xTrain, yTrain);
falsePos = 0;
falseNeg = 0;
truePos = 0;
trueNeg = 0;
xTest = vertcat(normal_test_sunset, normal_test_nonsunset);
[detectedClasses, distances] = svmfwd(net, xTest);
pos_test_class = ones(size(normal_test_sunset, 1), 1);
neg_test_class = ones(size(normal_test_nonsunset, 1), 1);
neg_test_class(:) = -1;
yTest = vertcat(pos_test_class, neg_test_class);
for i = 1:length(yTest)
% fprintf('Point %d, True class: %d, detected class: %d\n', i, yTest(i), detectedClasses(i))
if (yTest(i) == 1)
if (detectedClasses(i) == 1)
truePos = truePos + 1;
else
falseNeg = falseNeg + 1;
end
else
if (detectedClasses(i) == 1)
falsePos = falsePos + 1;
else
trueNeg = trueNeg + 1;
end
end
end
accuracy = (trueNeg + truePos)/length(yTest);
tpr = truePos/(truePos+falseNeg);
precision = truePos/(truePos+falsePos);
fpr = falsePos/(falsePos+trueNeg);
end