-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestCBRsNoisy.m
73 lines (53 loc) · 1.85 KB
/
testCBRsNoisy.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
function [] = testCBRsNoisy(metric, k)
% Load the data in for testing
[x,y] = loaddata('cleandata_students.txt');
% Set up variables to store the statistics for the larger NN
stats = cell(0);
% Set up the CBR
[cbr] = CBRinit(x, y);
[tx,ty] = loaddata('noisydata_students.txt');
% Simulate using the test data
[pred] = testCBR(cbr, tx, k, metric);
% Build the confusion matrix
confusionMatrix = buildCM(pred, ty)
% Averages recall and precision, and calculates the f1 measure
[recall, precision] = recall_precision(confusionMatrix);
recall
precision
f1 = f1measure(recall, precision)
avgf1 = sum(f1)/6
% Store the statistics to the stats variable for saving to file
stats{1} = confusionMatrix;
stats{2} = recall;
stats{3} = precision;
stats{4} = f1;
save('tests/testCBRsNoisy', 'stats');
end
function [CM] = buildCM(predictions, testTargets)
CM = zeros(6,6);
for i = 1:length(predictions)
CM(predictions(i), testTargets(i)) = CM(predictions(i), testTargets(i)) + 1;
end
end
function[recall, precision] = recall_precision(confMat)
% Calculates the recall and precision for the confusion matrix
% for *one* fold
recall = zeros(1,6);
precision = zeros(1,6);
for i = 1:size(confMat, 2)
truePositives = confMat(i, i);
falseNegatives = sum(confMat(i, :)) - truePositives;
falsePositives = sum(confMat(:, i)) - truePositives;
recall(i) = (truePositives + eps)/ (truePositives + falsePositives + eps);
precision(i) = (truePositives + eps) / (truePositives + falseNegatives ...
+ eps);
end
end
function[f1] = f1measure(recall, precision)
% Calculates the f1 measure using the recall and precision for all
% folds
fl = [];
for i = 1:6
f1(i) = 2 * (recall(i) * precision(i))/ (recall(i) + precision(i));
end
end