forked from mdelhey/kaggle-titanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-neuralnet.R
executable file
·212 lines (165 loc) · 6.63 KB
/
4-neuralnet.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
<<<<<<< HEAD
# Goal: (1) Construct basic randomForest models from the data
# (2) Select the best model (Model selection)
# (3) Save a prediction with our best randomForest
library(neuralnet)
library(plyr)
library(nnet)
library(caret)
# Load in the cleaned data sets
load("Data/train_clean.RData") # 891 obs
load("Data/test_clean.RData") # 418 obs
###
### Create neural network model
###
## Make vectors because neuralnet is weird
sex <- train$sex
pclass <- train$pclass
fare <- train$fare
age <- train$age
survived <- train$survived
vectors <- cbind(sex, pclass, fare, age, survived)
vectors <- as.data.frame(vectors)
vectors$sex <- factor(vectors$sex)
vectors$pclass <- factor(vectors$pclass)
vectors$survived <- factor(vectors$survived)
sex.test <- test$sex
pclass.test <- test$pclass
fare.test <- test$fare
age.test <- test$age
survived.test <- test$survived
vectors.test <- cbind(sex.test, pclass.test, fare.test, age.test)
vectors.test <- as.data.frame(vectors.test)
colnames(vectors.test) <- c("sex", "pclass", "fare", "age")
vectors.test$sex.test <- factor(vectors.test$sex)
vectors.test$pclass.test <- factor(vectors.test$pclass)
vectors.test$survived.test <- factor(vectors.test$survived)
## Add family column
train$family <- NA
test$family <- NA
train$family[which(train$sibsp != 0 | train$parch != 0)] <- 1
train$family[which(train$sibsp == 0 & train$parch == 0)] <- 0
test$family[which(test$sibsp != 0 | test$parch != 0)] <- 1
test$family[which(test$sibsp == 0 & test$parch == 0)] <- 0
test$family <- factor(test$family)
train$family <- factor(train$family)
# Create neural network based on PCLASS, SEX, and FARE
net <- neuralnet(survived ~ sex + pclass + fare + age, data = vectors,
hidden = 2, err.fct="ce")
net2 <- neuralnet(survived ~ sex + pclass + fare + age, data = vectors,
hidden = 7, err.fct="ce", linear.output = FALSE)
net3 <- nnet(survived ~ sex + pclass + fare + age, data = train, size = 2,
linout = FALSE, maxit = 10000)
net4 <- nnet(survived ~ sex.name + pclass + fare + age, data = train, size = 2,
linout = FALSE, maxit = 10000)
net5 <- nnet(survived ~ sex.name + pclass + fare + age + family,
data = train, size = 2, linout = FALSE, maxit = 10000)
net6 <- nnet(survived ~ sex.name + pclass + fare_scale + age_scale,
data = train, size = 2, linout = FALSE, maxit = 10000)
model_net <- "nnet(survived ~ sex.name + pclass + fare_scale + age_scale,
data = train, size = 2, linout = FALSE, maxit = 10000)"
## Get the result
result <- compute(net, vectors.test)
result2 <- compute(net2, vectors.test)
result3 <- predict(net3, test, type = "class")
result4 <- predict(net4, test, type = "class")
result5 <- predict(net5, test, type = "class")
result6 <- predict(net6, test, type = "class")
## Since neuralnet is being a bitch, we round values
result$net.result[which(result$net.result < 1.5)] <- 1
result$net.result[which(result$net.result >= 1.5)] <- 2
test$survived <- result$net.result
test$survived[which(test$survived == 1)] <- 0
test$survived[which(test$survived == 2)] <- 1
## Use result 3
test.net <- test
test.net$survived <- result3
## Use result 4
test.net4 <- test
test.net4$survived <- result4
## Use result 5
test.net5 <- test
test.net5$survived <- result5
## Use result 6
test.net6 <- test
test.net6$survived <- result6
###
### Saving our model and prediction as a new CSV
###
# save csv file for submission
write.csv(test, "Submissions/neuralnet-01.csv")
write.csv(test.net, "Submissions/neuralnet-02.csv")
write.csv(test.net4, "Submissions/neuralnet-03.csv")
write.csv(test.net5, "Submissions/neuralnet-04.csv")
write.csv(test.net6, "Submissions/neuralnet-05.csv")
=======
# Goal: (1) Construct basic randomForest models from the data
# (2) Select the best model (Model selection)
# (3) Save a prediction with our best randomForest
library(neuralnet)
library(plyr)
library(nnet)
library(caret)
# Load in the cleaned data sets
load("Data/train_clean.RData") # 891 obs
load("Data/test_clean.RData") # 418 obs
###
### Create neural network model
###
## Make vectors because neuralnet is weird
sex <- train$sex
pclass <- train$pclass
fare <- train$fare
age <- train$age
survived <- train$survived
vectors <- cbind(sex, pclass, fare, age, survived)
vectors <- as.data.frame(vectors)
vectors$sex <- factor(vectors$sex)
vectors$pclass <- factor(vectors$pclass)
vectors$survived <- factor(vectors$survived)
sex.test <- test$sex
pclass.test <- test$pclass
fare.test <- test$fare
age.test <- test$age
#survived.test <- test$survived
vectors.test <- cbind(sex.test, pclass.test, fare.test, age.test)
vectors.test <- as.data.frame(vectors.test)
colnames(vectors.test) <- c("sex", "pclass", "fare", "age")
vectors.test$sex.test <- factor(vectors.test$sex)
vectors.test$pclass.test <- factor(vectors.test$pclass)
#vectors.test$survived.test <- factor(vectors.test$survived)
# Create neural network based on PCLASS, SEX, and FARE
#net <- neuralnet(survived ~ sex + pclass + fare + age, data = vectors,
# hidden = 2, err.fct="ce")
#net2 <- neuralnet(survived ~ sex + pclass + fare + age, data = vectors,
# hidden = 7, err.fct="ce", linear.output = FALSE)
net3 <- nnet(survived ~ sex + pclass + fare + age, data = train, size = 2,
linout = FALSE, maxit = 10000)
model <- "nnet(survived ~ sex + pclass + fare + age, data = train, size = 2, linout = FALSE, maxit = 10000)"
#net4 <- nnet(survived ~ sex.name + pclass + fare + age, data = train, size = 2,
# linout = FALSE, maxit = 10000)
## Get the result
#result <- compute(net, vectors.test)
#result2 <- compute(net2, vectors.test)
result3 <- predict(net3, test, type = "class")
#result4 <- predict(net4, test, type = "class")
## Since neuralnet is being a bitch, we round values
#result$net.result[which(result$net.result < 1.5)] <- 1
#result$net.result[which(result$net.result >= 1.5)] <- 2
#test$survived <- result$net.result
#test$survived[which(test$survived == 1)] <- 0
#test$survived[which(test$survived == 2)] <- 1
## Use result 3
test.net <- test
test.net$survived <- result3
## Use result 4
#test.net4 <- test
#test.net4$survived <- result4
###
### Saving our model and prediction as a new CSV
###
# save csv file for submission
write.csv(test, "Submissions/neuralnet-01.csv")
write.csv(test.net, "Submissions/neuralnet-02.csv")
#write.csv(test.net4, "Submissions/neuralnet-03.csv")
>>>>>>> 9184ca6ddd2bae49789cd120e77105a0de946972