-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
165 lines (118 loc) · 6.1 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function void = main()
clear ; close all; clc
% Load wine data
load ('wine_random.mat');
disp('Wine Data loaded!!!');
lambda = [0;0.001;0.002;0.004;0.008];
jVal_training_history = [];
jVal_test_history = [];
h_x_training_accuracy = [];
h_x_test_accuracy = [];
elapsed_time = [];
precisionAndRecalls = [];
[X_training, y_training_matrix, X_test, y_test_matrix, y_training, y_test] = initVar(wine_randomized);
disp('X_training, y_training_matrix, X_test, y_test_matrix, theta initialized!!!');
% Join Labels y_train and y_test
y = [y_training; y_test];
printf('\n\n************Regularized Logistic Regression*************\n');
disp('--------------------------TRAINING & TESTING----------------------------');
disp('Training...');
disp('Learning parameters theta...');
for j = 1:size(lambda, 1),
thetas = initThetaVar();
% ------------------Training phase------------------
% Time this
tic();
thetas = gd(thetas, X_training, y_training_matrix, lambda(j));
t1 = toc();
elapsed_time = [elapsed_time; t1];
disp([num2str(j) ') ' 'Lambda=' num2str(lambda(j)) ' Training completed... Took ' num2str(t1) 's']);
% -----------------Test phase-----------------------
% Training set
h_x_training = sigmoid(X_training * thetas);
% Test set
h_x_test = sigmoid(X_test * thetas);
% Max h_(x) for each training example
[val_train ind_train] = max(h_x_training');
% Max h_(x) for each test example
[val_train ind_test] = max(h_x_test');
% Test whether or not test data is skewed or not
[precision recall] = precisionAndRecall(ind_test, y_test);
precisionAndRecalls = [precisionAndRecalls; precision recall];
% Calculate percentage accuracy value for h_(x)
h_x_training_accuracy_percentage = (sum(y_training == ind_train') / size(y_training, 1)) * 100;
h_x_training_accuracy = [h_x_training_accuracy ;h_x_training_accuracy_percentage];
% Calculate percentage accuracy value for h_(x)
h_x_test_accuracy_percentage = (sum(y_test == ind_test') / size(y_test, 1)) * 100;
h_x_test_accuracy = [h_x_test_accuracy ;h_x_test_accuracy_percentage];
%-----------------------------------------------------
% Cost Function for training set
% Calculate Cost Function for training set and add it to jVal_training_history
[jVal_training, grad_training] = costFunction(thetas(:), X_training, y_training_matrix, lambda(j));
jVal_training_history = [jVal_training_history; jVal_training];
%------------------------------------------------------
% Cost Function for test set
% Calculate Cost Function for test set and add it to jVal_test_history
[jVal_testing, grad_testing] = costFunction(thetas(:), X_test, y_test_matrix, lambda(j));
jVal_test_history = [jVal_test_history; jVal_testing];
%-------------------------------------------------------
end;
disp('------------------------END--TRAINING & TESTING----------------------------');
% Display Values of Cost Fuction and lambda
disp('');
disp('--------------------------ANALYSIS--------------------------------------');
disp('');
disp('1) TIME TO TRAIN');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('Elpased Time | Lambda');
disp([elapsed_time lambda]);
disp('');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('2) ACCURACY TEST');
disp('For Training Set:');
disp('Accuracy of h_(x)% | Lambda Values');
disp('*****************************************************************');
disp([h_x_training_accuracy lambda]);
disp('*****************************************************************');
disp('')
disp('For Test Set:');
disp('Accuracy of h_(x)% | Lambda Values');
disp('*****************************************************************');
disp([h_x_test_accuracy lambda]);
disp('*****************************************************************');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('3) J(theta) TEST');
disp('')
disp('1) For training case');
disp('*****************************************************************');
disp('Cost Function values | Lambda values');
disp([jVal_training_history lambda]);
disp('*****************************************************************');
disp('');
disp('2) For test case');
disp('*****************************************************************');
disp('Cost Function values | Lambda values')
disp([jVal_test_history lambda]);
disp('*****************************************************************');
disp('');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('3) Plotting Learning Curve');
for j = 1:length(lambda),
printf('\n-----------%d) Learning curve for lambda=%d-----------\n', j, lambda(j));
plotLearningCurve(X_training, y_training_matrix, X_test, y_test_matrix, lambda(j));
pause;
end;
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('');
disp('');
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('3) Precision & Recall Metric');
disp('Precison & Recall - Lambda Values');
disp([precisionAndRecalls lambda]);
disp('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
disp('');
disp('------------------------END--ANALYSIS--------------------------------------');