-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_engineering.py
167 lines (127 loc) · 5.64 KB
/
feature_engineering.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
import pandas as pd
import numpy as np
def build_y(df, delta_days='30 days'):
'''build y label (bool) for each data point based on the delta_days used in the defination of churn.
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
delta_days {str} -- [the variable used in the defination of user's churning] (default: {'30 days'})
Returns:
y{pandas series} -- bool series created based on the defination of churn
'''
today = df['last_trip_date'].max()
delta = pd.Timedelta(delta_days)
df['churn?'] = (df['last_trip_date'] < (today-delta)) *1
y = df['churn?']
return y
def fill_cont_nans(df, col_list=['avg_rating_by_driver', 'avg_rating_of_driver'], grouper=['city', 'luxury_car_user']):
'''Fill nans of continuous features
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
col_list {list} -- [column names of continuous features] (default: {['avg_rating_by_driver', 'avg_rating_of_driver']})
grouper {list} -- [features to group by] (default: {['city', 'luxury_car_user']})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
for col in col_list:
df[col].fillna(df.groupby(grouper)[col].transform('median'), inplace=True)
return df
def fill_categ_nans(df, col_list=['phone']):
'''Fill nans of categorical features.
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
col_list {list} -- [column names of categorical features] (default: {['phone']})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
for col in col_list:
value = df[col].mode().values.flatten()
# df.loc[df['phone'].isnull(), 'phone'] = value[0]
df.loc[df[col].isnull(), col] = value[0]
return df
def dummify(df, col_list=['city', 'phone', 'luxury_car_user']):
'''
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
col_list {list} -- [column names of features to be dummified] (default: {['city', 'phone', 'luxury_car_user']})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
for col in col_list:
dummies = pd.get_dummies(df[col],prefix=col)
df[dummies.columns] = dummies
return df
def logify(df, col_list=['avg_dist', 'avg_rating_by_driver', 'avg_rating_of_driver']):
'''log transform features
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
col_list {list} -- [features to be logrified] (default: {['avg_dist', 'avg_rating_by_driver', 'avg_rating_of_driver']})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
for col in col_list:
df[col+'_log'] = np.log(df[col]+1)
return df
def feature_creation(df, delta_days):
'''create the feature of user_lifespan
Arguments:
df {[pandas dataframe]} -- input dataframe
delta_days {str} -- [the variable used in the defination of user's churning] (default: {'30 days'})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
# Create user lifespan
today = df['last_trip_date'].max()
delta = pd.Timedelta(delta_days)
df['user_lifespan'] = ((today-delta)-df['signup_date']).dt.days
# Create dummy for if a user rated a driver or not
df['user_rated_driver'] = (df['avg_rating_of_driver'].isnull() == 0) *1
return df
def interactify(df, interacter1=['user_rated_driver'], interacter2=['avg_rating_of_driver']):
'''create new features as interaction between two features
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
interacter1 {list} -- [list of column names] (default: {['user_rated_driver']})
interacter2 {list} -- [list of column names] (default: {['avg_rating_of_driver']})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
for col1, col2 in zip(interacter1, interacter2):
df[col1+'_'+col2] = df[col1] * df[col2]
return df
def build_X(df, model='GradientBoostingRegressor', delta_days='30 days'):
'''feature engineer pipeline to call different functions
Arguments:
df {[pandas dataframe]} -- input dataframe
Keyword Arguments:
model {str} -- [name of sklearn model] (default: {'GradientBoostingRegressor'})
delta_days {str} -- [the variable used in the defination of user's churning] (default: {'30 days'})
Returns:
df {[pandas dataframe]} -- output dataframe
'''
data = fill_categ_nans(data, ['phone'])
data = fill_cont_nans(df, ['avg_rating_by_driver', 'avg_rating_of_driver'], ['city', 'luxury_car_user'])
data = dummify(data, ['city', 'phone', 'luxury_car_user'])
logify(df, ['avg_dist', 'avg_rating_by_driver', 'avg_rating_of_driver'])
data = feature_creation(data, delta_days)
data = interactify(data, interacter1=['user_rated_driver'], interacter2=['avg_rating_of_driver'])
cols_to_keep = ['avg_dist_log', 'avg_rating_by_driver_log', 'avg_rating_of_driver_log', 'avg_surge',
'surge_pct',
'trips_in_first_30_days', 'weekday_pct',
"city_King's Landing",
'city_Winterfell', 'phone_iPhone',
'luxury_car_user_True', 'user_lifespan', 'user_rated_driver',
'user_rated_driver_avg_rating_of_driver']
cols_to_keep_nonparam = ['city_Astapor', 'phone_Android',
'luxury_car_user_False',
'avg_dist', 'avg_rating_by_driver', 'avg_rating_of_driver']
if model == 'logisticModel':
X = data[cols_to_keep]
else:
X = data[cols_to_keep+cols_to_keep_nonparam]
return X