forked from DataTalksClub/mlops-zoomcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore.py
146 lines (97 loc) · 3.67 KB
/
score.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
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import uuid
import pickle
from datetime import datetime
import pandas as pd
import mlflow
from prefect import task, flow, get_run_logger
from prefect.context import get_run_context
from dateutil.relativedelta import relativedelta
from sklearn.feature_extraction import DictVectorizer
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
def generate_uuids(n):
ride_ids = []
for i in range(n):
ride_ids.append(str(uuid.uuid4()))
return ride_ids
def read_dataframe(filename: str):
df = pd.read_parquet(filename)
df['duration'] = df.lpep_dropoff_datetime - df.lpep_pickup_datetime
df.duration = df.duration.dt.total_seconds() / 60
df = df[(df.duration >= 1) & (df.duration <= 60)]
df['ride_id'] = generate_uuids(len(df))
return df
def prepare_dictionaries(df: pd.DataFrame):
categorical = ['PULocationID', 'DOLocationID']
df[categorical] = df[categorical].astype(str)
df['PU_DO'] = df['PULocationID'] + '_' + df['DOLocationID']
categorical = ['PU_DO']
numerical = ['trip_distance']
dicts = df[categorical + numerical].to_dict(orient='records')
return dicts
def load_model(run_id):
logged_model = f's3://mlflow-models-alexey/1/{run_id}/artifacts/model'
model = mlflow.pyfunc.load_model(logged_model)
return model
def save_results(df, y_pred, run_id, output_file):
df_result = pd.DataFrame()
df_result['ride_id'] = df['ride_id']
df_result['lpep_pickup_datetime'] = df['lpep_pickup_datetime']
df_result['PULocationID'] = df['PULocationID']
df_result['DOLocationID'] = df['DOLocationID']
df_result['actual_duration'] = df['duration']
df_result['predicted_duration'] = y_pred
df_result['diff'] = df_result['actual_duration'] - df_result['predicted_duration']
df_result['model_version'] = run_id
df_result.to_parquet(output_file, index=False)
@task
def apply_model(input_file, run_id, output_file):
logger = get_run_logger()
logger.info(f'reading the data from {input_file}...')
df = read_dataframe(input_file)
dicts = prepare_dictionaries(df)
logger.info(f'loading the model with RUN_ID={run_id}...')
model = load_model(run_id)
logger.info(f'applying the model...')
y_pred = model.predict(dicts)
logger.info(f'saving the result to {output_file}...')
save_results(df, y_pred, run_id, output_file)
return output_file
def get_paths(run_date, taxi_type, run_id):
prev_month = run_date - relativedelta(months=1)
year = prev_month.year
month = prev_month.month
input_file = f's3://nyc-tlc/trip data/{taxi_type}_tripdata_{year:04d}-{month:02d}.parquet'
output_file = f's3://nyc-duration-prediction-alexey/taxi_type={taxi_type}/year={year:04d}/month={month:02d}/{run_id}.parquet'
return input_file, output_file
@flow
def ride_duration_prediction(
taxi_type: str,
run_id: str,
run_date: datetime = None):
if run_date is None:
ctx = get_run_context()
run_date = ctx.flow_run.expected_start_time
input_file, output_file = get_paths(run_date, taxi_type, run_id)
apply_model(
input_file=input_file,
run_id=run_id,
output_file=output_file
)
def run():
taxi_type = sys.argv[1] # 'green'
year = int(sys.argv[2]) # 2021
month = int(sys.argv[3]) # 3
run_id = sys.argv[4] # 'e1efc53e9bd149078b0c12aeaa6365df'
ride_duration_prediction(
taxi_type=taxi_type,
run_id=run_id,
run_date=datetime(year=year, month=month, day=1)
)
if __name__ == '__main__':
run()