-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathJF_H20_GBM_script_v1.7.R
271 lines (225 loc) · 11.1 KB
/
JF_H20_GBM_script_v1.7.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# H2O GBM script version 1
library(caret)
library(plyr)
library(dplyr)
library(data.table)
library(h2o)
cat("reading the train and test data (with data.table) \n")
train0 <- fread("../data/train3.csv",stringsAsFactors = T)
test <- fread("../data/test3.csv",stringsAsFactors = T)
store <- fread("./input/store.csv",stringsAsFactors = T)
train0 <- train0[Sales > 0,] ## We are not judged on 0 sales records in test set
train0 <- merge(train0,store,by="Store")
test <- merge(test,store,by="Store")
## more care should be taken to ensure the dates of test can be projected from train
## decision trees do not project well, so you will want to have some strategy here, if using the dates
train0[,Date:=as.Date(Date)]
test[,Date:=as.Date(Date)]
# competition feature
train0$Competition <- (sqrt(max(train0$CompetitionDistance, na.rm = TRUE)-train0$CompetitionDistance))*
(((train0$year - train0$CompetitionOpenSinceYear) * 12) - (train0$CompetitionOpenSinceMonth-train0$month))
test$Competition <- (sqrt(max(test$CompetitionDistance, na.rm = TRUE)-test$CompetitionDistance))*
(((test$year - test$CompetitionOpenSinceYear) * 12) - (test$CompetitionOpenSinceMonth-test$month))
## log transformation to not be as sensitive to high sales
## decent rule of thumb:
## if the data spans an order of magnitude, consider a log transform
train0[,logSales:=log1p(Sales)]
train = train0
train_A = train0[day_of_year %% 2 == 0,]
train_B = train0[day_of_year %% 2 == 1,]
dim(valid)
dim(train)
unique(train0$day_of_year %% 2)
# Set appropriate variables to factors
for (j in c("Store", "DayOfWeek", "Promo",
"year", "month", "day", "PromoFirstDate",
# "day_of_year", "week_of_year", "DayBeforeClosed", "DayAfterClosed",
"State", "PromoSecondDate",
"CompetitionOpenSinceMonth", "CompetitionOpenSinceYear",
"Promo2", "Promo2SinceWeek", "Promo2SinceYear")) {
train[[j]] <- as.factor(train[[j]])
train_A[[j]] <- as.factor(train_A[[j]])
train_B[[j]] <- as.factor(train_B[[j]])
test[[j]] <- as.factor(test[[j]])
}
## Useful functions:
rmse = function(predictions, targets) {
return(((predictions - targets)/targets) ** 2)
}
sumup = function(model, trainHex, train) {
train_pred = as.data.frame(h2o.predict(model,trainHex))
train_pred <- expm1(train_pred[,1])
train$pred = train_pred
train$rmse = rmse(train_pred, train$Sales)
train2 = filter(train, month %in% c(8,9))
total_rmse = sqrt(sum(train$rmse)/nrow(train))
print("Total RMSE:")
print(total_rmse)
partial_rmse = sqrt(sum(train2$rmse)/nrow(train2))
print("RMSE on Aug/Sep:")
print(partial_rmse)
temp = as.data.frame(rbind(summary(train_pred), summary(train$Sales), summary(train2$pred), summary(train2$Sales)))
temp$sd = c(round(sd(train_pred)), round(sd(train$Sales)), round(sd(train2$pred)), round(sd(train2$Sales)))
print("Stats of predictions vs. actual:")
print(temp)
}
## Use H2O's random forest
## Start cluster with all available threads
h2o.init(nthreads=-1,max_mem_size='5G', assertion = FALSE)
## create validation and training set
trainHex <- as.h2o(train)
trainHex_A <- as.h2o(train_A)
trainHex_B <- as.h2o(train_B)
testHex <- as.h2o(test)
## Load data into cluster from R
features = read.csv('./H2O_submits/h2o_GBM_20_03_800_top100_varimp.csv')
features = features$variable
features = as.character(features)
####################################################################################
gbmHex_full <- h2o.gbm( x = features,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.5,
seed = 12345678,
ntrees = 200)
(varimps = data.frame(h2o.varimp(gbmHex_full)))
write.csv(varimps, "./H2O_submits/H2O_GBM_12_05_200_FULL_varimp.csv",row.names=F)
h2o.saveModel(gbmHex_full, path = '/Users/jfdarre/Documents/NYCDS/Project4/H2O_GBM_12_05_200_FULL', force = FALSE)
cat("Predicting Sales\n")
test_pred_full <- as.data.frame(h2o.predict(gbmHex_full,testHex))
test_pred_full <- expm1(test_pred_full[,1])
submit_full <- data.frame(Id=test$Id, Sales=test_pred_full)
cat("saving the submission file\n")
write.csv(submit_CV, "./H2O_submits/H2O_GBM_12_05_200_full",row.names=F)
train_pred_full <- as.data.frame(h2o.predict(gbmHex_full,trainHex))
train_pred_full <- expm1(train_pred_full[,1])
train_pred_full <- data.frame(Id=train$Id, Sales=train_pred_full)
write.csv(train_pred_full, "./H2O_submits/H2O_GBM_12_05_200_full_train.csv",row.names=F)
####################################################################################
gbmHex_A <- h2o.gbm( x = features,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.5,
seed = 12345678,
ntrees = 200)
(varimps = data.frame(h2o.varimp(gbmHex_A)))
write.csv(varimps, "./H2O_submits/H2O_GBM_12_05_200_A_varimp.csv",row.names=F)
h2o.saveModel(gbmHex_A, path = '/Users/jfdarre/Documents/NYCDS/Project4/H2O_GBM_12_05_200_A', force = FALSE)
cat("Predicting Sales\n")
test_pred_A <- as.data.frame(h2o.predict(gbmHex_A,testHex))
test_pred_A <- expm1(test_pred_A[,1])
submit_A <- data.frame(Id=test$Id, Sales=test_pred_A)
cat("saving the submission file\n")
write.csv(submit_CV, "./H2O_submits/H2O_GBM_12_05_200_A",row.names=F)
train_pred_A <- as.data.frame(h2o.predict(gbmHex_A,trainHex_B))
train_pred_A <- expm1(train_pred_A[,1])
train_pred_A <- data.frame(Id=train_B$Id, Sales=train_pred_A)
write.csv(train_pred_A, "./H2O_submits/H2O_GBM_12_05_200_A_train.csv",row.names=F)
####################################################################################
gbmHex_B <- h2o.gbm( x = features,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.5,
seed = 12345678,
ntrees = 200)
(varimps = data.frame(h2o.varimp(gbmHex_B)))
write.csv(varimps, "./H2O_submits/H2O_GBM_12_05_200_B_varimp.csv",row.names=F)
h2o.saveModel(gbmHex_B, path = '/Users/jfdarre/Documents/NYCDS/Project4/H2O_GBM_12_05_200_B', force = FALSE)
cat("Predicting Sales\n")
test_pred_B <- as.data.frame(h2o.predict(gbmHex_B,testHex))
test_pred_B <- expm1(test_pred_B[,1])
submit_B <- data.frame(Id=test$Id, Sales=test_pred_B)
cat("saving the submission file\n")
write.csv(submit_CV, "./H2O_submits/H2O_GBM_12_05_200_B",row.names=F)
train_pred_B <- as.data.frame(h2o.predict(gbmHex_B,trainHex_A))
train_pred_B <- expm1(train_pred_B[,1])
train_pred_B <- data.frame(Id=train_A$Id, Sales=train_pred_B)
write.csv(train_pred_B, "./H2O_submits/H2O_GBM_12_05_200_B_train.csv",row.names=F)
####################################################################################
e = seq(1,100,2)
f = seq(2,100,2)
####################################################################################
feats_e = features[e]
gbmHex_e <- h2o.gbm(x = feats_e,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.05,
seed = 12345678,
ntrees = 200,
validation_frame = validHex)
summary(gbmHex_e)
h2o.saveModel(gbmHex_e, path = '../H2O_models_GBM_15_05_400_CV_e', force = FALSE)
cat("Predicting Sales\n")
pred_e <- as.data.frame(h2o.predict(gbmHex_e, testHex))
pred_e <- expm1(pred_e[,1])
submit_e <- data.frame(Id = test$Id, Sales = pred_e)
cat("saving the submission file\n")
write.csv(submit_e, "./H2O_submits/H2O_models_GBM_15_05_400_CV_e.csv",row.names=F)
train_pred_e <- as.data.frame(h2o.predict(gbmHex_e, trainHex))
train_pred_e <- expm1(train_pred_e[,1])
train_pred_e <- data.frame(Id = train$Id, Sales = train_pred_e)
write.csv(train_pred_e, "./H2O_submits/H2O_models_GBM_15_05_400_CV_e.csv",row.names=F)
####################################################################################
feats_f = features[f]
gbmHex_f <- h2o.gbm(x = feats_f,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.05,
seed = 12345678,
ntrees = 200,
validation_frame = validHex)
summary(gbmHex_f)
h2o.saveModel(gbmHex_f, path = '../H2O_models_GBM_15_05_400_CV_f', force = FALSE)
cat("Predicting Sales\n")
pred_f <- as.data.frame(h2o.predict(gbmHex_f, testHex))
pred_f <- expm1(pred_f[,1])
submit_f <- data.frame(Id = test$Id, Sales = pred_f)
cat("saving the submission file\n")
write.csv(submit_f, "./H2O_submits/H2O_models_GBM_15_05_400_CV_f.csv",row.names=F)
train_pred_f <- as.data.frame(h2o.predict(gbmHex_f, trainHex))
train_pred_f <- expm1(train_pred_f[,1])
train_pred_f <- data.frame(Id = train$Id, Sales = train_pred_f)
write.csv(train_pred_f, "./H2O_submits/H2O_models_GBM_15_05_400_CV_f.csv",row.names=F)
####################################################################################
train_B$trainAB = log1p(train_pred_A$Sales)
train_A$trainAB = log1p(train_pred_B$Sales)
test$trainAB = log1p(test_pred_full)
train_new = rbind(train_A,train_B)
train_newHex = as.h2o(train_new)
features = c(features, "trainAB")
gbmHex_ensb <- h2o.gbm( x = features,
y = "logSales",
training_frame = trainHex,
model_id = "introGBM",
nbins_cats = 1115,
sample_rate = 0.5,
col_sample_rate = 0.5,
max_depth = 12,
learn_rate = 0.5,
seed = 12345678,
ntrees = 200)