-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfusion_matrix.m
executable file
·50 lines (41 loc) · 1.16 KB
/
confusion_matrix.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
function matrix=confusion_matrix(Y_real, Y_pred)
# Number of classes and data
num_classes = columns(Y_real);
data_amount = rows(Y_real);
# Initialize confusion matrix
matrix = zeros(num_classes,num_classes);
# Fill matrix
for(i=[1:data_amount])
[_, irow] = max(Y_real(i,:));
[_, icol] = max(Y_pred(i,:));
matrix(irow, icol) += 1;
endfor
# Show the matrix
disp("Confusion matrix:");
disp(matrix);
# Initialize precision, recall, and f1score
precision = zeros(num_classes,1);
recall = zeros(num_classes,1);
f1score = zeros(num_classes,1);
# Calculate values class-wise
for(i=[1:num_classes])
sumrow = sum(matrix(i,:));
sumcol = sum(matrix(:,i));
if(sumrow != 0 )
recall(i) = matrix(i,i)/sumrow;
endif
if(sumcol != 0)
precision(i) = matrix(i,i)/sumcol;
endif
if((precision(i) + recall(i)) != 0)
f1score(i) = (2 * (precision(i) * recall(i) / (precision(i) + recall(i))));
endif
endfor
# Show the resulting values
disp("Precision");
disp(precision);
disp("Recall");
disp(recall);
disp("F1Score");
disp(f1score);
endfunction