-
Notifications
You must be signed in to change notification settings - Fork 4
/
approach3.m
99 lines (68 loc) · 2.11 KB
/
approach3.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%Jervis Muindi
% Biometrics
% K-nearest neighbor with K = 3.
function approach3()
% Does a test for classifying the handwritten images.
disp('reading data');
% assume, we have loaded
[train_data, train_labels ] = readDATA();
train_data = procTD(train_data);% Process the trainig data to bound image to a bounding box.
[test_data, test_labels] = loadHndDATA();
sizeTrain = size(train_data,2);
%sizeTrain = 10000;
sizeTest = size(test_data,2);
sizeTest = 200;
%Do a quick nearest neighbor test classification.
errors = 0;
for i = 1:sizeTest
fprintf('test %d - ', i);
tv = test_data{i};
tv = proc(tv); %process the test image so that it's also bound to a box of 28 x 28.
tv = tv(:);
minIdx = 0;
minDist = Inf;
arr(1) = 0;
arr(2) = 0;
arr(3) = 0;
for j = 1:sizeTrain
av = train_data{j};
av = av(:);
diff = tv - av;
dist = norm(diff,2);
if(dist < minDist)
minDist = dist;
%disp(j);
minIdx = j;
prev_first = arr(1);
prev_sec = arr(2);
arr(1) = minIdx;
arr(2) = prev_first;
arr(3) = prev_sec;
end
%disp('running');
%disp(dist);
end
num = [train_labels{arr(1)};train_labels{arr(2)} ;train_labels{arr(3)}];
indices = zeros(10,1);
for ii = 1:3
indices(num(ii)+1) = indices(num(ii)+1) + 1;
end
[x,number] = max(indices);
%error('s');
%save result;
result(i) = number - 1;
%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;
if( test_labels(i) ~= result(i) )
fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) ); % print out actual number | classified number.
%figure;
continue;
end
fprintf('Curr Accuracy: %f.\n', curr_acc );
end
end