-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_results.m
45 lines (37 loc) · 1.01 KB
/
evaluate_results.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
% Evaluate Classification Results
%
% Syntax:
% [kappa acc acc_O acc_A] = evaluate_results(tlabs, Trlabs)
%
% Input:
% tlabs: M-by-1 vector of labels given to test data
% Trlabs: M-by-1 vector of ground truth labels
%
% Output:
% kappa: kappa coefficient
% acc: accuracy per class
% acc_O: overall accuracy
% acc_A: average accuracy
%
% Written by Ali Soltani-Farani <[email protected]>
% Copyright 2012 by Ali Soltani-Farani
function [kappa,acc,acc_O,acc_A] = evaluate_results(tlabs, Trlabs)
c = max(Trlabs) - min(Trlabs) + 1;
% make confusion matrix
CM = zeros(c,c);
for i = 1:c
for j = 1:c
CM(i,j) = sum(tlabs==i & Trlabs==j);
end
end
% Class accuracy
acc = zeros(c, 1);
for j = 1:c
acc(j) = CM(j,j)/sum(CM(:,j));
end
% Overall and average accuracy
acc_O = sum(diag(CM))/sum(sum(CM));
acc_A = mean( acc );
% Kappa coefficient of agreement
kappa = (acc_O - sum( sum(CM,1)*sum(CM,2) )/sum(sum(CM)).^2)/(1-sum( sum(CM,1)*sum(CM,2) )/sum(sum(CM)).^2);
end