-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFractal Hack Script.R
300 lines (194 loc) · 8.15 KB
/
Fractal Hack Script.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
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
#Loading required libraries
library(caret)
library(xgboost)
library(dplyr)
library(ggplot2)
#Reading train n test
train<-read.csv("train.csv")
test<-read.csv("test.csv")
sample_sub<-read.csv("Sample_Submission_Zxs5Ys1.csv")
#Correcting data types
#train$Category_1<-as.factor(train$Category_1)
#test$Category_1<-as.factor(test$Category_1)
train$Category_1<-as.numeric(train$Category_1)
test$Category_1<-as.numeric(test$Category_1)
train$Category_2<-as.numeric(train$Category_2)
test$Category_2<-as.numeric(test$Category_2)
train$Category_3<-as.factor(train$Category_3)
test$Category_3<-as.factor(test$Category_3)
train$Datetime<-as.Date(train$Datetime,"%Y-%m-%d")
test$Datetime<-as.Date(test$Datetime,"%Y-%m-%d")
#Creating features
train$year<-as.numeric(format(train$Datetime, "%Y"))
test$year<-as.numeric(format(test$Datetime, "%Y"))
train$month<-as.numeric(format(train$Datetime, "%m"))
test$month<-as.numeric(format(test$Datetime, "%m"))
train$day<-as.numeric(format(train$Datetime, "%d"))
test$day<-as.numeric(format(test$Datetime, "%d"))
train$weekday<-as.factor(weekdays(train$Datetime))
test$weekday<-as.factor(weekdays(test$Datetime))
train_features<-as.data.frame(train %>%
group_by(Item_ID) %>%
summarise(sum_price_n = sum(Price)/ log(1+length(Price)), mean_price = mean(Price), median_price = median(Price), sd_price = sd(Price), nprice = length(Price),
nsales_price_n = sum(Number_Of_Sales)/ log(1+length(Number_Of_Sales)), mean_nsales = mean(Number_Of_Sales), median_nsales = median(Number_Of_Sales), sd_nsales = sd(Number_Of_Sales), nsales_id = length(Number_Of_Sales)))
test_features<-as.data.frame(test %>%
group_by(Item_ID) %>%
summarise(nprice_2 = length(ID),
nsales_id_2 = length(ID)))
train_new<-merge(train,train_features,by = "Item_ID")
test_new<-merge(test,train_features,by = "Item_ID")
train_new2<-merge(train_new,test_features,by = "Item_ID")
test_new2<-merge(test_new,test_features,by = "Item_ID")
train_new2$diff_p<-train_new2$nprice-train_new2$nprice_2
train_new2$diff_s<-train_new2$nsales_id-train_new2$nsales_id_2
diff_df<-as.data.frame(train_new2[,c("Item_ID", "diff_p", "diff_s")] %>%
group_by(Item_ID) %>%
summarise(diff_p = mean(diff_p),
diff_s = length(diff_s)))
test_new3<-merge(test_new2,diff_df,by = "Item_ID")
train_new2$Datetime<-NULL
test_new3$Datetime<-NULL
y_price<-train_new2$Price
y_nsales<-train_new2$Number_Of_Sales
y_price_l<-log(y_price+1)
y_nsales_l<-log(y_nsales+1)
train_new2$Price<-NULL
train_new2$Number_Of_Sales<-NULL
test_item_id<-test_new3$Item_ID
test_id<-test_new3$ID
#train_new2$mean_price_max <- train_new2$mean_price+train_new2$sd_price
#train_new2$mean_nsales_max <- train_new2$mean_nsales+train_new2$sd_nsales
#train_new2$mean_price_max <- train_new2$mean_price-train_new2$sd_price
#train_new2$mean_nsales_max <- train_new2$mean_nsales-train_new2$sd_nsales
train_new2$meansd_price <- train_new2$mean_price*train_new2$sd_price
train_new2$meansd_nsales <- train_new2$mean_nsales*train_new2$sd_nsales
test_new3$meansd_price <- test_new3$mean_price*test_new3$sd_price
test_new3$meansd_nsales <- test_new3$mean_nsales*test_new3$sd_nsales
train_new2$Item_ID<-NULL
test_new3$Item_ID<-NULL
train_new2$ID<-NULL
test_new3$ID<-NULL
plot(train_new2$sum_price_n,y_price_l)
plot(train_new2$mean_price,y_price_l)
plot(train_new2$nprice,y_price_l)
dmy <- dummyVars(" ~ .", data = train_new2, fullRank = F)
x_train <- data.frame(predict(dmy, newdata = train_new2))
dmy <- dummyVars(" ~ .", data = test_new3, fullRank = F)
x_test <- data.frame(predict(dmy, newdata = test_new3))
#x_train$is_weekend<-as.numeric(ifelse(x_train$weekday.Saturday==1|x_train$weekday.Sunday==1,1,0))
#x_test$is_weekend<-as.numeric(ifelse(x_test$weekday.Saturday==1|x_test$weekday.Sunday==1,1,0))
x_train$mean_price_l<-log(1+x_train$mean_price)
x_train$mean_nsales_l<-log(1+x_train$mean_nsales)
x_test$mean_price_l<-log(1+x_test$mean_price)
x_test$mean_nsales_l<-log(1+x_test$mean_nsales)
x_train$sd_price_l<-log(1+x_train$sd_price)
x_train$sd_nsales_l<-log(1+x_train$sd_nsales)
x_test$sd_price_l<-log(1+x_test$sd_price)
x_test$sd_nsales_l<-log(1+x_test$sd_nsales)
predictors<-c("Category_3.0", "Category_3.1", "Category_2", "Category_1",
"year", "month", "day", "weekday.Friday", "weekday.Monday", "weekday.Saturday",
"weekday.Sunday", "weekday.Thursday", "weekday.Tuesday", "weekday.Wednesday",
"mean_price_l", "mean_nsales_l", "sd_price_l", "sd_nsales_l")
predictors<-c("Category_3.1", "Category_2", "Category_1",
"year", "month",
"mean_price_l", "mean_nsales_l", "sd_price_l", "sd_nsales_l")
dtrain_price <- xgb.DMatrix(data=as.matrix(x_train[,predictors]), label=y_price_l)
dtrain_sales <- xgb.DMatrix(data=as.matrix(x_train[,predictors]), label=y_nsales_l)
dtest <- xgb.DMatrix(data=as.matrix(x_test[,predictors]))
#watchlist <- list(train=dtrain_un2, validation=dval)
evalerror <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label")
precision <- sum(preds & labels) / sum(preds)
recall <- sum(preds & labels) / sum(labels)
fmeasure <- 2 * precision * recall / (precision + recall)
return(list(metric = "F-Score", value = fmeasure))
}
param <- list(
objective = "reg:linear",
eval_metric = "rmse",
eta = 0.3,
# lambda = 2
max_depth = 2
)
cv_price <- xgb.cv(
params = param,
data = dtrain_price,
nrounds = 10000,
# watchlist = watchlist,
nfold = 10,
maximize = F,
early_stopping_rounds = 20,
print_every_n = 50
)
xgb_price <- xgb.train(
params = param,
data = dtrain_price,
nrounds = 1500
# watchlist = watchlist,
# maximize = F,
# early_stopping_rounds = 20
)
param2 <- list(
objective = "reg:linear",
eval_metric = "rmse",
eta = 0.3,
# lambda = 2
max_depth = 2
)
set.seed(1)
cv_sales <- xgb.cv(
params = param2,
data = dtrain_sales,
nrounds = 10000,
nfold = 10,
print_every_n = 20
)
xgb_nsales <- xgb.train(
params = param2,
data = dtrain_sales,
nrounds = 900,
# watchlist = watchlist,
maximize = T
# early_stopping_rounds = 20
)
gg <- xgb.ggplot.importance(xgb.importance(model = xgb_price,feature_names = predictors), measure = "Gain", rel_to_first = TRUE)
gg + ggplot2::ylab("Frequency")
gg <- xgb.ggplot.importance(xgb.importance(model = xgb_nsales,feature_names = predictors), measure = "Gain", rel_to_first = TRUE)
gg + ggplot2::ylab("Frequency")
y_pred_price<-exp(predict(object = xgb_price,newdata = dtest))-1
y_pred_nsales<-exp(predict(object = xgb_nsales,newdata = dtest))-1
y_pred_price[y_pred_price<0]<-0
y_pred_nsales[y_pred_nsales<0]<-0
sub<-data.frame(ID = test_id, Number_Of_Sales = y_pred_nsales, Price = y_pred_price)
#Final submission
#Public LB Rank: 5
#Private LB Rank: 4
write.csv(sub, "sub16_without_weekdays.csv", row.names = F)
#Linear model
tr<-x_train[,predictors]
te<-x_test[,predictors]
for(i in 1:ncol(tr))
{
if(is.numeric(tr[,i]))
{
tr[is.na(tr[,i]),i]<-as.numeric(median(tr[,i], na.rm = TRUE))
}
}
for(i in 1:ncol(te))
{
if(is.numeric(te[,i]))
{
te[is.na(te[,i]),i]<-as.numeric(median(te[,i], na.rm = TRUE))
}
}
tr$y_price_l<-y_price_l
tr$y_nsales_l<-y_nsales_l
model_price = lm(y_price_l ~ ., data = tr[,predictors])
model_sales = lm(y_nsales_l ~ ., data = tr[,predictors])
y_pred_price<-exp(predict(object = model_price,newdata = te))-1
y_pred_nsales<-exp(predict(object = model_sales,newdata = te))-1
y_pred_price[y_pred_price<0]<-0
y_pred_nsales[y_pred_nsales<0]<-0
sub<-data.frame(ID = test_id, Number_Of_Sales = y_pred_nsales, Price = y_pred_price)
#Did not used the linear model
#write.csv(sub, "sub14_with_mean_sd_both_log_plus_one_linear.csv", row.names = F)