-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation_function.m
96 lines (90 loc) · 2.34 KB
/
validation_function.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function ret = validation_function(dec, labels);
labels = (labels >= 0) - (labels < 0);
valid_function = @(dec, labels) accuracy(dec, labels);
ret = valid_function(dec, labels);
%precision(dec, labels);
%recall(dec, labels);
%fscore(dec, labels);
%bac(dec, labels);
%auc(dec, labels);
%accuracy(dec, labels);
function ret = precision(dec, label)
tp = sum(label == 1 & dec >= 0);
tp_fp = sum(dec >= 0);
if tp_fp == 0;
disp(sprintf('warning: No positive predict label.'));
ret = 0;
else
ret = tp / tp_fp;
end
disp(sprintf('Precision = %g%% (%d/%d)', 100.0 * ret, tp, tp_fp));
function ret = recall(dec, label)
tp = sum(label == 1 & dec >= 0);
tp_fn = sum(label == 1);
if tp_fn == 0;
disp(sprintf('warning: No postive true label.'));
ret = 0;
else
ret = tp / tp_fn;
end
disp(sprintf('Recall = %g%% (%d/%d)', 100.0 * ret, tp, tp_fn));
function ret = fscore(dec, label)
tp = sum(label == 1 & dec >= 0);
tp_fp = sum(dec >= 0);
tp_fn = sum(label == 1);
if tp_fp == 0;
disp(sprintf('warning: No positive predict label.'));
precision = 0;
else
precision = tp / tp_fp;
end
if tp_fn == 0;
disp(sprintf('warning: No postive true label.'));
recall = 0;
else
recall = tp / tp_fn;
end
if precision + recall == 0;
disp(sprintf('warning: precision + recall = 0.'));
ret = 0;
else
ret = 2 * precision * recall / (precision + recall);
end
disp(sprintf('F-score = %g', ret));
function ret = bac(dec, label)
tp = sum(label == 1 & dec >= 0);
tn = sum(label == -1 & dec < 0);
tp_fn = sum(label == 1);
tn_fp = sum(label == -1);
if tp_fn == 0;
disp(sprintf('warning: No positive true label.'));
sensitivity = 0;
else
sensitivity = tp / tp_fn;
end
if tn_fp == 0;
disp(sprintf('warning: No negative true label.'));
specificity = 0;
else
specificity = tn / tn_fp;
end
ret = (sensitivity + specificity) / 2;
disp(sprintf('BAC = %g', ret));
function ret = auc(dec, label)
[dec idx] = sort(dec, 'descend');
label = label(idx);
tp = cumsum(label == 1);
fp = sum(label == -1);
ret = sum(tp(label == -1));
if tp == 0 | fp == 0;
disp(sprintf('warning: Too few postive true labels or negative true labels'));
ret = 0;
else
ret = ret / tp(end) / fp;
end
disp(sprintf('AUC = %g', ret));
function ret = accuracy(dec, label)
correct = sum(dec .* label >= 0);
total = length(dec);
ret = correct / total;
disp(sprintf('Accuracy = %g%% (%d/%d)', 100.0 * ret, correct, total));