-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_dataAnalysis_04_NN.R
248 lines (194 loc) · 7.4 KB
/
02_dataAnalysis_04_NN.R
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
# RANDOM FORESTS WITH BAGGING OPTIMIZATION
# Setup -------------------------
library(tidyverse)
library(caret)
library(pROC)
# cran <- getOption("repos")
# cran["dmlc"] <- "https://s3-us-west-2.amazonaws.com/apache-mxnet/R/CRAN/"
# options(repos = cran)
# install.packages("mxnet",dependencies = T)
library(mxnet)
rm(list = ls())
# Reading the previously saved version of our data
load("../Roeser, Jonas - 2_Data/DF.RData")
# Because of OneDrive we need to load from two different paths
load("../2_Data/DF.RData")
# Preperation ------------
# Data Formatting
DFopt = DF[1:(0.3*nrow(DF)),]
DFkfold = DF[-(1:(0.3*nrow(DF))),]
# Train() needs the ouput to be a factor of two levels
DFopt[,ncol(DFopt)] = as.factor(DFopt[,ncol(DFopt)])
DFkfold[,ncol(DFkfold)] = as.factor(DFkfold[,ncol(DFopt)])
# Optimisation --------------------------------------------------
# Pre-optimising hyperparameters with all features ---------------
# The grid allows us to create several different models with various parameter settings.
# Grid features
grid = expand.grid(layer1 = seq(1,11,2),
layer2 = seq(0,8,2),
layer3 = seq(0,9,3),
learning.rate = c(0.05,0.1),
momentum = 0.85,
dropout = 0.5,
activation = "relu"
)
# We are using 5-fold cross-validation for paramter tuning
control = trainControl(method = "cv",
number = 5)
temp_model = train(Y ~ .,
data = DFopt,
method = "mxnet",
tuneGrid = grid,
trControl = control,
num.round = 10,
maximize = TRUE
)
# Accuracy was used to select the optimal model using the largest value.
# The final values used for the model were
# layer1 = 5
# layer2 = 6
# layer3 = 6
# learning.rate = 0.1
# momentum = 0.85
# dropout = 0.5
# activation = "relu"
# Optimising features with pre-optimized hyperparameters ----------
# Reading the feature_test created with logistic regression
load("../Roeser, Jonas - 2_Data/feature_test.RData")
# Because of OneDrive we need to load from two different paths
load("../2_Data/feature_test.RData")
# Creating a matrix for testing the 10 best feature combinations of logistic regression
feature_test_nn = matrix(nrow = 10, ncol = 2)
colnames(feature_test_oob) = c("comb",
"nn_accuracy")
feature_test_nn[,1] = feature_test[order(feature_test[,3], decreasing=T)[1:10],1]
# Creating function that takes feature combination vector as input
neural = function(comb) {
DFopt = DFopt[,comb]
grid = expand.grid(layer1 = 5,
layer2 = 6,
layer3 = 6,
learning.rate = 0.1,
momentum = 0.85,
dropout = 0.5,
activation = "relu"
)
# We are using 5-fold cross-validation for paramter tuning
control = trainControl(method = "cv",
number = 10)
temp_model = train(Y ~ .,
data = DFopt,
method = "mxnet",
tuneGrid = grid,
trControl = control,
num.round = 10,
maximize = TRUE
)
return(temp_model$results$Accuracy)
}
for(i in 1:10) {
feature_test_nn[i,2] = neural(c(unlist(lapply(strsplit(feature_test_nn[i,1], split=","), as.numeric)),10))
}
best_comb = feature_test_nn[which.max(feature_test_nn[,2]),1]
# --> We get the highest testing accuracy when training with all feateures, except home game!
# Optimising hyperparameters with optimized features --------------
# Now we use the optimal feature combination from above
best_comb
# The grid allows us to create several different models with various parameter settings.
# Grid features
grid = expand.grid(layer1 = c(4,5,6),
layer2 = c(5,6,7),
layer3 = c(5,6,7),
learning.rate = c(0.075,0.125),
momentum = 0.85,
dropout = 0.5,
activation = "relu"
)
# We are using 5-fold cross-validation for paramter tuning
control = trainControl(method = "cv",
number = 5)
temp_model = train(Y ~ .,
data = DFopt[,c(1,2,3,4,5,6,7,8,10)],
method = "mxnet",
tuneGrid = grid,
trControl = control,
num.round = 20,
maximize = TRUE
)
# Accuracy was used to select the optimal model using the largest value.
# The final values used for the model were
# layer1 = 6
# layer2 = 5
# layer3 = 6
# learning.rate = 0.075
# momentum = 0.85
# dropout = 0.5
# activation = "relu"
# Creating the optimal model
grid = expand.grid(layer1 = 6,
layer2 = 5,
layer3 = 6,
learning.rate = 0.075,
momentum = 0.85,
dropout = 0.5,
activation = "relu"
)
control = trainControl(method = "cv",
number = 5)
model = train(Y ~ .,
data = DFopt[,c(1,2,3,4,5,6,7,8,10)],
method = "mxnet",
tuneGrid = grid,
trControl = control,
num.round = 20,
maximize = TRUE
)
# Plotting ROC curve -------------------
NN_probs <- predict(model, DFopt, type = "prob")
pred_NN <- prediction(NN_probs[,1], DFopt$Y)
perf_NN <- performance(pred_NN, "fpr", "tpr")
plot(perf_NN,xlim = c(0, 1), ylim = c(0, 1), type = "l",
xlab = "false positive rate", ylab = "true positive rate", col = 'green')
abline(0, 1, col= "black")
#AUC
auc_NN = performance(pred_NN, "auc")
auc_ROCR = [email protected][[1]]
#0.7157735
# Kfold --------------------------------
# Perform 10 fold cross validation
neural_kfold = function (data,comb) {
data = data[,comb]
grid = expand.grid(layer1 = 6,
layer2 = 5,
layer3 = 6,
learning.rate = 0.075,
momentum = 0.85,
dropout = 0.5,
activation = "relu"
)
control = trainControl(method = "cv",
number = 10)
model = train(Y ~ .,
data = data,
method = "mxnet",
tuneGrid = grid,
trControl = control,
num.round = 20,
maximize = TRUE
)
return(model$results$Accuracy)
}
# Because we are unable to handle very large amounts of data, we split DFkfold up into 3 subsets
DFkfold1 = DFkfold[1:(nrow(DFkfold)/3),]
DFkfold2 = DFkfold[(nrow(DFkfold)/3):(nrow(DFkfold)*2/3),]
DFkfold3 = DFkfold[(nrow(DFkfold)*2/3):nrow(DFkfold),]
# Create kfold matrix
kfold = matrix(nrow = 3, ncol = 1)
best_comb
# Perform 10 fold cross validation for each DKfold
kfold[1,1] = neural_kfold(DFkfold1, c(1,2,3,4,5,6,7,8,10))
kfold[2,1] = neural_kfold(DFkfold2, c(1,2,3,4,5,6,7,8,10))
kfold[3,1] = neural_kfold(DFkfold3, c(1,2,3,4,5,6,7,8,10))
# Saving model accuracy as "model_accuracy_NN.RData"
model_accuracy_NN = colMeans(kfold)
# save(model_accuracy_NN, file = "../Roeser, Jonas - 2_Data/model_accuracy_NN.RData")