-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxi_fare_prediction.py
503 lines (231 loc) · 9.21 KB
/
taxi_fare_prediction.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# coding: utf-8
# ## Loading 1M rows of the dataset
# In[1]:
import pandas as pd
import numpy as np
# In[2]:
df = pd.read_csv('train.csv',nrows=1000000)
# ## Explore and Analyse the DATA
# In[3]:
df.dtypes
# In[4]:
#We don't need such big datatypes to represent our dataset efficiently
types1 = {'fare_amount': 'float32',
'pickup_longitude': 'float32',
'pickup_latitude': 'float32',
'dropoff_longitude': 'float32',
'dropoff_latitude': 'float32',
'passenger_count': 'uint8'}
# In[5]:
df = df.astype(types1)
# In[6]:
df.dtypes
# In[7]:
df.head()
# #### Missing Value Treatment
# In[8]:
df.describe()
# In[9]:
#Check how many rows have null values
df.isnull().sum()
# In[10]:
#Drop null since it is negligible in this case
df.dropna(inplace=True)
# In[11]:
df.describe()
# #### Outlier treatment
# In[12]:
print(f"There are {len(df[df['fare_amount'] < 0])} negative fares.")
print(f"There are {len(df[df['fare_amount'] == 0])} $0 fares.")
print(f"There are {len(df[df['fare_amount'] > 100])} fares greater than $100.")
# In[13]:
df = df[(df['fare_amount'] > 0) & (df['fare_amount'] < 100)]
# In[14]:
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# In[15]:
df['passenger_count'].value_counts().plot.bar(color = 'b', edgecolor = 'k')
plt.title('Passenger Counts'); plt.xlabel('Number of Passengers'); plt.ylabel('Count')
# In[16]:
df = df[(df['passenger_count'] > 0) & (df['passenger_count'] < 6)]
# In[17]:
for col in ['pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude']:
print(f'PERCENTILES OF {col.capitalize():17}: 2.5% = {round(np.percentile(df[col], 2.5), 2):5}\t\t97.5% = {round(np.percentile(df[col], 97.5), 2)}\n')
# In[18]:
df = df.loc[df['pickup_latitude'].between(40, 44)]
df = df.loc[df['pickup_longitude'].between(-75, -72)]
df = df.loc[df['dropoff_latitude'].between(40, 44)]
df = df.loc[df['dropoff_longitude'].between(-75, -72)]
# In[19]:
df.describe()
# ## Feature Engineering
# In[20]:
from math import sin, cos, sqrt, atan2, radians
# In[21]:
#Get useable date for feature engineering
df['pickup_datetime'] = df['pickup_datetime'].str.replace(" UTC", "")
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], format='%Y-%m-%d %H:%M:%S')
# #### From Timestamp we can get below new features
# In[22]:
#Getting interger numbers from the pickup_datetime
df["hour"] = df.pickup_datetime.dt.hour
df["weekday"] = df.pickup_datetime.dt.weekday
df["month"] = df.pickup_datetime.dt.month
df["year"] = df.pickup_datetime.dt.year
# #### Distance is aslo another crucial attribute
# In[23]:
#Quicker but slightly less accurate
def dist_calc(df):
R = 6373.0
for i,row in df.iterrows():
lat1 = radians(row['pickup_latitude'])
lon1 = radians(row['pickup_longitude'])
lat2 = radians(row['dropoff_latitude'])
lon2 = radians(row['dropoff_longitude'])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
df.at[i,'distance'] = distance
# In[24]:
dist_calc(df)
# #### Another concept Hotspot proximity can serve as useful feature
# In[25]:
#Function for distance calculation between coordinates as mapped variables
def sphere_dist(pickup_lat, pickup_lon, dropoff_lat, dropoff_lon):
#Define earth radius (km)
R_earth = 6371
#Convert degrees to radians
pickup_lat, pickup_lon, dropoff_lat, dropoff_lon = map(np.radians,
[pickup_lat, pickup_lon,
dropoff_lat, dropoff_lon])
#Compute distances along lat, lon dimensions
dlat = dropoff_lat - pickup_lat
dlon = dropoff_lon - pickup_lon
#Compute haversine distance
a = np.sin(dlat/2.0)**2 + np.cos(pickup_lat) * np.cos(dropoff_lat) * np.sin(dlon/2.0)**2
return 2 * R_earth * np.arcsin(np.sqrt(a))
# In[26]:
#Function for calculating distance between newly obtained distances from the hotspots.
def add_airport_dist(dataset):
jfk_coord = (40.639722, -73.778889)
ewr_coord = (40.6925, -74.168611)
lga_coord = (40.77725, -73.872611)
pickup_lat = dataset['pickup_latitude']
dropoff_lat = dataset['dropoff_latitude']
pickup_lon = dataset['pickup_longitude']
dropoff_lon = dataset['dropoff_longitude']
pickup_jfk = sphere_dist(pickup_lat, pickup_lon, jfk_coord[0], jfk_coord[1])
dropoff_jfk = sphere_dist(jfk_coord[0], jfk_coord[1], dropoff_lat, dropoff_lon)
pickup_ewr = sphere_dist(pickup_lat, pickup_lon, ewr_coord[0], ewr_coord[1])
dropoff_ewr = sphere_dist(ewr_coord[0], ewr_coord[1], dropoff_lat, dropoff_lon)
pickup_lga = sphere_dist(pickup_lat, pickup_lon, lga_coord[0], lga_coord[1])
dropoff_lga = sphere_dist(lga_coord[0], lga_coord[1], dropoff_lat, dropoff_lon)
dataset['jfk_dist'] = pd.concat([pickup_jfk, dropoff_jfk], axis=1).min(axis=1)
dataset['ewr_dist'] = pd.concat([pickup_ewr, dropoff_ewr], axis=1).min(axis=1)
dataset['lga_dist'] = pd.concat([pickup_lga, dropoff_lga], axis=1).min(axis=1)
return dataset
# In[27]:
#Run the functions to add the features to the dataset
df = add_airport_dist(df)
# In[28]:
df.dtypes
# In[29]:
#We don't need such big datatypes to represent our dataset efficiently plus it is computationally costly
types2 = {'hour': 'uint8',
'weekday': 'uint8',
'month': 'uint8',
'year': 'uint8',
'distance': 'float32'}
# In[30]:
df = df.astype(types2)
df.dtypes
# In[31]:
df.head()
# ## Attribute selection for regression
# In[32]:
import seaborn as sns
# #### Correlation between attributes
# In[33]:
#Plot heatmap of value correlations
plt.figure(figsize=(15,8))
sns.heatmap(df.drop(['key','pickup_datetime'],axis=1).corr(),annot=True,fmt='.4f')
# #### Scatter plot of 1K records
# In[34]:
d = df[['fare_amount','distance','jfk_dist','lga_dist']]
d = d[:1000]
# In[35]:
sns.set(style='whitegrid', context = 'notebook')
sns.pairplot(d,size=2.5)
# In[36]:
X = df[['distance','jfk_dist']]
y = df[['fare_amount']]
# In[37]:
from sklearn.model_selection import train_test_split
# In[38]:
X_training, X_testing, y_training, y_testing = train_test_split(X, y, test_size=0.2, random_state=42)
# ### Ordinary least squares
# In[39]:
w_OLS = np.matmul(np.matmul(np.linalg.inv(np.matmul(X_training.T, X_training)), X_training.T), y_training)
print(w_OLS)
# In[40]:
y_predict = np.matmul(X_testing, w_OLS).round(decimals = 2)
# In[41]:
from sklearn.metrics import mean_squared_error
# In[42]:
print('Mean Squared Error using Ordinary Least Square for two variables: %.2f' % mean_squared_error(y_testing, y_predict))
# In[43]:
y_predict1 = np.matmul(X_training, w_OLS).round(decimals = 2)
# In[44]:
print('Mean Squared Error using Ordinary Least Square for two variables ( Training Error ): %.2f' % mean_squared_error(y_training, y_predict1))
# ### Linear Regression
# In[45]:
from sklearn.linear_model import LinearRegression
# In[46]:
lr = LinearRegression()
lr_predict = lr.fit(X_training,y_training)
# In[47]:
lin_predict = lr.predict(X_testing)
print('Mean Squared Error using Linear Regression for two variables: %.2f' % mean_squared_error(y_testing, lin_predict))
# In[48]:
lr_predict.score(X_testing,y_testing)
# ## Now lets take it to other way around
# In[49]:
y_data = df[['fare_amount']]
X_data = df.drop(['key','fare_amount','pickup_datetime'],axis=1)
# In[50]:
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2, random_state=42)
# In[51]:
w_OLS = np.matmul(np.matmul(np.linalg.inv(np.matmul(X_train.T, X_train)), X_train.T), y_train)
print(w_OLS)
# In[52]:
y_pred = np.matmul(X_test, w_OLS).round(decimals = 2)
# In[53]:
print('Mean Squared Error using Ordinary Least Square for all possible attributes: %.2f' % mean_squared_error(y_test, y_pred))
# In[54]:
lr_predictions = lr.fit(X_train,y_train)
# In[55]:
linpred = lr.predict(X_test)
print('Mean Squared Error using Linear Regression for all possible attributes: %.2f' % mean_squared_error(y_test, linpred))
# In[56]:
lr_predictions.score(X_test,y_test)
# ### XGBoost
# In[57]:
import xgboost as xgb
# In[58]:
#Define a XGB model and parameters
def XGBoost(X_train,X_test,y_train,y_test):
dtrain = xgb.DMatrix(X_train,label=y_train)
dtest = xgb.DMatrix(X_test,label=y_test)
return xgb.train(params={'objective':'reg:linear','eval_metric':'mae'}
,dtrain=dtrain,num_boost_round=400,
early_stopping_rounds=30,evals=[(dtest,'test')])
# In[59]:
#Fit data and optimise the model, generate predictions
xgbm = XGBoost(X_train,X_test,y_train,y_test)
# In[60]:
XGBPredictions = xgbm.predict(xgb.DMatrix(X_test), ntree_limit = xgbm.best_ntree_limit)
# In[61]:
print('Mean Squared Error using XGBoost for all possible attributes: %.2f' % mean_squared_error(y_test, XGBPredictions))
# # Apparently ensemble approache (i.e. XGBoost) improved the performance over baseline approaches (i.e.Ordinary least squares and Linear Regression) significantly.