-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNet1.m
177 lines (154 loc) · 5.31 KB
/
NeuralNet1.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
function [outputArg1,outputArg2] = NeuralNet1(inputArg1,inputArg2)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
%table = readtable('../kddcupdata_10percent_duplicatesremoved_small.xlsx');
%table = readtable('../kddcupdata_10percent_duplicatesremoved_small_cols8_20_21_deleted.xlsx');
%table = readtable('../kddcupdata_10percent_duplicatesremoved_allTargets_Col7_8_9_15_20_21_deleted_multiclass.xlsx','Range','A1:AO145578');
%table = readtable('../kddcupdata_10percent_duplicatesremoved_allTargets_Col7_8_9_15_20_21_deleted_multiclass.xlsx','Range','A1:AO1000000');
%table = readtable('../kddcupdata_10percent_duplicatesremoved_allTargets_Col21_deleted_mulitclass.xlsx','Range','A1:AT100000');
table = readtable('../kddcupdata_10percent_duplicatesremoved_allTargets_Col20_21_deleted_mulitclass.xlsx','Range','A1:AS145587');
%X = table(:,[1:end-3]); %small file
X = table(:,1:39); %big file
A = table2array(X);
%T = table(:,[end-1:end]); %small file
T = table(:,41:end); %big file
targets = table2array(T);
%normalise data
normA = normalize(A);
%Remove NaNs
% Create an anonymous function to detect NaNs
f = @(x) any(isnan(x));
% Detect where there are NaNs
out = num2cell(normA,1);
B = cellfun(f,out);
% Now detect columns that contain ONLY NaNs
C = all(B);
% Remove these columns
normA(:,C) = [];
% Principal Component Analysis
[coeff,score,latent] = pca(normA);
%use Kaiser criteria rather than constant???????????????? drop every
%component under 1.0 -> 11 for 50k sample
% -> 13 for 100k sample
% -> 10 for total dataset
k = 10;
%k = cumsum(latent)./sum(latent);
inputs = score(:,[1:k]);
% Detect where there are NaNs
out = num2cell(inputs,1);
B = cellfun(f,out);
% Now detect columns that contain ONLY NaNs
C = all(B);
% Remove these columns
inputs(:,C) = [];
% Create a Pattern Recognition Network
%hiddenLayerSize = 2;
%net = patternnet(hiddenLayerSize);
%
%inputs = transpose(A);
inputs = transpose(inputs);
targets = transpose(targets);
% K-fold cross validation
k = 10;
CVO = cvpartition(length(inputs),'KFold',k); %%% low perforance k = 3;
for i = 1:k %# for each fold
trainIdx = CVO.training(i);
testIdx = CVO.test(i); %# get indices training instances
trInd = find(trainIdx);
tstInd = find(testIdx);
% Create a Pattern Recognition Network
%hiddenLayerSize = 10;
%net = patternnet(hiddenLayerSize);
if i == 1
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainbr';
%net.trainFcn = 'trainbr';
net.trainParam.epochs = 50;
end
if i == 2
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainbr';
net.trainParam.epochs = 100;
end
if i == 3
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'traingd';
% net.trainFcn = 'traingd';
net.trainParam.epochs = 50;
end
if i == 4
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'traingd';
net.trainParam.epochs = 100;
end
if i == 5
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
%net.trainFcn = 'trainbr';
net.trainFcn = 'traingda';
net.trainParam.epochs = 50;
end
if i == 6
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'traingda';
net.trainParam.epochs = 100;
end
if i == 7
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainrp';
net.trainParam.epochs = 50;
end
if i == 8
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainrp';
net.trainParam.epochs = 100;
end
if i == 9
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainb';
net.trainParam.epochs = 50;
end
if i == 10
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
net.trainFcn = 'trainb';
net.trainParam.epochs = 100;
end
%net.trainParam.lr = 0.5; %Learning rate
%net.trainFcn = 'trainbr';
%net.trainFcn = 'traingd';
%net.trainParam.epochs = 100;
%net.trainParam.epochs = 200;
net.divideFcn = 'divideind';
net.divideParam.trainInd = trInd;
net.divideParam.testInd = tstInd;
% Choose a Performance Function
net.performFcn = 'mse'; % Mean squared error
% Train the Network
[net,tr] = train(net,inputs,targets);
%# test using test instances
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);
trainTargets = targets .* tr.trainMask{1};
testTargets = targets .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs);
testPerformance = perform(net,testTargets,outputs);
test(i)=testPerformance;
%save net;
figure, plotconfusion(targets,outputs);
end
accuracy=mean(test);
% View the Network
view(net);
end