-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarter.py
58 lines (41 loc) · 1.46 KB
/
starter.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
#!/usr/bin/env python
# coding: utf-8
import pickle
import pandas as pd
import os
import sys
def read_data(filename):
df = pd.read_parquet(filename)
df['duration'] = df.tpep_dropoff_datetime - df.tpep_pickup_datetime
df['duration'] = df.duration.dt.total_seconds() / 60
df = df[(df.duration >= 1) & (df.duration <= 60)].copy()
categorical = ['PULocationID', 'DOLocationID']
df[categorical] = df[categorical].fillna(-1).astype('int').astype('str')
return df
def main(year, month):
input_file = f'https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_{year:04d}-{month:02d}.parquet'
output_file = f'output/{year:04d}-{month:02d}.parquet'
os.makedirs('output', exist_ok=True)
with open('model.bin', 'rb') as f_in:
dv, model = pickle.load(f_in)
categorical = ['PULocationID', 'DOLocationID']
df = read_data(input_file)
dicts = df[categorical].to_dict(orient='records')
X_val = dv.transform(dicts)
y_pred = model.predict(X_val)
# Create results dataframe
df_result = pd.DataFrame()
df_result['ride_id'] = f'{year:04d}/{month:02d}_' + df.index.astype('str')
df_result['preds'] = y_pred
df_result.to_parquet(
output_file,
engine='pyarrow',
compression=None,
index=False
)
return y_pred.mean()
if __name__ == '__main__':
year = int(sys.argv[1])
month = int(sys.argv[2])
pred_mean = main(year, month)
print(pred_mean)