-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestCBRs.m
72 lines (52 loc) · 1.86 KB
/
testCBRs.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
function [perFoldF1s] = testCBRs(metric, k)
% Load the data in for testing
[x,y] = loaddata('noisydata_students.txt');
stats = cell(0);
confusionMatrix = zeros(6,6);
perFoldF1s = zeros(10, 6);
foldIndices = cell(1,10);
totalNumber = size(x,1);
for i = 1:10
foldIndices{i} = [round(((i-1)*totalNumber/10)+1):round(i*totalNumber/10)];
end
for i = 1:10
trainExamples = x([foldIndices{1:(i-1)}, foldIndices{i+1:end}], :);
trainTargets = y([foldIndices{1:(i-1)}, foldIndices{i+1:end}]);
testExamples = x([foldIndices{i}], :);
testTargets = y([foldIndices{i}]);
% Set up the CBR
[cbr] = CBRinit(trainExamples, trainTargets);
% Simulate using the test data
[pred] = testCBR(cbr, testExamples, k, metric);
% Build the confusion matrix
thisCM = buildCM(pred, testTargets);
% Calculate the recall and precision
[thisRecall, thisPrecision] = recall_precision(thisCM);
thisF1 = f1measure(thisRecall, thisPrecision);
% Add the recall and precision for averaging later
% recall = recall + thisRecall;
% precision = precision + thisPrecision;
confusionMatrix = confusionMatrix + thisCM;
perFoldF1s(i, :) = thisF1;
end
confusionMatrix
% Averages recall and precision, and calculates the f1 measure
%recall = recall/10
%precision = precision/10
[recall, precision] = recall_precision(confusionMatrix);
recall
precision
f1 = f1measure(recall, precision)
% Store the statistics to the stats variable for saving to file
stats{1} = confusionMatrix;
stats{2} = recall;
stats{3} = precision;
stats{4} = f1;
save(strcat('tests/testCBRsMetric', num2str(metric), 'k', num2str(k)), '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