-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLOps.py
473 lines (318 loc) · 15.5 KB
/
MLOps.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
# Databricks notebook source
# MAGIC %md #Gradient Boosted Trees Regression Model
# COMMAND ----------
# MAGIC %run ./includes/utilities
# COMMAND ----------
# MAGIC %run ./includes/configuration
# COMMAND ----------
from datetime import datetime as dt
from datetime import timedelta
dbutils.widgets.removeAll()
dbutils.widgets.dropdown("00.Airport_Code", "JFK", ["JFK","SEA","BOS","ATL","LAX","SFO","DEN","DFW","ORD","CVG","CLT","DCA","IAH"])
dbutils.widgets.text('01.training_start_date', "2018-01-01")
dbutils.widgets.text('02.training_end_date', "2019-03-15")
dbutils.widgets.text('03.inference_date', (dt.strptime(str(dbutils.widgets.get('02.training_end_date')), "%Y-%m-%d") + timedelta(days=1)).strftime("%Y-%m-%d"))
airport_code = str(dbutils.widgets.get('00.Airport_Code'))
training_start_date = str(dbutils.widgets.get('01.training_start_date'))
training_end_date = str(dbutils.widgets.get('02.training_end_date'))
inference_date = str(dbutils.widgets.get('03.inference_date'))
print(airport_code,training_start_date,training_end_date,inference_date)
# COMMAND ----------
databaseName = 'dscc202_group05_db'
silverDepDF = (spark
.readStream
.table('dscc202_group05_db.silverdep_delta'))
# COMMAND ----------
silverDepDF_1 = spark.sql("""
SELECT *
FROM {0}.silverdep_delta
WHERE FL_DATE BETWEEN '{1}' AND '{2}'
""".format(GROUP_DBNAME,training_start_date,training_end_date))
silverDepDF_1.cache()
# COMMAND ----------
silverArrDF_1 = spark.sql("""
SELECT *
FROM {0}.silverarr_delta
WHERE FL_DATE BETWEEN '{1}' AND '{2}'
""".format(GROUP_DBNAME,training_start_date,training_end_date))
silverArrDF_1.cache()
display(silverArrDF_1)
# COMMAND ----------
silverDepDF.printSchema()
# COMMAND ----------
print("The dep. dataset has %d rows." % silverDepDF_1.count())
print("The arr. dataset has %d rows." % silverArrDF_1.count())
# COMMAND ----------
display(silverDepDF_1)
# COMMAND ----------
from pyspark.sql.functions import col
silverDepDF_1 = silverDepDF_1.filter(col('DEP_DELAY').isNotNull())
silverDepDF_1 = silverDepDF_1.filter(col('ARR_DELAY').isNotNull())
# COMMAND ----------
silverArrDF_1 = silverArrDF_1.filter(col('DEP_DELAY').isNotNull())
silverArrDF_1 = silverArrDF_1.filter(col('ARR_DELAY').isNotNull())
# COMMAND ----------
from pyspark.sql.functions import col,sum
silverDepDF_1.select(*(sum(col(c).isNull().cast("int")).alias(c) for c in silverDepDF_1.columns)).show()
# COMMAND ----------
from pyspark.sql.functions import col,sum
silverArrDF_1.select(*(sum(col(c).isNull().cast("int")).alias(c) for c in silverArrDF_1.columns)).show()
# COMMAND ----------
#https://datascience.stackexchange.com/questions/72408/remove-all-columns-where-the-entire-column-is-null
def drop_null_columns(df):
"""
This function drops columns containing all null values.
:param df: A PySpark DataFrame
"""
null_counts = df.select([sqlf.count(sqlf.when(sqlf.col(c).isNull(), c)).alias(c) for c in df.columns]).collect()[0].asDict()
to_drop = [k for k, v in null_counts.items() if v >= df.count()]
df = df.drop(*to_drop)
return df
# COMMAND ----------
import pandas as pd
import numpy as np
import pyspark.sql.functions as sqlf
silverDepDF_1 = drop_null_columns(silverDepDF_1)
silverArrDF_1 = drop_null_columns(silverArrDF_1)
# COMMAND ----------
silverDepDF_1.na.drop()
print("The Dep. dataset has %d rows." % silverDepDF_1.count())
silverArrDF_1.na.drop()
print("The Arr. dataset has %d rows." % silverArrDF_1.count())
# COMMAND ----------
silverDepDF_1=silverDepDF_1.drop('FL_DATE')
silverArrDF_1=silverArrDF_1.drop('FL_DATE')
# COMMAND ----------
print("The dep. dataset has %d rows." % silverDepDF_1.count())
print("The arr. dataset has %d rows." % silverArrDF_1.count())
# COMMAND ----------
from pyspark.ml.regression import LinearRegression
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.evaluation import RegressionEvaluator
from pyspark.ml.feature import VectorAssembler, VectorIndexer
featuresCols = silverDepDF_1.columns
featuresCols.remove('DEP_DELAY')
# vectorAssembler combines all feature columns into a single feature vector column, "rawFeatures".
vectorAssembler = VectorAssembler(inputCols=featuresCols, outputCol="rawFeatures")
#vectorAssembler=vectorAssembler.transform(silverDepDF_1.na.drop).show(20)
# vectorIndexer identifies categorical features and indexes them, and creates a new column "features".
vectorIndexer = VectorIndexer(inputCol="rawFeatures", outputCol="features", maxCategories=4)
# COMMAND ----------
from pyspark.ml.regression import GBTRegressor
# The next step is to define the model training stage of the pipeline.
# The following command defines a GBTRegressor model that takes an input column "features" by default and learns to predict the labels in the "DEP_DELAY" column.
gbt = GBTRegressor(labelCol="DEP_DELAY")
# COMMAND ----------
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import RegressionEvaluator
# Define a grid of hyperparameters to test:
# - maxDepth: maximum depth of each decision tree
# - maxIter: iterations, or the total number of trees
paramGrid = ParamGridBuilder()\
.addGrid(gbt.maxDepth, [2, 5])\
.addGrid(gbt.maxIter, [10, 100])\
.build()
# Define an evaluation metric. The CrossValidator compares the true labels with predicted values for each combination of parameters, and calculates this value to determine the best model.
evaluator = RegressionEvaluator(metricName="rmse", labelCol=gbt.getLabelCol(), predictionCol=gbt.getPredictionCol())
# Declare the CrossValidator, which performs the model tuning.
cv = CrossValidator(estimator=gbt, evaluator=evaluator, estimatorParamMaps=paramGrid)
# COMMAND ----------
from pyspark.ml import Pipeline
pipeline = Pipeline(stages=[vectorAssembler, vectorIndexer, cv])
# COMMAND ----------
# Split the dataset randomly into 70% for training and 30% for testing. Passing a seed for deterministic behavior
train, test = silverDepDF_1.randomSplit([0.7, 0.3], seed = 0)
print("Departure Delay: There are %d training examples and %d test examples." % (train.count(), test.count()))
# COMMAND ----------
from mlflow.models.signature import infer_signature
signature = infer_signature(train.drop("DEP_DELAY"), train.select("DEP_DELAY"))
# COMMAND ----------
signature
# COMMAND ----------
display(silverDepDF_1)
# COMMAND ----------
import mlflow
import mlflow.spark
from mlflow.models.signature import infer_signature
# turn on autologging
mlflow.autolog(log_input_examples=True,log_model_signatures=True,log_models=True)
experiment_name = '/Users/[email protected]/flight_delay/dscc202_group05_experiment'
mlflow.set_experiment(experiment_name)
with mlflow.start_run(experiment_id=70892):
#with mlflow.start_run(experiment_id=experimentID, run_name=run_name) as run:
pipelineModel = pipeline.fit(train)
# Log the best model.
mlflow.spark.log_model(spark_model=pipelineModel, artifact_path='best-model-dep-newfeatures',signature=signature, input_example=train.drop("DEP_DELAY").toPandas().head())
# COMMAND ----------
predictions_dep = pipelineModel.transform(test)
# COMMAND ----------
rmse = evaluator.evaluate(predictions_dep)
print("RMSE on our test set: %g" % rmse)
# COMMAND ----------
display(predictions_dep.select("DEP_DELAY", "prediction", *featuresCols))
# COMMAND ----------
from pyspark.ml.feature import VectorAssembler, VectorIndexer
featuresCols = silverArrDF_1.columns
featuresCols.remove('ARR_DELAY')
# vectorAssembler combines all feature columns into a single feature vector column, "rawFeatures".
vectorAssembler = VectorAssembler(inputCols=featuresCols, outputCol="rawFeatures")
#vectorAssembler=vectorAssembler.transform(silverDepDF_1.na.drop).show(20)
# vectorIndexer identifies categorical features and indexes them, and creates a new column "features".
vectorIndexer = VectorIndexer(inputCol="rawFeatures", outputCol="features", maxCategories=4)
# COMMAND ----------
from pyspark.ml.regression import GBTRegressor
# The next step is to define the model training stage of the pipeline.
# The following command defines a GBTRegressor model that takes an input column "features" by default and learns to predict the labels in the "DEP_DELAY" column.
gbt = GBTRegressor(labelCol="ARR_DELAY")
# COMMAND ----------
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
from pyspark.ml.evaluation import RegressionEvaluator
# Define a grid of hyperparameters to test:
# - maxDepth: maximum depth of each decision tree
# - maxIter: iterations, or the total number of trees
paramGrid = ParamGridBuilder()\
.addGrid(gbt.maxDepth, [2, 5])\
.addGrid(gbt.maxIter, [10, 100])\
.build()
# Define an evaluation metric. The CrossValidator compares the true labels with predicted values for each combination of parameters, and calculates this value to determine the best model.
evaluator = RegressionEvaluator(metricName="rmse", labelCol=gbt.getLabelCol(), predictionCol=gbt.getPredictionCol())
# Declare the CrossValidator, which performs the model tuning.
cv = CrossValidator(estimator=gbt, evaluator=evaluator, estimatorParamMaps=paramGrid)
# COMMAND ----------
from pyspark.ml import Pipeline
pipeline = Pipeline(stages=[vectorAssembler, vectorIndexer, cv])
# COMMAND ----------
# Split the dataset randomly into 70% for training and 30% for testing. Passing a seed for deterministic behavior
train, test = silverArrDF_1.randomSplit([0.7, 0.3], seed = 0)
print("Arrival Delay: There are %d training examples and %d test examples." % (train.count(), test.count()))
# COMMAND ----------
from mlflow.models.signature import infer_signature
signature = infer_signature(train.drop("ARR_DELAY"), train.select("ARR_DELAY"))
# COMMAND ----------
signature
# COMMAND ----------
import mlflow
import mlflow.spark
# turn on autologging
mlflow.autolog(log_input_examples=True,log_model_signatures=True,log_models=True)
experiment_name = '/Users/[email protected]/flight_delay/dscc202_group05_experiment'
mlflow.set_experiment(experiment_name)
with mlflow.start_run(experiment_id=70892):
pipelineModel = pipeline.fit(train)
# Log the best model.
#mlflow.set_experiment(experiment_name: '/databricks/mlflow-tracking/70892/')
mlflow.spark.log_model(spark_model=pipelineModel, artifact_path='best-model-arr-newfeatures', signature=signature, input_example=train.drop("ARR_DELAY").toPandas().head())
# COMMAND ----------
predictions_arr = pipelineModel.transform(test)
# COMMAND ----------
rmse = evaluator.evaluate(predictions_arr)
print("RMSE on our test set: %g" % rmse)
# COMMAND ----------
display(predictions_arr.select("ARR_DELAY", "prediction", *featuresCols))
# COMMAND ----------
# MAGIC %md
# MAGIC #Baseline Model
# COMMAND ----------
import pandas as pd
from datetime import datetime
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, explained_variance_score, r2_score
import matplotlib.pyplot as plt
import pyspark.sql.functions as sqlf
from pyspark.sql.functions import col, to_date
# COMMAND ----------
# Load Departure Dataframe
#silverArrDF_2 = spark.sql("""
# SELECT *
# FROM {}.silverarr_delta
# WHERE FL_DATE
# """.format(GROUP_DBNAME))
silverArrDF_2 = spark.sql("""
SELECT *
FROM {0}.silverdep_delta
WHERE FL_DATE BETWEEN '{1}' AND '{2}'
""".format(GROUP_DBNAME,training_start_date,training_end_date))
# Clean Arrival Data
silverArrDF_2 = silverArrDF_2.filter(col('ARR_DELAY').isNotNull())
silverArrDF_2 = silverArrDF_2.filter(col('DEP_DELAY').isNotNull())
silverArrDF_2 = silverArrDF_2.drop('FL_DATE')
silverArrDF_2 = drop_null_columns(silverArrDF_2)
#Transform into Pandas Dataframe
silverArrDF_2 = silverArrDF_2.toPandas()
display(silverArrDF_2.head())
# COMMAND ----------
# Load Departure Dataframe
#silverDepDF_2 = spark.sql("""
# SELECT *
# FROM {}.silverdep_delta
# """.format(GROUP_DBNAME))
silverDepDF_2 = spark.sql("""
SELECT *
FROM {0}.silverarr_delta
WHERE FL_DATE BETWEEN '{1}' AND '{2}'
""".format(GROUP_DBNAME,training_start_date,training_end_date))
# Clean Departure Data
silverDepDF_2 = silverDepDF_2.filter(col('ARR_DELAY').isNotNull())
silverDepDF_2 = silverDepDF_2.filter(col('DEP_DELAY').isNotNull())
silverDepDF_2 = silverDepDF_2.drop('FL_DATE')
#silverDepDF_1 = idToAbbr(silverDepDF_1)
silverDepDF_2 = drop_null_columns(silverDepDF_2)
#Transform into Pandas Dataframe
silverDepDF_2 = silverDepDF_2.toPandas()
display(silverDepDF_2.head())
# COMMAND ----------
# Split datasets into features and targets
arrivalFeats = silverArrDF_2.drop('ARR_DELAY', axis=1)
arrivalTarget = pd.Series.to_frame(silverArrDF_2["ARR_DELAY"])
departFeats = silverDepDF_2.drop('DEP_DELAY', axis=1)
departTarget = pd.Series.to_frame(silverDepDF_2["DEP_DELAY"])
arrivalTrain_x, arrivalTest_x, arrivalTrain_y, arrivalTest_y = train_test_split(arrivalFeats, arrivalTarget, test_size=.25, random_state=0, shuffle=True)
departTrain_x, departTest_x, departTrain_y, departTest_y = train_test_split(departFeats, departTarget, test_size=.25, random_state=0, shuffle=True)
# COMMAND ----------
group05_arrBaselineModel = LinearRegression().fit(arrivalTrain_x, arrivalTrain_y)
group05_depBaselineModel = LinearRegression().fit(departTrain_x, departTrain_y)
# COMMAND ----------
arrivalPredictions = group05_arrBaselineModel.predict(arrivalTest_x)
arrivalRMSE = mean_squared_error(arrivalTest_y, arrivalPredictions, squared=False)
arrivalEVS = explained_variance_score(arrivalTest_y, arrivalPredictions)
arrivalR2 = r2_score(arrivalTest_y, arrivalPredictions)
print("Arrival Dataset:\n Root Mean Squared Error: {0}\n Explained Variance: {1}\n R^2 Score: {2}".format(arrivalRMSE, arrivalEVS, arrivalR2))
departPredictions = group05_arrBaselineModel.predict(departTest_x)
departRMSE = mean_squared_error(departTest_y, departPredictions, squared=False)
departEVS = explained_variance_score(departTest_y, departPredictions)
departR2 = r2_score(departTest_y, departPredictions)
print("Departure Dataset:\n Root Mean Squared Error: {0}\n Explained Variance: {1}\n R^2 Score: {2}".format(departRMSE, departEVS, departR2))
# COMMAND ----------
import uuid
with mlflow.start_run(run_name="Group5 Arrival Linear Model") as run:
mlflow.sklearn.log_model(group05_arrBaselineModel, "g05_Arr_LinearModel")
mlflow.log_metric("rmse", arrivalRMSE)
mlflow.log_metric("expVar", arrivalEVS)
mlflow.log_metric("r2", arrivalR2)
g05_Arr_LinearModel_runID = run.info.run_uuid
g05_Arr_LinearModel_name = f"g05_Arr_LinearModel"
g05_Arr_LinearModel_uri = "runs:/{run_id}/g05_Arr_LinearModel".format(run_id=g05_Arr_LinearModel_runID)
g05_Arr_LinearModel_details = mlflow.register_model(model_uri=g05_Arr_LinearModel_uri, name=g05_Arr_LinearModel_name)
#g05ArrLinearModel_name
# COMMAND ----------
with mlflow.start_run(run_name="Group5 Departure Linear Model") as run:
mlflow.sklearn.log_model(group05_depBaselineModel, "g05_Dep_LinearModel")
mlflow.log_metric("rmse", departRMSE)
mlflow.log_metric("expVar", departEVS)
mlflow.log_metric("r2", departR2)
g05_Dep_LinearModel_runID = run.info.run_uuid
g05_Dep_LinearModel_name = f"g05_Dep_LinearModel"
g05_Dep_LinearModel_uri = "runs:/{run_id}/g05_Dep_LinearModel".format(run_id=g05_Dep_LinearModel_runID)
g05_Dep_LinearModel_details = mlflow.register_model(model_uri=g05_Dep_LinearModel_uri, name=g05_Dep_LinearModel_name)
# COMMAND ----------
# MAGIC %md
# MAGIC ####Model Deletion Code
# COMMAND ----------
#from mlflow.tracking import MlflowClient
#client = MlflowClient()
#client.delete_registered_model(name="g05_Arr_LinearModel_edf53e361a")
# COMMAND ----------
import json
dbutils.notebook.exit("Success")