-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlab_59_customer_ltv.py
368 lines (273 loc) · 8.34 KB
/
lab_59_customer_ltv.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
# BUSINESS SCIENCE LEARNING LABS ----
# LAB 59: CUSTOMER LIFETIME VALUE ----
# CUSTOMER LIFETIME VALUE WITH MACHINE LEARNING ----
# **** ----
# CONDA ENV USED: lab_59_customer_ltv_py
# LIBRARIES ----
import pandas as pd
import numpy as np
import joblib
import plydata.cat_tools as cat
import plotnine as pn
from xgboost import XGBClassifier, XGBRegressor
from sklearn.model_selection import GridSearchCV
pn.options.dpi = 300
# 1.0 DATA PREPARATION ----
cdnow_raw_df = pd.read_csv(
"data/CDNOW_master.txt",
sep = "\s+",
names = ["customer_id", "date", "quantity", "price"]
)
cdnow_raw_df.info()
cdnow_df = cdnow_raw_df \
.assign(
date = lambda x: x['date'].astype(str)
) \
.assign(
date = lambda x: pd.to_datetime(x['date'])
) \
.dropna()
cdnow_df.info()
# 2.0 COHORT ANALYSIS ----
# - Only the customers that have joined at the specific business day
# Get Range of Initial Purchases ----
cdnow_first_purchase_tbl = cdnow_df \
.sort_values(['customer_id', 'date']) \
.groupby('customer_id') \
.first()
cdnow_first_purchase_tbl
cdnow_first_purchase_tbl['date'].min()
cdnow_first_purchase_tbl['date'].max()
# Visualize: All purchases within cohort
cdnow_df \
.reset_index() \
.set_index('date') \
[['price']] \
.resample(
rule = "MS"
) \
.sum() \
.plot()
# Visualize: Individual Customer Purchases
ids = cdnow_df['customer_id'].unique()
ids_selected = ids[0:10]
cdnow_cust_id_subset_df = cdnow_df \
[cdnow_df['customer_id'].isin(ids_selected)] \
.groupby(['customer_id', 'date']) \
.sum() \
.reset_index()
pn.ggplot(
pn.aes('date', 'price', group = 'customer_id'),
data = cdnow_cust_id_subset_df
) \
+ pn.geom_line() \
+ pn.geom_point() \
+ pn.facet_wrap('customer_id') \
+ pn.scale_x_date(
date_breaks = "1 year",
date_labels = "%Y"
)
# 3.0 MACHINE LEARNING ----
# Frame the problem:
# - What will the customers spend in the next 90-Days? (Regression)
# - What is the probability of a customer to make a purchase in next 90-days? (Classification)
# 3.1 TIME SPLITTING (STAGE 1) ----
n_days = 90
max_date = cdnow_df['date'].max()
cutoff = max_date - pd.to_timedelta(n_days, unit = "d")
temporal_in_df = cdnow_df \
[cdnow_df['date'] <= cutoff]
temporal_out_df = cdnow_df \
[cdnow_df['date'] > cutoff]
# 3.2 FEATURE ENGINEERING (RFM) ----
# - Most challenging part
# - 2-Stage Process
# - Need to frame the problem
# - Need to think about what features to include
# Make Targets from out data ----
targets_df = temporal_out_df \
.drop('quantity', axis=1) \
.groupby('customer_id') \
.sum() \
.rename({'price': 'spend_90_total'}, axis = 1) \
.assign(spend_90_flag = 1)
# Make Recency (Date) Features from in data ----
max_date = temporal_in_df['date'].max()
recency_features_df = temporal_in_df \
[['customer_id', 'date']] \
.groupby('customer_id') \
.apply(
lambda x: (x['date'].max() - max_date) / pd.to_timedelta(1, "day")
) \
.to_frame() \
.set_axis(["recency"], axis=1)
recency_features_df
# Make Frequency (Count) Features from in data ----
frequency_features_df = temporal_in_df \
[['customer_id', 'date']] \
.groupby('customer_id') \
.count() \
.set_axis(['frequency'], axis=1)
frequency_features_df
# Make Price (Monetary) Features from in data ----
price_features_df = temporal_in_df \
.groupby('customer_id') \
.aggregate(
{
'price': ["sum", "mean"]
}
) \
.set_axis(['price_sum', 'price_mean'], axis = 1)
price_features_df
# 3.3 COMBINE FEATURES ----
features_df = pd.concat(
[recency_features_df, frequency_features_df, price_features_df], axis = 1
) \
.merge(
targets_df,
left_index = True,
right_index = True,
how = "left"
) \
.fillna(0)
# 4.0 MACHINE LEARNING -----
from xgboost import XGBClassifier, XGBRegressor
from sklearn.model_selection import GridSearchCV
X = features_df[['recency', 'frequency', 'price_sum', 'price_mean']]
# 4.1 NEXT 90-DAY SPEND PREDICTION ----
y_spend = features_df['spend_90_total']
xgb_reg_spec = XGBRegressor(
objective="reg:squarederror",
random_state=123
)
xgb_reg_model = GridSearchCV(
estimator=xgb_reg_spec,
param_grid=dict(
learning_rate = [0.01, 0.1, 0.3, 0.5]
),
scoring = 'neg_mean_absolute_error',
refit = True,
cv = 5
)
xgb_reg_model.fit(X, y_spend)
xgb_reg_model.best_score_
xgb_reg_model.best_params_
xgb_reg_model.best_estimator_
predictions_reg = xgb_reg_model.predict(X)
# 4.2 NEXT 90-DAY SPEND PROBABILITY ----
y_prob = features_df['spend_90_flag']
xgb_clf_spec = XGBClassifier(
objective = "binary:logistic",
random_state = 123
)
xgb_clf_model = GridSearchCV(
estimator=xgb_clf_spec,
param_grid=dict(
learning_rate = [0.01, 0.1, 0.3, 0.5]
),
scoring = 'roc_auc',
refit = True,
cv = 5
)
xgb_clf_model.fit(X, y_prob)
xgb_clf_model.best_score_
xgb_clf_model.best_params_
xgb_clf_model.best_estimator_
predictions_clf = xgb_clf_model.predict_proba(X)
# 4.3 FEATURE IMPORTANCE (GLOBAL) ----
# Importance | Spend Amount Model
imp_spend_amount_dict = xgb_reg_model \
.best_estimator_ \
.get_booster() \
.get_score(importance_type = 'gain')
imp_spend_amount_df = pd.DataFrame(
data = {
'feature':list(imp_spend_amount_dict.keys()),
'value':list(imp_spend_amount_dict.values())
}
) \
.assign(
feature = lambda x: cat.cat_reorder(x['feature'] , x['value'])
)
pn.ggplot(
pn.aes('feature', 'value'),
data = imp_spend_amount_df
) \
+ pn.geom_col() \
+ pn.coord_flip()
# Importance | Spend Probability Model
imp_spend_prob_dict = xgb_clf_model \
.best_estimator_ \
.get_booster() \
.get_score(importance_type = 'gain')
imp_spend_prob_df = pd.DataFrame(
data = {
'feature':list(imp_spend_prob_dict.keys()),
'value':list(imp_spend_prob_dict.values())
}
) \
.assign(
feature = lambda x: cat.cat_reorder(x['feature'] , x['value'])
)
pn.ggplot(
pn.aes('feature', 'value'),
data = imp_spend_prob_df
) \
+ pn.geom_col() \
+ pn.coord_flip()
# 5.0 SAVE WORK ----
# Save Predictions
predictions_df = pd.concat(
[
pd.DataFrame(predictions_reg).set_axis(['pred_spend'], axis=1),
pd.DataFrame(predictions_clf)[[1]].set_axis(['pred_prob'], axis=1),
features_df.reset_index()
],
axis=1
)
predictions_df
predictions_df.to_pickle("artifacts/predictions_df.pkl")
pd.read_pickle('artifacts/predictions_df.pkl')
# Save Importance
imp_spend_amount_df.to_pickle("artifacts/imp_spend_amount_df.pkl")
imp_spend_prob_df.to_pickle("artifacts/imp_spend_prob_df.pkl")
pd.read_pickle("artifacts/imp_spend_amount_df.pkl")
# Save Models
joblib.dump(xgb_reg_model, 'artifacts/xgb_reg_model.pkl')
joblib.dump(xgb_clf_model, 'artifacts/xgb_clf_model.pkl')
model = joblib.load('artifacts/xgb_reg_model.pkl')
model.predict(X)
# 6.0 HOW CAN WE USE THIS INFORMATION ----
# 6.1 Which customers have the highest spend probability in next 90-days?
# - Target for new products similar to what they have purchased in the past
predictions_df \
.sort_values('pred_prob', ascending=False)
# 6.2 Which customers have recently purchased but are unlikely to buy?
# - Incentivize actions to increase probability
# - Provide discounts, encourage referring a friend, nurture by letting them know what's coming
predictions_df \
[
predictions_df['recency'] > -90
] \
[
predictions_df['pred_prob'] < 0.20
] \
.sort_values('pred_prob', ascending=False)
# 6.3 Missed opportunities: Big spenders that could be unlocked ----
# - Send bundle offers encouraging volume purchases
# - Focus on missed opportunities
predictions_df \
[
predictions_df['spend_90_total'] == 0.0
] \
.sort_values('pred_spend', ascending=False)
# 7.0 NEXT STEPS ----
# - It's really exciting what you can do with Machine Learning.
# Very powerful. But you have to put in the work.
# - Learning Data Wrangling, Modeling, and Visualization (101)
# - Model Improvement (Coming Soon):
# - Algorithms (201-P)
# - AutoML (201-P)
# - Hyper Parameter Tuning (201-P)
# - Forecasting: When will customers purchase? (TBD)
# - Web Applications, API's & Production (202-P)