-
Notifications
You must be signed in to change notification settings - Fork 0
/
aa_single.m
341 lines (251 loc) · 10 KB
/
aa_single.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
clc
close all
clear all
format long
% =========================================================================
% SINGLE CLASSIFIER
% =========================================================================
runs = 20;
results = zeros(4*5,5);
% Because of the nature of the datasets, we need to do a bit of data
% formatting before applying the model. Specifically, some variables must
% be turned into categorical. Information about the variables are available
% with the downloaded dataset.
% -------------------------------------------------------------------------
% Australian
% -------------------------------------------------------------------------
australian = importdata('data/australian/australian.dat');
A1_encoding = double(categorical(australian(:,1)));
A4 = categorical(australian(:,4));
A4_encoding = onehotencode(A4,2);
A5 = categorical(australian(:,5));
A5_encoding = onehotencode(A5,2);
A6 = categorical(australian(:,6));
A6_encoding = onehotencode(A6,2);
A8_encoding = double(categorical(australian(:,8)));
A9_encoding = double(categorical(australian(:,9)));
A11_encoding = double(categorical(australian(:,11)));
A12 = categorical(australian(:,12));
A12_encoding = onehotencode(A12,2);
TARGET = double(categorical(australian(:,15)));
% The following code structures the final dataset by normalizing the
% features containing continuous values.
australian_df = horzcat(A1_encoding(:,1), ...
normalize(australian(:,2)),...
normalize(australian(:,3)),...
A4_encoding(:,1:3),...
A5_encoding(:,1:14),...data
A6_encoding(:,1:8),...
normalize(australian(:,7)),...
A8_encoding(:,1),...
A9_encoding(:,1),...
normalize(australian(:,10)),...
A11_encoding(:,1),...
A12_encoding(:,1:3),...
normalize(australian(:,13)),...
normalize(australian(:,14)),...
TARGET(:,1));
% Separate in train and test sets
cv = cvpartition(size(australian_df,1),'HoldOut',0.3);
idx = cv.test;
dataTrain = australian_df(~idx,:);
dataTest = australian_df(idx,:);
X_train = dataTrain(:, 1:end-1);
Y_train = dataTrain(:, end);
X_test = dataTest(:, 1:end-1);
Y_test = dataTest(:, end);
epochs = [50 100 200 300];
hidden_nodes = [8 12 16 24 32];
for e = 1:length(epochs)
epoch = epochs(e);
for h = 1:length(hidden_nodes)
node = hidden_nodes(h);
partial_results = zeros(runs,1);
for j = 1:runs
net = fitcnet(X_train, Y_train,...
'LayerSizes', node,...
'Activations','sigmoid',...
'IterationLimit',epoch,...
'LayerBiasesInitializer','ones');
testAccuracy = 1 - loss(net,X_test,Y_test, "LossFun","classiferror");
partial_results(j) = testAccuracy;
end
% Process to save progressively data in a separated table
base = (e - 1) * 5;
results(base + h, 1) = epoch;
results(base + h, 2) = node;
results(base + h, 3) = max(partial_results);
end
end
% Clear unused variables
clear A11_encoding A12 A12_encoding A1_encoding A4 A4_encoding A5...
A5_encoding A6 A6_encoding A8_encoding A9_encoding TARGET base e...
epoch h j node num_observations num_samples partial_results...
splitting test testAccuracy train X_test X_train Y_test Y_train...
australian net dataTest dataTrain cv idx
% -------------------------------------------------------------------------
% German
%--------------------------------------------------------------------------
german = importdata('data/german/german.dat');
A1 = categorical(german.textdata(:,1));
A1_encoding = onehotencode(A1,2);
A3 = categorical(german.textdata(:,3));
A3_encoding = onehotencode(A3,2);
A4 = categorical(german.textdata(:,4));
A4_encoding = onehotencode(A4,2);
A6 = categorical(german.textdata(:,6));
A6_encoding = onehotencode(A6,2);
A7 = categorical(german.textdata(:,7));
A7_encoding = onehotencode(A7,2);
A9 = categorical(german.textdata(:,9));
A9_encoding = onehotencode(A9,2);
A10 = categorical(german.textdata(:,10));
A10_encoding = onehotencode(A10,2);
A12 = categorical(german.textdata(:,12));
A12_encoding = onehotencode(A12,2);
A14 = categorical(german.textdata(:,14));
A14_encoding = onehotencode(A14,2);
A15 = categorical(german.textdata(:,15));
A15_encoding = onehotencode(A15,2);
A17 = categorical(german.textdata(:,17));
A17_encoding = onehotencode(A17,2);
A19_encoding = double(categorical(german.textdata(:,19)));
A20_encoding = double(categorical(german.textdata(:,20)));
TARGET = double(categorical(german.data(:,1)));
german_df = horzcat(A1_encoding(:,1:4),...
normalize(str2double(german.textdata(:,2))),...
A3_encoding(:,1:5),...
A4_encoding(:,1:10),...
normalize(str2double(german.textdata(:,5))),...
A6_encoding(:,1:5),...
A7_encoding(:,1:5),...
normalize(str2double(german.textdata(:,8))),...
A9_encoding(:,1:4),...
A10_encoding(:,1:3),...
normalize(str2double(german.textdata(:,11))),...
A12_encoding(:,1:4),...
normalize(str2double(german.textdata(:,13))),...
A14_encoding(:,1:3),...
A15_encoding(:,1:3),...
normalize(str2double(german.textdata(:,16))),...
A17_encoding(:,1:4),...
normalize(str2double(german.textdata(:,18))),...
A19_encoding(:,1),...
A20_encoding(:,1),...
TARGET(:,1));
% Separate train and test
cv = cvpartition(size(german_df,1),'HoldOut',0.3);
idx = cv.test;
dataTrain = german_df(~idx,:);
dataTest = german_df(idx,:);
X_train = dataTrain(:, 1:end-1);
Y_train = dataTrain(:, end);
X_test = dataTest(:, 1:end-1);
Y_test = dataTest(:, end);
for e = 1:length(epochs)
epoch = epochs(e);
for h = 1:length(hidden_nodes)
node = hidden_nodes(h);
partial_results = zeros(runs,1);
for j = 1:runs
net = fitcnet(X_train, Y_train,...
'LayerSizes', node,...
'Activations','sigmoid',...
'IterationLimit',epoch,...
'LayerBiasesInitializer','ones');
testAccuracy = 1 - loss(net,X_test,Y_test, "LossFun","classiferror");
partial_results(j) = testAccuracy;
end
% Process to save progressively data in a separated table
base = (e - 1) * 5;
results(base + h, 4) = max(partial_results);
end
end
clear idx A1 A10 A10_encoding A12 A12_encoding A14 A14_encoding A15...
A15_encoding A17 A17_encoding A19_encoding A1_encoding A20_encoding...
A3 A3_encoding A4 A4_encoding A6 A6_encoding A7 A7_encoding A9...
A9_encoding base cv dataTest dataTrain e epoch german h j net node...
partial_results TARGET testAccuracy X_test X_train Y_test Y_train
% -------------------------------------------------------------------------
% Japanese
% -------------------------------------------------------------------------
% Hands importing to convert empty values into NaNs
opts = delimitedTextImportOptions("NumVariables", 16);
opts.DataLines = [1, Inf];
opts.Delimiter = " ";
opts.VariableNames = ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "TARGET"];
opts.VariableTypes = ["categorical", "double", "double", "categorical", "categorical", "categorical", "categorical", "double", "categorical", "categorical", "double", "categorical", "categorical", "double", "double", "categorical"];
opts.ImportErrorRule = "omitrow";
opts.MissingRule = "omitrow";
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
opts = setvaropts(opts, ["A1", "A4", "A5", "A6", "A7", "A9", "A10", "A12", "A13", "TARGET"], "EmptyFieldRule", "auto");
japanese = readtable("data/japanese/original/data.dat", opts);
clear opts
A1 = categorical(japanese.A1);
A1_encoding = onehotencode(A1,2);
A4 = categorical(japanese.A4);
A4_encoding = onehotencode(A4,2);
A5 = categorical(japanese.A5);
A5_encoding = onehotencode(A5,2);
A6 = categorical(japanese.A6);
A6_encoding = onehotencode(A6,2);
A7 = categorical(japanese.A7);
A7_encoding = onehotencode(A7,2);
A9_encoding = double(categorical(japanese.A9));
A10_encoding = double(categorical(japanese.A10));
A12_encoding = double(categorical(japanese.A12));
A13 = categorical(japanese.A13);
A13_encoding = onehotencode(A13,2);
TARGET = double(categorical(japanese.TARGET));
japanese_df = horzcat(A1_encoding(:,1:3),...
normalize(japanese.A2),...
normalize(japanese.A3),...
A4_encoding(:,1:4),...
A5_encoding(:,1:4),...
A6_encoding(:,1:15),...
A7_encoding(:,1:10),...
normalize(japanese.A8),...
A9_encoding(:,1),...
A10_encoding(:,1),...
normalize(japanese.A11),...
A12_encoding(:,1),...
A13_encoding(:,1:3),...
normalize(japanese.A14),...
normalize(japanese.A15),...
TARGET(:,1));
% Separate train and test
cv = cvpartition(size(japanese_df,1),'HoldOut',0.3);
idx = cv.test;
dataTrain = japanese_df(~idx,:);
dataTest = japanese_df(idx,:);
X_train = dataTrain(:, 1:end-1);
Y_train = dataTrain(:, end);
X_test = dataTest(:, 1:end-1);
Y_test = dataTest(:, end);
for e = 1:length(epochs)
epoch = epochs(e);
for h = 1:length(hidden_nodes)
node = hidden_nodes(h);
partial_results = zeros(runs,1);
for j = 1:runs
net = fitcnet(X_train, Y_train,...
'LayerSizes', node,...
'Activations','sigmoid',...
'IterationLimit',epoch,...
'LayerBiasesInitializer','ones');
testAccuracy = 1 - loss(net,X_test,Y_test, "LossFun","classiferror");
partial_results(j) = testAccuracy;
end
% Process to save progressively data in a separated table
base = (e - 1) * 5;
results(base + h, 5) = max(partial_results);
end
end
clear A1 A1_encoding A10_encoding A12_encoding A13 A13_encoding A4 A4_encoding A5...
A5_encoding A6 A6_encoding A7 A7_encoding A9_encoding base cv dataTest...
dataTrain h e epoch epochs g hidden_nodes idx j japanese net node...
partial_results runs TARGET testAccuracy X_test X_train Y_test Y_train
writematrix(results,'data/single.csv')