-
Notifications
You must be signed in to change notification settings - Fork 0
/
Measure_AUC.m
63 lines (54 loc) · 1.63 KB
/
Measure_AUC.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
function AccumAuc = Measure_AUC(Scores, Labels)
% Area Under Curve for Amonaly
%
% Scores: predicted scores;
% Labels: groundtruth labels, PosLabel = 1& NegLabel = 0;
NumInst = length(Scores);
% sort Scores and Labels
[Scores, index] = sort(Scores, 'descend');
Labels = Labels(index);
PosLabel = 1;
NegLabel = 0;
NumPos = length(find(Labels == PosLabel));
NumNeg = length(find(Labels == NegLabel));
AccumPos = 0;
AccumNeg = 0;
AccumAuc = 0;
UnitPos = 1 / NumPos;
UnitNeg = 1 / NumNeg;
i = 1;
while i <= NumInst
temp = AccumPos;
if (i < NumInst - 1) && (Scores(i) == Scores(i + 1))
while (i < NumInst - 1) && (Scores(i) == Scores(i + 1))
if Labels(i) == NegLabel
AccumNeg = AccumNeg + 1;
elseif Labels(i) == PosLabel
AccumPos = AccumPos + 1;
else
disp('Label is not defined!');
end
i = i + 1;
end
if Labels(i) == NegLabel
AccumNeg = AccumNeg + 1;
elseif Labels(i) == PosLabel
AccumPos = AccumPos + 1;
else
disp('Label is not defined!');
end
AccumAuc = AccumAuc + (AccumPos + temp) * UnitPos * AccumNeg * UnitNeg / 2;
AccumNeg = 0;
else
if Labels(i) == NegLabel
AccumNeg = AccumNeg + 1;
AccumAuc = AccumAuc + AccumPos * UnitPos * AccumNeg * UnitNeg;
AccumNeg = 0;
elseif Labels(i) == PosLabel
AccumPos = AccumPos + 1;
else
disp('Label is not defined');
end
end
i = i + 1;
end