-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFare Prediction.py
729 lines (383 loc) · 18 KB
/
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/usr/bin/env python
# coding: utf-8
# ## Import the libraries
# In[ ]:
import warnings
import numpy as np #Exploratory Data Analysis
import pyspark.pandas as ps
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from plotly.offline import iplot
from random import random
from pyspark.ml import Pipeline #Modeling
from pyspark.ml.regression import LinearRegression, DecisionTreeRegressor, RandomForestRegressor, GBTRegressor
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.evaluation import RegressionEvaluator
import mlflow #Deployment
import mlflow.sklearn
import mlflow.azureml
import azureml
from azureml.core import Workspace
from azureml.core.authentication import InteractiveLoginAuthentication
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# ### Mount the Azure Storage
# In[ ]:
dbutils.fs.mount(
source = "wasbs://dataset@pricedatastorage.blob.core.windows.net",
mount_point = "/mnt/data",
extra_configs = {"fs.azure.account.key.pricedatastorage.blob.core.windows.net":"UUpMhyQ6jNC4fFy2QbYKaomV+hJAXB3P1oSxDP9jQyXIpBc1ljzZb4wAZQ4U7eyTYphlEHso+4tj+AStbx1GPQ=="})
# **Import the dataset**
# In[ ]:
df = spark.read.csv('/mnt/data/train.csv',inferSchema=True,header=True)
test_df = spark.read.csv('/mnt/data/test.csv',inferSchema=True,header=True)
# **Convert spark dataframe into pandas dataframe**
# In[ ]:
df = df.to_pandas_on_spark()
df = df.to_pandas()
# In[ ]:
test_df = test_df.to_pandas_on_spark()
test_df = test_df.to_pandas()
# In[ ]:
df.display()
# In[ ]:
df.shape
# In[ ]:
df.describe()
# In[ ]:
df.info()
# **Convert object datatype to datetime64[ns]**
# In[ ]:
df.key = df.key.astype('datetime64[ns]')
df.pickup_datetime = df.pickup_datetime.astype('datetime64[ns]')
test_df.key = test_df.key.astype('datetime64[ns]')
test_df.pickup_datetime = test_df.pickup_datetime.astype('datetime64[ns]')
# **Check and drop the null values**
# In[ ]:
df.isna().sum().sort_values(ascending=False)
# In[ ]:
df.dropna(inplace=True)
# **Check and drop the records which have negative fare amount**
# In[ ]:
df[df.fare_amount < 0].fare_amount
# In[ ]:
df.drop(df[df.fare_amount < 0].index, axis = 0, inplace = True)
# **Check and drop the records which have passenger count more than 6**
# In[ ]:
df[df.passenger_count > 6]
# In[ ]:
df.drop(df[df.passenger_count > 6].index, axis = 0, inplace = True)
# **Check the standard range of latitude and longitude, if value bound the range then drop**
# In[ ]:
df.pickup_latitude.between(-90,90,inclusive=True).sum()
# In[ ]:
df.drop(df[df.pickup_latitude < -90].index, axis = 0, inplace = True)
df.drop(df[df.pickup_latitude > 90].index, axis = 0, inplace = True)
# In[ ]:
df.dropoff_latitude.between(-90,90,inclusive=True).sum()
df.drop(df[df.dropoff_latitude < -90].index, axis = 0, inplace = True)
df.drop(df[df.dropoff_latitude > 90].index, axis = 0, inplace = True)
# In[ ]:
df.pickup_longitude.between(-180,180,inclusive=True).sum()
df.drop(df[df.pickup_longitude < -180].index, axis = 0, inplace = True)
df.drop(df[df.pickup_longitude > 180].index, axis = 0, inplace = True)
# In[ ]:
df.dropoff_longitude.between(-180,180,inclusive=True).sum()
df.drop(df[df.dropoff_longitude < -180].index, axis = 0, inplace = True)
df.drop(df[df.dropoff_longitude > 180].index, axis = 0, inplace = True)
# **Function to calculate distance using haversine formula**
# - Distance is calculated in miles.
# In[ ]:
def distance(data, lat1, long1, lat2, long2):
x = np.sin(np.radians(df[lat2]-df[lat1])/2.0) ** 2 + np.cos(np.radians(df[lat1])) * np.cos(np.radians(df[lat2])) * \
np.sin(np.radians(df[long2]-df[long1])/2.0) ** 2
y = 2 * np.arctan2(np.sqrt(x), np.sqrt(1 - x))
data['distance'] = 3959 * y
pass
# In[ ]:
distance(df, 'pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude')
# **Feature scaling of pickup datetime**
# In[ ]:
df['year'] = df.pickup_datetime.dt.year
df['month'] = df.pickup_datetime.dt.month
df['date'] = df.pickup_datetime.dt.day
df['day_of_week'] = df.pickup_datetime.dt.dayofweek
df['hour'] = df.pickup_datetime.dt.hour
# ## Data Visualization
# **Histogram of fare amount**
# In[ ]:
Hist = [go.Histogram(x=df.fare_amount[:1000000])]
layout = go.Layout(title = 'Fare Amount')
fig = go.Figure(data = Hist, layout = layout)
iplot(fig)
# **Scatterplot of passenger count and fare amount**
# In[ ]:
plt.figure(figsize=(15,7))
plt.scatter(x=df.passenger_count, y=df.fare_amount)
plt.xlabel('Passenger Count')
plt.ylabel('Fare')
# **Scatterplot of date and fare amount**
# In[ ]:
plt.figure(figsize=(15,7))
plt.scatter(x=df.date, y=df.fare_amount, s=1.5)
plt.xlabel('Date')
plt.ylabel('Fare')
# **Scatterplot of hour and fare**
# In[ ]:
plt.figure(figsize=(15,7))
plt.scatter(x=df.hour, y=df.fare_amount)
plt.xlabel('Hour')
plt.ylabel('Fare')
# **Scatterplot of fare amount vs day of week**
# In[ ]:
plt.figure(figsize=(15,7))
plt.scatter(x=df.day_of_week, y=df.fare_amount, s=1.5)
plt.xlabel('Day of Week')
plt.ylabel('Fare')
# **Relation between distance and fare**
# In[ ]:
plt.figure(figsize=(16,10))
plt.scatter(x=df.distance, y=df.fare_amount, s=1.5)
plt.xlabel('Distance')
plt.ylabel('Fare')
# **Distribution of distance relative to fare and linear patterns in the distance**
# In[ ]:
fig, axs = plt.subplots(1, 2, figsize=(18,7))
axs[0].scatter(df.distance, df.fare_amount, alpha=0.2)
axs[0].set_xlabel('Distance')
axs[0].set_ylabel('Fare')
axs[0].set_title('All data')
idx = (df.distance < 10) & (df.fare_amount < 75)
axs[1].scatter(df[idx].distance, df[idx].fare_amount, alpha=0.2)
axs[1].set_xlabel('Distance')
axs[1].set_ylabel('Fare')
axs[1].set_title('Distance < 10 mile, fare < $75');
# ### Feature Engineering
# **Scenario**
#
# - Pickup latitude and longitude are 0, Dropoff latitude and longitude are not 0 but fare amount is 0
#
# - vice versa.
# In[ ]:
df.drop(df.loc[((df.pickup_latitude == 0) & (df.pickup_longitude == 0))&((df.dropoff_latitude != 0) & (df.dropoff_longitude != 0)) & (df.fare_amount == 0)].index, axis = 0, inplace = True)
# In[ ]:
df.drop(df.loc[((df.pickup_latitude != 0) & (df.pickup_longitude != 0))&((df.dropoff_latitude == 0) & (df.dropoff_longitude == 0)) & (df.fare_amount == 0)].index, axis = 0, inplace = True)
# **There are so many irrelevant records in the correlation of distance and fare.**
#
# So as per the statistics of the data available for the new york city, base fare for the taxi ride is `$`2.5 and average `$`1.56 per mile.
# > Fare = (Distance `*` 1.56) + 2.5
# **Check the records if the distance is above 200 and fare amount is not equal to 0**
# In[ ]:
distance_check = df.loc[(df.distance > 200)&(df.fare_amount != 0)]
distance_check
# **Update all the records with correct values if satisfy the above condition**
# In[ ]:
distance_check.distance = distance_check.apply(lambda x: (x.fare_amount - 2.50) / 1.56, axis = 1)
df.update(distance_check)
# **Check if both distance and fare are 0 then it should be dropped**
# In[ ]:
df[(df.distance == 0) & (df.fare_amount == 0)]
# In[ ]:
df.drop(df[(df.distance == 0) & (df.fare_amount == 0)].index, axis = 0, inplace = True)
# **Check the fare amount is less than the base amount in the working days then drop it**
# In[ ]:
df.loc[(((df.hour >= 6) & (df.hour <= 20)) & ((df.day_of_week >= 1) & (df.day_of_week <= 5)) & (df.distance == 0) & (df.fare_amount < 2.5))]
# In[ ]:
df.drop(df.loc[(((df.hour >= 6) & (df.hour <= 20)) & ((df.day_of_week >= 1) & (df.day_of_week <= 5)) &
(df.distance == 0) & (df.fare_amount < 2.5))].index, axis = 0, inplace = True)
# **Check and drop the records if fare is less than base amount on the weekends**
# In[ ]:
df.loc[((df.day_of_week == 0) | (df.day_of_week == 6)) & (df.distance == 0) & (df.fare_amount < 2.5)]
# In[ ]:
df.drop(df.loc[((df.day_of_week == 0) | (df.day_of_week == 6)) & (df.distance == 0) & (df.fare_amount < 2.5)].index, axis = 0, inplace = True)
# **Check if fare is 0 but distance is not 0 then impute it with mathematical calculation**
# In[ ]:
fare_check = df.loc[(df.fare_amount == 0) & (df.distance != 0)]
fare_check.sort_values('distance',ascending = False)
# In[ ]:
fare_check.fare_amount = fare_check.apply(lambda x: ((x.distance * 1.56) + 2.5), axis = 1)
df.update(fare_check)
# **Check if fare is above 3 but distance is equal to 0 then correct the distance according to the fare**
# In[ ]:
dist_check = df.loc[(df.fare_amount > 3) & (df.distance == 0)]
dist_check.sort_values('fare_amount',ascending = False)
# In[ ]:
dist_check.distance = dist_check.apply(lambda x: ((x.distance - 2.5) / 1.56), axis = 1)
df.update(dist_check)
# **Check if distance is above 100 but fare is still lower than 100, then fare should be imputed with correct values**
# In[ ]:
target_check = df.loc[(df.fare_amount < 100) & (df.distance > 100)]
target_check.sort_values(['distance','fare_amount'], ascending = False)
# In[ ]:
target_check.fare_amount = target_check.apply(lambda x: ((x.distance * 1.56) + 2.5), axis = 1)
df.update(target_check)
# **After all corrections, correlation of distance and fare amount should be linear and fair**
# In[ ]:
fig, ax = plt.subplots(1, 1, figsize=(16,10))
ax.scatter(x=df.distance, y=df.fare_amount, s=1.5)
ax.set_xlabel('Distance')
ax.set_ylabel('Fare')
ax.set_title('Fare Distribution with relation to distance')
# **Get the minimum and maximum range of the co-ordinates from the test dataset to plot the graph**
# In[ ]:
bb = (min(test_df.pickup_longitude.min(), test_df.dropoff_longitude.min()),
max(test_df.pickup_longitude.max(), test_df.dropoff_longitude.max()),
min(test_df.pickup_latitude.min(), test_df.dropoff_latitude.min()),
max(test_df.pickup_latitude.max(), test_df.dropoff_latitude.max()))
bb
# **Function filter the range of pickup and dropoff locations from the training dataset**
# In[ ]:
def filter_range(df, bb):
return (df.pickup_longitude >= bb[0]) & (df.pickup_longitude <= bb[1]) & \
(df.pickup_latitude >= bb[2]) & (df.pickup_latitude <= bb[3]) & \
(df.dropoff_longitude >= bb[0]) & (df.dropoff_longitude <= bb[1]) & \
(df.dropoff_latitude >= bb[2]) & (df.dropoff_latitude <= bb[3])
# **Plot of non-linear traffic in the city**
# In[ ]:
fig, axs = plt.subplots(1, 2, figsize=(24,12))
idx = filter_range(df, (-74.1, -73.7, 40.6, 40.9))
axs[0].set_title('Pickup Locations')
axs[0].scatter(df[idx].pickup_longitude, df[idx].pickup_latitude, c='r', s=0.01, alpha=0.5)
axs[1].set_title('Dropoff Locations')
axs[1].scatter(df[idx].dropoff_longitude, df[idx].dropoff_latitude, c='b', s=0.01, alpha=0.5)
# **Correlation map of features**
# In[ ]:
plt.figure(figsize=(15,12))
cor = df.corr()
sns.heatmap(cor, annot=True, cmap=plt.cm.CMRmap_r)
plt.show()
# **Prepare the test dataset**
# In[ ]:
distance(test_df, 'pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude')
# In[ ]:
test_df['year'] = test_df.pickup_datetime.dt.year
test_df['month'] = test_df.pickup_datetime.dt.month
test_df['date'] = test_df.pickup_datetime.dt.day
test_df['day_of_week'] = test_df.pickup_datetime.dt.dayofweek
test_df['hour'] = test_df.pickup_datetime.dt.hour
# **Drop the insignificant variables**
# In[ ]:
df.drop(['key','pickup_datetime'], axis = 1, inplace = True)
test_df.drop(['key','pickup_datetime'], axis = 1, inplace = True)
# **Convert pandas dataframe to spark dataframe for further modeling**
# In[ ]:
train_df = ps.from_pandas(df)
train_df = train_df.to_spark()
# ## Modeling
# **Linear Regression**
# In[ ]:
feature_assembler = VectorAssembler(inputCols=['pickup_longitude', 'dropoff_longitude', 'passenger_count', 'distance', 'year', 'month', 'date'], outputCol="features")
lr = LinearRegression(labelCol="fare_amount")
pipeline = Pipeline(stages=[feature_assembler, lr])
train, test = train_df.randomSplit([0.75, 0.25])
lr_model = pipeline.fit(train)
predictions = lr_model.transform(test)
evaluator = RegressionEvaluator(labelCol= 'fare_amount', predictionCol= 'prediction')
print('RMSE:', evaluator.evaluate(predictions, {evaluator.metricName: "rmse"}))
print('R-squared:', evaluator.evaluate(predictions, {evaluator.metricName: "r2"}))
# **Decision Tree**
# In[ ]:
feature_assembler = VectorAssembler(inputCols=['pickup_longitude', 'dropoff_longitude', 'passenger_count', 'distance', 'year', 'month', 'day_of_week', 'hour'], outputCol="features")
dt = DecisionTreeRegressor(labelCol="fare_amount")
pipeline = Pipeline(stages=[feature_assembler, dt])
train, test = train_df.randomSplit([0.75, 0.25])
dt_model = pipeline.fit(train)
predictions = dt_model.transform(test)
evaluator = RegressionEvaluator(labelCol= 'fare_amount', predictionCol= 'prediction')
print('RMSE:', evaluator.evaluate(predictions, {evaluator.metricName: "rmse"}))
print('R-squared:', evaluator.evaluate(predictions, {evaluator.metricName: "r2"}))
# **Random Forest**
# In[ ]:
feature_assembler = VectorAssembler(inputCols=['pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude', 'passenger_count', 'distance', 'year', 'month', 'date', 'hour'], outputCol="features")
rf = RandomForestRegressor(labelCol="fare_amount")
pipeline = Pipeline(stages=[feature_assembler, rf])
train, test = train_df.randomSplit([0.75, 0.25])
rf_model = pipeline.fit(train)
predictions = rf_model.transform(test)
evaluator = RegressionEvaluator(labelCol= 'fare_amount', predictionCol= 'prediction')
print('RMSE:', evaluator.evaluate(predictions, {evaluator.metricName: "rmse"}))
print('R-squared:', evaluator.evaluate(predictions, {evaluator.metricName: "r2"}))
# **Gradient Boosting**
# In[ ]:
feature_assembler = VectorAssembler(inputCols=['pickup_longitude', 'pickup_latitude', 'dropoff_longitude', 'dropoff_latitude', 'passenger_count', 'distance', 'year', 'month', 'date', 'day_of_week'], outputCol="features")
gbr = GBTRegressor(labelCol="fare_amount")
pipeline = Pipeline(stages=[feature_assembler, gbr])
train, test = train_df.randomSplit([0.75, 0.25])
gbr_model = pipeline.fit(train)
predictions = gbr_model.transform(test)
evaluator = RegressionEvaluator(labelCol= 'fare_amount', predictionCol= 'prediction')
print('RMSE:', evaluator.evaluate(predictions, {evaluator.metricName: "rmse"}))
print('R-squared:', evaluator.evaluate(predictions, {evaluator.metricName: "r2"}))
# ## Deployment
# **Create or load an Azure ML workspace**
# > It will load a workspace or create a new one if it does not exist.
# In[ ]:
workspace_name = "fare-prediction"
workspace_location = "eastus"
resource_group = "NYC_Price_Prediction"
subscription_id = "75b2e7f9-9c16-4ece-9685-e7db922c1b62"
authentication = InteractiveLoginAuthentication(tenant_id = "bc7e12a8-5234-4447-9371-44fc05a1d39c")
workspace = Workspace.create(name = workspace_name,
location = workspace_location,
resource_group = resource_group,
subscription_id = subscription_id,
auth = authentication,
exist_ok = True)
# **Train the best performance model on the dataset using MLflow to log metrics, parameters, artifacts and model**
# In[ ]:
def fare_training(df, alpha, n_estimators):
warnings.filterwarnings("ignore")
if float(alpha) is None:
alpha = 0.9
else:
alpha = float(alpha)
if int(n_estimators) is None:
n_estimators = 100
else:
n_estimators = int(n_estimators)
np.random.seed(42)
train, test = train_test_split(df)
X_train = train.drop(["fare_amount"], axis=1)
X_test = test.drop(["fare_amount"], axis=1)
y_train = train[["fare_amount"]]
y_test = test[["fare_amount"]]
def evaluation_metrics(true, pred):
return np.sqrt(mean_squared_error(true, pred)), mean_absolute_error(true, pred), r2_score(true, pred)
with mlflow.start_run() as run:
gbr = GradientBoostingRegressor(alpha=alpha, n_estimators = n_estimators, random_state = 42)
gbr.fit(X_train, y_train)
predictions = gbr.predict(X_test)
(rmse, mae, r2) = evaluation_metrics(y_test, predictions)
print("GradientBoostingRegressor(alpha=%f, n_estimators=%f):" % (alpha, n_estimators))
print(" RMSE: %s" % rmse)
print(" MAE: %s" % mae)
print(" R2 Score: %s" % r2)
mlflow.log_param("alpha", alpha)
mlflow.log_param("n_estimators", n_estimators)
mlflow.log_metric("rmse", rmse)
mlflow.log_metric("mae", mae)
mlflow.log_metric("r2", r2)
mlflow.sklearn.log_model(gbr, "model")
temp = int(random()*1000)
path = "/dbfs/mlflow/fare_test/model-%f-%f-%f" % (alpha, n_estimators, temp)
mlflow.sklearn.save_model(gbr, path)
run_id = run.info.run_id
print('Run ID: ', run_id)
model_uri = "runs:/" + run_id + "/model"
print('model_uri: ', model_uri)
return run_id, model_uri
# In[ ]:
run_id, model_uri = fare_training(df, 0.01, 80)
# **Create a container image for trained model to deploy in Azure Container Instances(ACI) using MLflow**
#
# Further as per the requirement container image can be deployed to ACI for staging which serve as REST endpoint and Azure Kubernetes Service(AKS) for production.
# In[ ]:
model_image, azure_model = mlflow.azureml.build_image(model_uri = model_uri,
workspace = workspace,
model_name = "gbr-model",
image_name = "gbr-model",
description="Gradient Boosting Regressor for fare prediction",
synchronous=False)
model_image.wait_for_creation(show_output=True)