-
Notifications
You must be signed in to change notification settings - Fork 1
/
zipcodeNearestN.m
40 lines (34 loc) · 1.03 KB
/
zipcodeNearestN.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
% ---------------------------------------------
%
% Simple classification of hand written digits
% using a nearest neighbour classifier
%
% Johan Dahlin (johan.dahlin@isy.liu.se)
% 2013-03-19
%
% ---------------------------------------------
clear all;
% Import the training and test data
% The Zip code data is available from:
% http://www-stat.stanford.edu/~tibs/ElemStatLearn/
zipTraining = importdata('zip.train');
zipTesting = importdata('zip.test');
% Find the classes and the data
classTraining=zipTraining(:,1);
classTesting=zipTesting(:,1);
dataTraining=zipTraining(:,2:end);
dataTesting=zipTesting(:,2:end);
% Classify the test data
correct=0;
for ii=1:length(classTesting)
for jj=1:length(classTraining)
featurenorm(ii,jj)=sum((dataTesting(ii,:)-dataTraining(jj,:)).^2);
end
[~,NN]=min(featurenorm(ii,:));
classified(ii)=classTraining(NN);
correct=correct+(classified(ii)==classTesting(ii));
disp(ii)
end
% Estimate the classification error
1-correct/length(classTesting)
% 5.63% mis-classication rate