-
Notifications
You must be signed in to change notification settings - Fork 1
/
zipcodeLS.m
49 lines (39 loc) · 1.22 KB
/
zipcodeLS.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
% ---------------------------------------------
%
% Simple classification of hand written digits
% using a least squares classifier
%
% Johan Dahlin ([email protected])
% 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);
%Train the classifier
% Construct the vector of 1-of-K coding scheme
classTrainingCoding=zeros(length(classTraining),10);
for ii=1:length(classTraining)
classTrainingCoding(ii,classTraining(ii) 1)=1;
end
% Estimate the parameters
beta=classTrainingCoding\dataTraining;
%Classify the test data
correct=0;
for ii=1:length(classTesting)
feature(ii,:)=beta*dataTesting(ii,:)';
[~,classified(ii)]=max(feature(ii,:));
classified(ii)=classified(ii)-1;
correct=correct (classified(ii)==classTesting(ii));
end
% Estimate the classification error
1-correct/length(classTesting)
% 38.22% mis-classification rate