-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lr.m
99 lines (90 loc) · 2.14 KB
/
test_lr.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
% Logistic Regression validation and test code
% Author: Xiujiao Gao
% Parameters
W = load('w_lr.txt');
% Validation
% X, feature matrix
X = load(strcat('train0.txt'));
[rows,columns] = size(X);
% get train data , the left will be used as validation data
X(1:ceil(rows*0.95),:)=[];
[rows,columns] = size(X);
xones = ones(rows,1);
X = [xones X];
% T, lable for each x feature,is 1 of 10 vector
T = zeros(rows,10);
T(:,1) = 1;
for i=1:9
x = load(strcat('train',num2str(i),'.txt'));
[rows,columns] = size(x);
% get training data
x(1:ceil(rows*0.95),:)=[];
[rows,columns] = size(x);
xones = ones(rows,1);
X = [X; xones x];
t = zeros(rows,10);
t(:,i+1) = 1;
T = [T;t];
end
A = exp(X*W);
[rows,columns] = size(A);
% Get first Y
rowsum = sum(A,2);
temp = zeros(rows,columns);
for i = 1:columns
temp(:,i) = rowsum;
end
Y = A./temp;
% get max value from Y for each row and the corresponding column number
[y,n] = max(Y');
Y_Lable = zeros(rows,columns);
for i= 1:rows
Y_Lable(i,n(i)) = 1;
end
% get error rate
E = xor(Y_Lable,T);
validation_err = (sum(sum(E))/2)/rows
% Test
% X, feature matrix
X = load(strcat('test0.txt'));
[rows,columns] = size(X);
% get test data
xones = ones(rows,1);
X = [xones X];
% T, lable for each x feature,is 1 of 10 vector
T = zeros(rows,10);
T(:,1) = 1;
for i=1:9
x = load(strcat('test',num2str(i),'.txt'));
[rows,columns] = size(x);
xones = ones(rows,1);
X = [X; xones x];
t = zeros(rows,10);
t(:,i+1) = 1;
T = [T;t];
end
A = exp(X*W);
[rows,columns] = size(A);
% Get first Y
rowsum = sum(A,2);
temp = zeros(rows,columns);
for i = 1:columns
temp(:,i) = rowsum;
end
Y = A./temp;
% get max value from Y for each row and the corresponding column number
[y,n] = max(Y');
Y_Lable = zeros(rows,columns);
for i= 1:rows
Y_Lable(i,n(i)) = 1;
end
% get error rate
E = xor(Y_Lable,T);
test_err = (sum(sum(E))/2)/rows
% fid=fopen('classes_lr.txt','w');
% for i=1:rows
% for j =1:columns
% fprintf(fid,'%d \t',Y_Lable(i,j));
% end
% fprintf(fid,'\n');
% end