-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatsOfMeasure.m
66 lines (56 loc) · 2.47 KB
/
statsOfMeasure.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
function [stats] = statsOfMeasure(confusion, verbatim)
% The input 'confusion' is the the output of the Matlab function
% 'confusionmat'
% if 'verbatim' = 1; output the generated table in the command window
% confusion: 3x3 confusion matrix
tp = [];
fp = [];
fn = [];
tn = [];
len = size(confusion, 1);
for k = 1:len % predict
% True positives % | x o o |
tp_value = confusion(k,k); % | o o o | true
tp = [tp, tp_value]; % | o o o |
% predict
% False positives % | o o o |
fp_value = sum(confusion(:,k)) - tp_value; % | x o o | true
fp = [fp, fp_value]; % | x o o |
% predict
% False negatives % | o x x |
fn_value = sum(confusion(k,:)) - tp_value; % | o o o | true
fn = [fn, fn_value]; % | o o o |
% predict
% True negatives (all the rest) % | o o o |
tn_value = sum(sum(confusion)) - (tp_value + fp_value + fn_value); % | o x x | true
tn = [tn, tn_value]; % | o x x |
end
% Statistics of interest for confusion matrix
prec = tp ./ (tp + fp); % precision
sens = tp ./ (tp + fn); % sensitivity, recall
spec = tn ./ (tn + fp); % specificity
acc = sum(tp) ./ sum(sum(confusion));
f1 = (2 .* prec .* sens) ./ (prec + sens);
% For micro-average
microprec = sum(tp) ./ (sum(tp) + sum(fp)); % precision
microsens = sum(tp) ./ (sum(tp) + sum(fn)); % sensitivity, recall
microspec = sum(tn) ./ (sum(tn) + sum(fp)); % specificity
microacc = acc;
microf1 = (2 .* microprec .* microsens) ./ (microprec + microsens);
% Names of the rows
name = ["true_positive"; "false_positive"; "false_negative"; "true_negative"; ...
"precision"; "sensitivity"; "specificity"; "accuracy"; "F-measure"];
% Names of the columns
varNames = ["name"; "classes"; "macroAVG"; "microAVG"];
% Values of the columns for each class
values = [tp; fp; fn; tn; prec; sens; spec; repmat(acc, 1, len); f1];
% Macro-average
macroAVG = mean(values, 2);
% Micro-average
microAVG = [macroAVG(1:4); microprec; microsens; microspec; microacc; microf1];
% OUTPUT: final table
stats = table(name, values, macroAVG, microAVG, 'VariableNames',varNames);
if verbatim
stats
end
end