-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew York Fare prediction xgboost.Rmd
241 lines (201 loc) · 8.98 KB
/
New York Fare prediction xgboost.Rmd
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
---
title: "NY Fare Prediction - Following Kaggle"
output:
html_document:
df_print: paged
editor_options:
chunk_output_type: inline
---
Below analysis is inspired by this kernel https://www.kaggle.com/obrienmitch94/nyc-taxi-fare-prediction (thanks Mitchell !, it's hard to find R kernel)
Loading the required libraries
```{r}
library(tidyverse)
library(xgboost)
library(caret)
library(magrittr)
library(Matrix)
library(geosphere)
```
Since i am running in my local machine , i am randomly selecting 2M rows instead of first 2M rows to avoid any selection bias
and rows with missing values are removed.
```{r cache=TRUE,cache.lazy = FALSE}
train=read_csv('datasets/train.csv',col_types =list(
key=col_character(),
fare_amount=col_double(),
pickup_datetime=col_datetime("%Y-%m-%d %H:%M:%S %Z"),
pickup_longitude=col_double(),
pickup_latitude=col_double(),
dropoff_longitude=col_double(),
dropoff_latitude=col_double(),
passenger_count=col_integer()
)) %>% select(-key) %>% sample_n(2000000) %>% na.omit()
```
Time for Summary stats
```{r}
summary(train)
```
We will remove some outliers and add some features
```{r}
train_1 = train %>%
mutate(pickup_datetime = as.POSIXct(pickup_datetime)) %>%
mutate(hour = as.numeric(format(pickup_datetime, "%H"))) %>%
mutate(min = as.numeric(format(pickup_datetime, "%M"))) %>%
mutate(year = as.factor(format(pickup_datetime, "%Y"))) %>%
mutate(day = as.factor(format(pickup_datetime, "%d"))) %>%
mutate(month = as.factor(format(pickup_datetime, "%m"))) %>%
mutate(Wday = as.factor(weekdays(pickup_datetime))) %>%
mutate(hour_class=as.factor(ifelse(hour<5,"Overnight",ifelse(hour<11,"Morning",ifelse(hour<16,"Noon",ifelse(hour<20,"Evening",ifelse(hour<23,"Night","Overnight"))))))) %>%
filter(pickup_longitude>-80 & pickup_longitude < -70) %>%
filter(pickup_latitude>35 & pickup_latitude < 45) %>%
filter(dropoff_longitude > -80 & dropoff_longitude < -70) %>%
filter(dropoff_latitude > 35 & dropoff_latitude < 45) %>%
filter(fare_amount > 2.5 & fare_amount <= 60) %>%
filter(passenger_count > 0 & passenger_count < 10)
```
important landmarks
```{r}
#jfk
jfk_lat<-40.6413
jfk_long<--73.7781
jfk<-c(jfk_long, jfk_lat)
#newark
nwk_lat<-40.6895
nwk_long<--74.1745
nwk<-c(nwk_long, nwk_lat)
#laguardia
lag_lat<-40.779
lag_long<--73.8740
lag<-c(lag_long, lag_lat)
#MSG
msg_lat<-40.7505
msg_long<--73.9934
msg<-c(msg_long, msg_lat)
#times square
ts_lat<-40.7589
ts_long<--73.9851
ts<-c(ts_long, ts_lat)
#freedom tower
freedom_lat<-40.7127
freedom_long<--74.0134
freedom<-c(freedom_long, freedom_lat)
#empire state building
esb_lat<-40.7484
esb_long<--73.9857
esb<-c(esb_long, esb_lat)
#grand central
grand_lat<-40.7527
grand_long<--73.9772
grand<-c(grand_long, grand_lat)
#bronx
bronx_lat <- (40.837048 * pi)/180
bronx_long <- (-73.865433 * pi)/180
bronx<-c(bronx_long, bronx_lat)
nyc<-c(-74.0063889, 40.7141667)
```
Calculating distance to the above landmarks
```{r}
train_2=train_1 %>%
mutate(
dist = distHaversine(cbind(pickup_longitude, pickup_latitude), cbind(dropoff_longitude, dropoff_latitude), r = 6371),
to_jfk = distHaversine(cbind(pickup_longitude, pickup_latitude), jfk, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), jfk, r = 6371),
to_nkw = distHaversine(cbind(pickup_longitude, pickup_latitude), nwk, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), nwk, r = 6371),
to_lag = distHaversine(cbind(pickup_longitude, pickup_latitude), lag, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), lag, r = 6371),
to_msg = distHaversine(cbind(pickup_longitude, pickup_latitude), msg, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), msg, r = 6371),
to_ts = distHaversine(cbind(pickup_longitude, pickup_latitude), ts, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), ts, r = 6371),
to_freedom = distHaversine(cbind(pickup_longitude, pickup_latitude), freedom, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), freedom, r = 6371),
#to_esb = distHaversine(cbind(pickup_longitude, pickup_latitude), esb, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), esb, r = 6371),
to_grand = distHaversine(cbind(pickup_longitude, pickup_latitude), grand, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), grand, r = 6371),
to_bronx = distHaversine(cbind(pickup_longitude, pickup_latitude), bronx, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), bronx, r = 6371),
to_nyc = distHaversine(cbind(pickup_longitude, pickup_latitude), nyc, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), nyc, r = 6371)
)
```
splitting into train and test dataset , since we have large dataset we split 60%/40%
```{r}
index=createDataPartition(train_2$year,p=0.6,list=FALSE)
dtrain_3=train_2[index,]
dtest_3=train_2[-index,]
```
Converting to radients
```{r}
to_rad=function(df)
{
df$pickup_longitude=df$pickup_longitude*pi/180
df$pickup_latitude=df$pickup_latitude*pi/180
df$dropoff_longitude=df$dropoff_longitude*pi/180
df$dropoff_latitude=df$dropoff_latitude*pi/180
return(df)
}
dtrain_4=to_rad(dtrain_3)
dtest_4=to_rad(dtest_3)
```
Converting to dmatrix as required by xgboost
```{r}
dtrain_4_matrix=xgb.DMatrix(data=data.matrix(dtrain_4[,-1]),label=dtrain_4$fare_amount)
dtest_4_matrix=xgb.DMatrix(data=data.matrix(dtest_4[,-1]),label=dtest_4$fare_amount)
```
training the model with the below parameters
```{r}
p <- list(objective = "reg:linear",
eval_metric = "rmse",
max_depth = 6 ,
eta = .05, #.05
subsample=1,
colsample_bytree=0.8,
num_boost_round=1000,
nrounds = 300)
set.seed(0)
m_xgb <- xgb.train(p, dtrain_4_matrix, p$nrounds, list(val = dtest_4_matrix), print_every_n = 1, early_stopping_rounds = 10)
```
loading the test dataset
```{r}
test=read_csv('datasets/test.csv',col_types =list(
key=col_character(),
pickup_datetime=col_datetime("%Y-%m-%d %H:%M:%S %Z"),
pickup_longitude=col_double(),
pickup_latitude=col_double(),
dropoff_longitude=col_double(),
dropoff_latitude=col_double(),
passenger_count=col_integer()
))
```
Adding the same feature as train
```{r}
test_1 = test %>%
mutate(pickup_datetime = as.POSIXct(pickup_datetime)) %>%
mutate(hour = as.numeric(format(pickup_datetime, "%H"))) %>%
mutate(min = as.numeric(format(pickup_datetime, "%M"))) %>%
mutate(year = as.factor(format(pickup_datetime, "%Y"))) %>%
mutate(day = as.factor(format(pickup_datetime, "%d"))) %>%
mutate(month = as.factor(format(pickup_datetime, "%m"))) %>%
mutate(Wday = as.factor(weekdays(pickup_datetime))) %>%
mutate(hour_class=as.factor(ifelse(hour<5,"Overnight",ifelse(hour<11,"Morning",ifelse(hour<16,"Noon",ifelse(hour<20,"Evening",ifelse(hour<23,"Night","Overnight")))))))
```
```{r}
test_2=test_1 %>%
mutate(
dist = distHaversine(cbind(pickup_longitude, pickup_latitude), cbind(dropoff_longitude, dropoff_latitude), r = 6371),
to_jfk = distHaversine(cbind(pickup_longitude, pickup_latitude), jfk, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), jfk, r = 6371),
to_nkw = distHaversine(cbind(pickup_longitude, pickup_latitude), nwk, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), nwk, r = 6371),
to_lag = distHaversine(cbind(pickup_longitude, pickup_latitude), lag, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), lag, r = 6371),
to_msg = distHaversine(cbind(pickup_longitude, pickup_latitude), msg, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), msg, r = 6371),
to_ts = distHaversine(cbind(pickup_longitude, pickup_latitude), ts, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), ts, r = 6371),
to_freedom = distHaversine(cbind(pickup_longitude, pickup_latitude), freedom, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), freedom, r = 6371),
#to_esb = distHaversine(cbind(pickup_longitude, pickup_latitude), esb, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), esb, r = 6371),
to_grand = distHaversine(cbind(pickup_longitude, pickup_latitude), grand, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), grand, r = 6371),
to_bronx = distHaversine(cbind(pickup_longitude, pickup_latitude), bronx, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), bronx, r = 6371),
to_nyc = distHaversine(cbind(pickup_longitude, pickup_latitude), nyc, r = 6371) + distHaversine(cbind(dropoff_longitude, dropoff_latitude), nyc, r = 6371)
)
```
```{r}
test_3=to_rad(test_2)
test_3_matrix=xgb.DMatrix(data=data.matrix(test_3[,-1]))
```
Finally predicting !!
```{r}
prediction_xgb=predict(m_xgb,newdata=test_3_matrix)
```
```{r}
prediction=data.frame(key=test$key,fare_amount=prediction_xgb)
write_csv(prediction,"prediction.csv")
```
I got 3.34 RMSE in public leader board