-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn.m
74 lines (66 loc) · 1.8 KB
/
knn.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
start=time();
fid = fopen('train-images.idx3-ubyte', 'r', 'b');
header = fread(fid, 1, 'int32');
totaltrainimages = fread(fid, 1, 'int32');
numrows = fread(fid, 1, 'int32');
numcols = fread(fid, 1, 'int32');
trainimg=ones(numrows,numcols,totaltrainimages);
for k=1:totaltrainimages
%disp(k);
trainimg(:,:,k)= fread(fid, [numrows,numcols], 'uchar');
endfor
fclose(fid);
fid2 = fopen('train-labels.idx1-ubyte', 'r', 'b');
header1 = fread(fid2, 1, 'int32');
totaltrianlabels = fread(fid2, 1, 'int32');
trainlabels= fread(fid2, totaltrianlabels, 'uchar');
fclose(fid2);
disp('input readed');
fid = fopen('t10k-images-idx3-ubyte', 'r', 'b');
header = fread(fid, 1, 'int32');
totaltestimages = fread(fid, 1, 'int32');
numrows = fread(fid, 1, 'int32');
numcols = fread(fid, 1, 'int32');
testimg=ones(numrows,numcols,totaltestimages);
for k=1:totaltestimages
testimg(:,:,k)= fread(fid, [numrows,numcols], 'uchar');
endfor
fclose(fid);
fid2 = fopen('t10k-labels.idx1-ubyte', 'r', 'b');
header1 = fread(fid2, 1, 'int32');
totaltestlabels = fread(fid2, 1, 'int32');
testlabels= fread(fid2, totaltestlabels, 'uchar');
fclose(fid2);
accuracy=0;
for k=1:totaltestimages
disp(k);
xi=[];
for i=1:numrows
xi=[xi testimg(i,:,k)]; %xi=kth row from inage matrix,convert xi to 784*1
endfor
dist=zeros;
for j=1:totaltrainimages
yi=[];
for i=1:numrows
yi=[yi trainimg(i,:,j)]; %xi=kth row from inage matrix,convert xi to 784*1
endfor
temp=xi-yi;
temp=temp.^2;
temp1=sum(temp(:));
temp1=sqrt(temp1);
dist(j)=temp1;
endfor
[val,ind]=min(dist(:));
if testlabels(k) == trainlabels(ind)
accuracy++;
end
%if k==20
% break;
%endif
endfor
disp(time()-start);
per=accuracy/totaltestimages;
printf("Accuaracy: %f\n",per*100);
printf("Error Rate: %f\n",(1-per)*100);
printf("Precision: %f\n",per);
printf("Recall: %f\n",per);