/
VehicleStops.py
398 lines (339 loc) · 12.9 KB
/
VehicleStops.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
import os
import io
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import *
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.preprocessing import FunctionTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
# ---------------------------------------------------------------------
# Question # 1
# ---------------------------------------------------------------------
def create_cat_feats():
"""
:Example:
>>> pl = create_cat_feats()
>>> isinstance(pl, Pipeline)
True
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)[['subject_sex']]
>>> pl.fit(stops) # doctest:+ELLIPSIS
Pipeline(...)
>>> pl.transform(stops.iloc[0:1]).toarray().tolist()[0] == [0, 1, 0]
True
"""
return Pipeline([
('string-ify', FunctionTransformer(func = lambda x : x.astype(str), validate = False)),
('simple-impute', SimpleImputer(strategy = 'constant', fill_value = 'NULL')),
('one-hot', OneHotEncoder(handle_unknown = 'ignore'))
])
# ---------------------------------------------------------------------
# Question # 2
# ---------------------------------------------------------------------
def round_to_five(nums):
to_return = []
for num in nums:
if num % 5 == 0:
to_return.append(num)
elif num % 5 > 2:
to_return.append(num + (5 - num % 5))
else:
to_return.append(num - num % 5)
return np.array(to_return)
def create_age_feats():
"""
:Example:
>>> pl = create_age_feats()
>>> isinstance(pl, Pipeline)
True
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)[['subject_age']]
>>> pl.fit(stops) # doctest:+ELLIPSIS
Pipeline(...)
>>> pl.transform(stops.iloc[0:1])[0][0] == 20
True
"""
return Pipeline([
('fillna', SimpleImputer(strategy = 'mean')),
('move-to-five', FunctionTransformer(func = round_to_five, validate = False))
])
# ---------------------------------------------------------------------
# Question # 3
# ---------------------------------------------------------------------
def dayofweek_and_hour(df):
return df[['day of week', 'hour']]
def baseline_model():
"""
:Example:
>>> pl = baseline_model()
>>> isinstance(pl, Pipeline)
True
>>> isinstance(pl.steps[-1][-1], LogisticRegression)
True
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp).drop('searched', axis=1)
>>> searched_fp = os.path.join('data', 'sample_imp_search.csv')
>>> searched = pd.read_csv(searched_fp, names=['searched'], squeeze=True)
>>> pl.fit(stops.iloc[:500], searched.iloc[:500]) # doctest:+ELLIPSIS
Pipeline(...)
>>> out = pl.predict(stops)
>>> pd.Series(out).isin([0,1]).all()
True
"""
cat_feat = ['stop_cause', 'service_area', 'subject_race', 'subject_sex', 'sd_resident']
smoothed_feat = ['subject_age']
unchanged_feat = ['dayofweek', 'hour']
pl_cat_feat = create_cat_feats()
pl_smoothed_feat = create_age_feats()
preproc = ColumnTransformer(transformers=
[('cat', pl_cat_feat, cat_feat),
('smoothed', pl_smoothed_feat, smoothed_feat),
('unchanged', 'passthrough', unchanged_feat)])
return Pipeline(steps=[('preprocessor', preproc), ('regressor', LogisticRegression())])
def train_test_acc(pl, stops, searched):
"""
:Example:
>>> pl = baseline_model()
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)
>>> searched_fp = os.path.join('data', 'sample_imp_search.csv')
>>> searched = pd.read_csv(searched_fp, names=['searched'], squeeze=True)
>>> out = train_test_acc(pl, stops, searched)
>>> np.isclose(out, 0.90, 0.1).all()
True
"""
df = stops.copy().drop('stop_id', axis = 1)
if 'searched' in df.columns:
X = df.drop('searched', axis = 1)
else:
X = df
y = searched
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
pl.fit(X_train, y_train)
return (pl.score(X_train, y_train), pl.score(X_test, y_test))
# ---------------------------------------------------------------------
# Question # 4
# ---------------------------------------------------------------------
def constant_model_acc(searched):
"""
:Example:
>>> searched_fp = os.path.join('data', 'sample_imp_search.csv')
>>> searched = pd.read_csv(searched_fp, names=['searched'], squeeze=True)
>>> 0.8 <= constant_model_acc(searched) <= 1.0
True
"""
predict = [0] * len(searched)
return accuracy_score(predict, searched)
def model_outcomes(predictions, target):
"""
:Example:
>>> out = model_outcomes(pd.Series([1,0,1,0]), pd.Series([0,1,1,0]))
>>> (np.diag(out) == 1).all()
True
>>> set(out.columns) == {'FN', 'FP', 'TN', 'TP'}
True
"""
df = pd.DataFrame()
df['FP'] = [1 if predictions.loc[x] == 1 and target.loc[x] == 0 else 0 for x in range(len(predictions))]
df['FN'] = [1 if predictions.loc[x] == 0 and target.loc[x] == 1 else 0 for x in range(len(predictions))]
df['TP'] = [1 if predictions.loc[x] == 1 and target.loc[x] == 1 else 0 for x in range(len(predictions))]
df['TN'] = [1 if predictions.loc[x] == 0 and target.loc[x] == 0 else 0 for x in range(len(predictions))]
return df
def metrics(predictions, target):
"""
:Example:
>>> out = metrics(pd.Series([1,0,1,0]), pd.Series([0,1,1,0]))
>>> set(out.index) == {'acc', 'f1', 'fdr', 'fnr', 'fpr', 'precision', 'recall', 'specificity'}
True
>>> (out == 0.5).all()
True
"""
index = ['acc', 'f1', 'fdr', 'fnr', 'fpr', 'precision', 'recall', 'specificity']
tn, fp, fn, tp = (confusion_matrix(predictions, target, labels=[0,1]).ravel())
values = [accuracy_score(predictions, target),
recall_score(predictions, target),
recall_score(predictions, target, pos_label=0),
precision_score(predictions, target),
fn / (fn + tn),
fp / (fp + tp),
fp / (tp + fp),
f1_score(predictions, target)
]
return pd.Series(values, index = index)
# ---------------------------------------------------------------------
# Question # 5
# ---------------------------------------------------------------------
class AdditiveSmoother(BaseEstimator, ClassifierMixin):
"""
:Example:
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)
>>> searched_fp = os.path.join('data', 'sample_imp_search.csv')
>>> searched = pd.read_csv(searched_fp, names=['searched'], squeeze=True)
>>> asm = AdditiveSmoother()
>>> asm.fit(stops[['subject_sex']], searched)
AdditiveSmoother(alpha=100)
>>> np.isclose(asm.srate, 0.054)
True
>>> internal = asm.smdists['subject_sex']['M']
>>> out = asm.transform(stops[['subject_sex']].iloc[[0]])[0][0]
>>> np.isclose(internal, out)
True
"""
def __init__(self, alpha=100):
self.alpha = alpha
def fit(self, X, y, **kwargs):
"""
Calculates the smoothed condition empirical
distributions of the columns of X dependent on y.
In this case, y is searches in the stops data.
"""
# calculate the search rate
X = pd.DataFrame(X)
self.srate = np.mean(y)
smdists = {}
df = X.copy()
df['searched'] = y
# loop through the columns of X
for c in X.columns:
# create a smoothed empirical distribution for each column in X
smoothed = df.groupby(c)['searched'].mean().to_dict()
toSeries = pd.Series(smoothed)
values = X[c].value_counts()
smoothedSeries = (toSeries * values + self.alpha * self.srate) / (values + self.alpha)
smoothedDict = smoothedSeries.to_dict()
smdists[c] = smoothed
# smoothed empirical search rates in smdists
self.smdists = smdists
return self
def transform(self, X):
"""
Transforms the categorical values in the columns of X to
the smoothed search rates of those values.
"""
X = pd.DataFrame(X)
toReturn = []
for i in range(len(X)):
smoothed = []
row = X.loc[i]
for c in X.columns:
if row[c] in self.smdists[c]:
val = self.smdists[c][row[c]]
else:
val = self.srate
smoothed.append(val)
toReturn.append(smoothed)
return np.array(toReturn)
def get_params(self, deep=False):
"""
Gets the parameters of the transformer;
Allows Gridsearch to be used with class.
"""
return {'alpha': self.alpha}
# ---------------------------------------------------------------------
# Question # 6
# ---------------------------------------------------------------------
def final_model():
"""
:Example:
>>> pl = final_model()
>>> isinstance(pl, Pipeline)
True
>>> isinstance(pl.steps[-1][-1], BaseEstimator)
True
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp).drop('searched', axis=1)
>>> searched_fp = os.path.join('data', 'sample_imp_search.csv')
>>> searched = pd.read_csv(searched_fp, names=['searched'], squeeze=True)
>>> pl.fit(stops.iloc[:500], searched.iloc[:500]) # doctest:+ELLIPSIS
Pipeline(...)
>>> out = pl.predict(stops)
>>> pd.Series(out).isin([0,1]).all()
True
"""
cat_feat = ['stop_cause', 'service_area', 'subject_race', 'subject_sex', 'sd_resident']
smoothed_feat = ['subject_age']
unchanged_feat = ['dayofweek', 'hour']
pl_cat_feat = Pipeline([
('string-ify', FunctionTransformer(func = lambda x : x.astype(str), validate = False)),
('simple-impute', SimpleImputer(strategy = 'constant', fill_value = 'NULL')),
('add-smooth', AdditiveSmoother())
])
pl_smoothed_feat = create_age_feats()
preproc = ColumnTransformer(transformers=
[('cat', pl_cat_feat, cat_feat),
('smoothed', pl_smoothed_feat, smoothed_feat),
('unchanged', 'passthrough', unchanged_feat)])
return Pipeline(steps=[('preprocessor', preproc), ('tree', RandomForestClassifier())])
# ---------------------------------------------------------------------
# Question # 7
# ---------------------------------------------------------------------
def compare_search_rate(stops, predictions, col):
"""
:Example:
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)
>>> randpred = np.random.choice([0,1], size=len(stops))
>>> out = compare_search_rate(stops, randpred, 'hour')
>>> set(out.columns) == {'searched', 'predicted'}
True
>>> (out.index == range(24)).all()
True
"""
df = stops.copy()
df['predicted'] = predictions
df = df.dropna(subset = ['searched'])
df = df.groupby(col)[['searched', 'predicted']].mean()
return df
def compare_metrics(stops, predictions, col):
"""
:Example:
>>> fp = os.path.join('data', 'sample_stops.csv')
>>> stops = pd.read_csv(fp)
>>> randpred = np.random.choice([0,1], size=len(stops))
>>> out = compare_metrics(stops, randpred, 'hour')
>>> 'precision' in out.columns
True
>>> (out.index == range(24)).all()
True
"""
df = stops.copy()
df['predicted'] = predictions
df = df.dropna(subset = ['searched'])
df = df.groupby(col).apply(lambda x : metrics(x['predicted'], x['searched']))
return df
# ---------------------------------------------------------------------
# DO NOT TOUCH BELOW THIS LINE
# IT'S FOR YOUR OWN BENEFIT!
# ---------------------------------------------------------------------
# Graded functions names! DO NOT CHANGE!
# This dictionary provides your doctests with
# a check that all of the questions being graded
# exist in your code!
GRADED_FUNCTIONS = {
'q01': ['create_cat_feats'],
'q02': ['create_age_feats'],
'q03': ['baseline_model','train_test_acc'],
'q04': ['constant_model_acc', 'model_outcomes', 'metrics'],
'q05': ['AdditiveSmoother'],
'q06': ['final_model'],
'q07': ['compare_search_rate', 'compare_metrics']
}
def check_for_graded_elements():
"""
>>> check_for_graded_elements()
True
"""
for q, elts in GRADED_FUNCTIONS.items():
for elt in elts:
if elt not in globals():
stmt = "YOU CHANGED A QUESTION THAT SHOULDN'T CHANGE! \
In %s, part %s is missing" %(q, elt)
raise Exception(stmt)
return True