forked from jervisfm/Digit-Recognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testk.m
88 lines (61 loc) · 1.78 KB
/
testk.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
74
75
76
77
78
79
80
81
82
83
84
function testk(k)
%K-nearest neighbor classifier
% Call with values of k to test. Defaults to test size of 2000 for speed reasons. Can be edited to run with size 10,000.
% 96 accuracy for test data. of size 2000.
disp('reading data');
% assume, we have loaded
[train_data, train_labels, test_data, test_labels] = readDATA();
sizeTrain = size(train_data,2);
sizeTest = size(test_data,2);
sizeTest = 2000;
%Do a quick nearest neighbor test classification.
errors = 0;
%fprintf('size of train - %d',sizeTrain);
for i = 1:sizeTest
fprintf('test %d - ', i);
tv = test_data{i};
tv = tv(:);
minIdxVec = zeros(1,k);
minDistVec = zeros(1,k) + Inf;
% minIdx = 0;
% minDist = Inf;
for j = 1:sizeTrain
av = train_data{j};
av = av(:);
diff = tv - av;
dist = norm(diff,2);
% get the nearest neighbor that is furthest away and its index
[minDist, minDistIdx] = max(minDistVec);
if(dist < minDist)
minDistVec(minDistIdx) = dist;
%disp(j);
minIdxVec(minDistIdx) = j;
end
%disp('running');
%disp(dist);
end
%error('s');
%save result;
minIdx = mode(minIdxVec);
result(i) = train_labels{minIdx};
%do accuracy checking in line
if( test_labels{i} ~= result(i) )
errors = errors + 1;
end
tot = i;
curr_acc = (tot - errors) / tot;
fprintf('Curr Accuracy: %f.\n', curr_acc);
end
%{
%compute accuracy
total = sizeTest;
errors = 0;
for ii = 1:sizeTest
if( test_labels{i} ~= result(ii) )
errors = errors + 1;
end
end
%output accuracy.
acc = (total - errors) / total;
fprintf('Accuracy : %f', acc);
%}