-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_data.py
552 lines (408 loc) · 19.7 KB
/
clean_data.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
""" Creates an analytic dataset from the Starbucks Capstone challenge
data files:
* portfolio.json - contains offer ids and meta data (duration, type, etc.)
* id (string) - offer id
* offer_type (string) - string that describes the offer type
* difficulty (int) - minimum a customer has to spend to complete an offer
* reward (int) - reward given for completing an offer
* duration (int) - offer duration [days]
* channels - list of strings that describe how an offer is communicated
to a customer
* profile.json - demographic data for each customer
* age (int) - age of the customer
* became_member_on (int) - date when customer created an app account
* gender (str) - gender of the customer (note some entries contain
'O' for other rather than M or F)
* id (str) - customer id
* income (float) - customer's income
* transcript.json - records for transactions, offers received, offers viewed,
and offers completed
* event (str) - record description (i.e. transaction, offer received,
offer viewed, etc.)
* person (str) - customer id
* time (int) - time in hours. The data begins at time t=0
* value - (dict of strings) - either an offer id or transaction amount
depending on the record
References:
----------
https://github.com/jtleek/datasharing - Defines what an analytic dataset is
https://stackoverflow.com/questions/41968732/
set-order-of-columns-in-pandas-dataframe
https://stackoverflow.com/questions/40245507/
python-pandas-selecting-rows-whose-column-value-is-null-none-nan
https://scikit-learn.org/stable/modules/generated/
sklearn.preprocessing.MultiLabelBinarizer.html
#sklearn.preprocessing.MultiLabelBinarizer
https://stackoverflow.com/questions/26454649/
python-round-up-to-the-nearest-ten
https://stackoverflow.com/questions/44596077/
datetime-strptime-in-python
https://stackoverflow.com/questions/4953272/
how-to-match-exact-multiple-strings-in-python
https://stackoverflow.com/questions/402504/
how-to-determine-a-python-variables-type
https://stackoverflow.com/questions/49728421/
pandas-dataframe-settingwithcopywarning-a-value-is-trying-to-be-
set-on-a-copy
https://stackoverflow.com/questions/43515877/
should-binary-features-be-one-hot-encoded
https://www.shanelynn.ie/select-pandas-dataframe-rows-and-
columns-using-iloc-loc-and-ix/
https://stackoverflow.com/questions/38987/how-to-merge-two-
dictionaries-in-a-single-expression
https://stackoverflow.com/questions/3002085/python-to-print-out-
status-bar-and-percentage
https://progressbar-2.readthedocs.io/en/latest/progressbar.bar.html
https://progressbar-2.readthedocs.io/en/latest/index.html#introduction
"""
from datetime import datetime
import numpy as np
import pandas as pd
import re
import os
import progressbar
from sklearn.preprocessing import LabelBinarizer, MultiLabelBinarizer
def update_column_name(dataframe,
old_column_name,
new_column_name):
""" Updates a Pandas DataFrame column name
INPUT:
dataframe: Pandas DataFrame object
old_column_name: String that stores the old column name
new_column_name: String that stores the new column name
OUTPUT:
column_names: np.array that stores the updated Pandas DataFrame
column names"""
column_names = dataframe.columns.values
select_data = np.array([elem == old_column_name for elem in column_names])
column_names[select_data] = new_column_name
return column_names
def clean_portfolio(data_dir="./data"):
"""
Transforms a DataFrame containing offer ids and meta data about
each offer (duration, type, etc.)
INPUT:
(Optional) data_dir: String that stores the full path to the
data directory
OUTPUT:
portfolio: DataFrame containing offer ids and meta data about
each offer (duration, type, etc.)
"""
portfolio = pd.read_json(os.path.join(data_dir, 'portfolio.json'),
orient='records',
lines=True)
# Change the name of the 'id' column to 'offerid'
columns = update_column_name(portfolio,
'id',
'offerid')
# Change the name of the 'duration' column to 'durationdays'
portfolio.columns = update_column_name(portfolio,
'duration',
'durationdays')
# Remove underscores from column names
portfolio.columns = [re.sub('_', '', elem) for elem in columns]
# Initialize a list that stores the desired output DataFrame
# column ordering
column_ordering = ['offerid',
'difficulty',
'durationdays',
'reward']
# One hot encode the 'offertype' column
offertype_df = pd.get_dummies(portfolio['offertype'])
column_ordering.extend(offertype_df.columns.values)
# One hot encode the 'channels' columns
ml_binarizerobj = MultiLabelBinarizer()
ml_binarizerobj.fit(portfolio['channels'])
channels_df =\
pd.DataFrame(ml_binarizerobj.transform(portfolio['channels']),
columns=ml_binarizerobj.classes_)
column_ordering.extend(channels_df.columns.values)
# Replace the 'offertype' and 'channels' columns
portfolio = pd.concat([portfolio, offertype_df, channels_df], axis=1)
portfolio = portfolio.drop(columns=['offertype', 'channels'])
# Return the "cleaned" portfolio data
return portfolio[column_ordering]
def convert_to_datetime(elem):
"""Converts a string to a datetime object
INPUT:
elem: String that stores a date in the %Y%m%d format
OUTPUT:
datetimeobj: Datetime object"""
return datetime.strptime(str(elem), '%Y%m%d')
def clean_profile(data_dir = "./data"):
""" Transforms a DataFrame that contains demographic data for each
customer
INPUT:
(Optional) data_dir: String that stores the full path to the
data directory
OUTPUT:
profile: DataFrame that contains demographic data for each
customer
"""
profile = pd.read_json('data/profile.json',
orient='records',
lines=True)
# Remove customers with N/A income data
profile = profile[profile['income'].notnull()]
# Remove customers with unspecified gender
profile = profile[profile['gender'] != 'O']
profile = profile.reset_index(drop=True)
# Change the name of the 'id' column to 'customerid'
profile.columns = update_column_name(profile,
'id',
'customerid')
# Initialize a list that describes the desired DataFrame column
# ordering
column_ordering = ['customerid',
'gender',
'income']
# Transform the 'became_member_on' column to a datetime object
profile['became_member_on'] =\
profile['became_member_on'].apply(convert_to_datetime)
# One hot encode a customer's membership start year
profile['membershipstartyear'] =\
profile['became_member_on'].apply(lambda elem: elem.year)
membershipstartyear_df = pd.get_dummies(profile['membershipstartyear'])
column_ordering.extend(membershipstartyear_df.columns.values)
# One hot encode a customer's age range
min_age_limit = np.int(np.floor(np.min(profile['age'])/10)*10)
max_age_limit = np.int(np.ceil(np.max(profile['age'])/10)*10)
profile['agerange'] =\
pd.cut(profile['age'],
(range(min_age_limit,max_age_limit + 10, 10)),
right=False)
profile['agerange'] = profile['agerange'].astype('str')
agerange_df = pd.get_dummies(profile['agerange'])
column_ordering.extend(agerange_df.columns.values)
# Transform a customer's gender from a character to a number
binarizerobj = LabelBinarizer()
profile['gender'] = binarizerobj.fit_transform(profile['gender'])
gender_integer_map = {}
for elem in binarizerobj.classes_:
gender_integer_map[elem] = binarizerobj.transform([elem])[0,0]
# Appened one hot encoded age range and membership start year variables
profile = pd.concat([profile,
agerange_df,
membershipstartyear_df], axis=1)
# Drop depcreated columns
profile = profile.drop(columns=['age',
'agerange',
'became_member_on',
'membershipstartyear'])
# Return a DataFrame with "clean" customer profile data
return profile[column_ordering], gender_integer_map
def clean_transcript(profile,
data_dir = './data'):
""" Transforms a DataFrame that contains records for transactions, offers
received, offers viewed, and offers completed
INPUT:
profile: DataFrame that contains demographic data for each
customer
(Optional) data_dir: String that stores the full path to the
data directory
OUTPUT:
offer_data: DataFrame that describes customer offer data
transaction: DataFrame that describes customer transactions
"""
transcript = pd.read_json(os.path.join(data_dir,
'transcript.json'),
orient='records',
lines=True)
# Change the name of the 'person' column to 'customerid'
transcript.columns = update_column_name(transcript,
'person',
'customerid')
# Remove customer id's that are not in the customer profile DataFrame
select_data = transcript['customerid'].isin(profile['customerid'])
transcript = transcript[select_data]
percent_removed = 100 * (1 - select_data.sum() / select_data.shape[0])
print("Percentage of transactions removed: %.2f %%" % percent_removed)
# Convert from hours to days
transcript['time'] /= 24.0
# Change the name of the 'time' column to 'timedays'
transcript.columns = update_column_name(transcript,
'time',
'timedays')
# Select customer offers
pattern_obj = re.compile('^offer (?:received|viewed|completed)')
h_is_offer = lambda elem: pattern_obj.match(elem) != None
is_offer = transcript['event'].apply(h_is_offer)
offer_data = transcript[is_offer].copy()
offer_data = offer_data.reset_index(drop=True)
# Initialize a list that describes the desired output DataFrame
# column ordering
column_order = ['offerid', 'customerid', 'timedays']
# Create an offerid column
offer_data['offerid'] =\
offer_data['value'].apply(lambda elem: list(elem.values())[0])
# Transform a column that describes a customer offer event
pattern_obj = re.compile('^offer ([a-z]+$)')
h_transform = lambda elem: pattern_obj.match(elem).groups(1)[0]
offer_data['event'] = offer_data['event'].apply(h_transform)
# One hot encode customer offer events
event_df = pd.get_dummies(offer_data['event'])
column_order.extend(event_df.columns.values)
# Create a DataFrame that describes customer offer events
offer_data = pd.concat([offer_data, event_df], axis=1)
offer_data.drop(columns=['event', 'value'])
offer_data = offer_data[column_order]
# Select customer transaction events
transaction = transcript[is_offer == False]
transaction = transaction.reset_index(drop=True)
# Transform customer transaction event values
transaction['amount'] =\
transaction['value'].apply(lambda elem: list(elem.values())[0])
# Create a DataFrame that describes customer transactions
transaction = transaction.drop(columns=['event', 'value'])
column_order = ['customerid', 'timedays', 'amount']
transaction = transaction[column_order]
return offer_data, transaction
def create_offeranalysis_dataset(profile,
portfolio,
offer_data,
transaction):
""" Creates an analytic dataset from the following Starbucks challenge
datasets:
* portfolio.json - Contains offer ids and meta data (duration, type,
etc.)
* profile.json - demographic data for each customer
* transcript.json - records for transactions, offers received, offers
viewed, and offers completed
INPUT:
profile: DataFrame that contains demographic data for each
customer
portfolio: Contains offer ids and meta data (duration, type, etc.)
offer_data: DataFrame that describes customer offer data
transaction: DataFrame that describes customer transactions
OUTPUT:
clean_data: DataFrame that characterizes the effectiveness of
customer offers"""
clean_data = []
customerid_list = offer_data['customerid'].unique()
widgets=[' [',
progressbar.Timer(), '] ',
progressbar.Bar(),
' (',
progressbar.ETA(),
') ']
for idx in progressbar.progressbar(range(len(customerid_list)),
widgets=widgets):
clean_data.extend(create_combined_records(customerid_list[idx],
portfolio,
profile,
offer_data,
transaction))
clean_data = pd.DataFrame(clean_data)
# Initialize a list that describes the desired output DataFrame
# column ordering
column_ordering = ['time', 'offerid', 'customerid', 'totalamount',
'offersuccessful', 'difficulty', 'durationdays',
'reward', 'bogo', 'discount', 'informational',
'email', 'mobile', 'social', 'web', 'gender',
'income', 2013, 2014, 2015, 2016, 2017, 2018,
'[10, 20)', '[20, 30)', '[30, 40)', '[40, 50)',
'[50, 60)', '[60, 70)', '[70, 80)', '[80, 90)',
'[90, 100)', '[100, 110)']
clean_data = clean_data[column_ordering]
clean_data = clean_data.sort_values('time')
return clean_data.reset_index(drop=True)
def create_combined_records(customer_id,
portfolio,
profile,
offer_data,
transaction):
"""
Creates a list of dictionaries that describes the effectiveness of
offers to a specific customer
INPUT:
customer_id: String that refers to a specific customer
profile: DataFrame that contains demographic data for each
customer
portfolio: DataFrame containing offer ids and meta data about
each offer (duration, type, etc.)
offer_data: DataFrame that describes customer offer data
transaction: DataFrame that describes customer transactions
OUTPUT:
rows: List of dictionaries that describes the effectiveness of
offers to a specific customer
"""
# Select a customer's profile
cur_customer = profile[profile['customerid'] == customer_id]
# Select offer data for a specific customer
select_offer_data = offer_data['customerid'] == customer_id
customer_offer_data = offer_data[select_offer_data]
customer_offer_data = customer_offer_data.drop(columns='customerid')
customer_offer_data = customer_offer_data.reset_index(drop=True)
# Select transactions for a specific customer
select_transaction = transaction['customerid'] == customer_id
customer_transaction_data = transaction[select_transaction]
customer_transaction_data =\
customer_transaction_data.drop(columns='customerid')
customer_transaction_data =\
customer_transaction_data.reset_index(drop=True)
# Initialize DataFrames that describe when a customer receives,
# views, and completes an offer
event_type = ['completed',
'received',
'viewed']
offer_received =\
customer_offer_data[customer_offer_data['received'] == 1]
offer_received = offer_received.drop(columns=event_type)
offer_received = offer_received.reset_index(drop=True)
offer_viewed =\
customer_offer_data[customer_offer_data['viewed'] == 1]
offer_viewed = offer_viewed.drop(columns=event_type)
offer_viewed = offer_viewed.reset_index(drop=True)
offer_completed =\
customer_offer_data[customer_offer_data['completed'] == 1]
offer_completed = offer_completed.drop(columns=event_type)
offer_completed = offer_completed.reset_index(drop=True)
# Iterate over each offer a customer receives
rows = []
for idx in range(offer_received.shape[0]):
# Initialize the current offer id
cur_offer_id = offer_received.iloc[idx]['offerid']
# Look-up a description of the current offer
cur_offer = portfolio.loc[portfolio['offerid'] == cur_offer_id]
durationdays = cur_offer['durationdays'].values[0]
# Initialize the time period when an offer is valid
cur_offer_startime = offer_received.iloc[idx]['timedays']
cur_offer_endtime =\
offer_received.iloc[idx]['timedays'] + durationdays
# Initialize a boolean array that select customer transcations that
# fall within the valid offer time window
select_transaction =\
np.logical_and(customer_transaction_data['timedays'] >=
cur_offer_startime,
customer_transaction_data['timedays'] <=
cur_offer_endtime)
# Initialize a boolean array that selects a description of when a
# customer completes an offer (this array may not contain any True
# values)
select_offer_completed =\
np.logical_and(offer_completed['timedays'] >= cur_offer_startime,
offer_completed['timedays'] <= cur_offer_endtime)
# Initialize a boolean array that selects a description of when a
# customer views an offer (this array may not contain any True
# values)
select_offer_viewed =\
np.logical_and(offer_viewed['timedays'] >= cur_offer_startime,
offer_viewed['timedays'] <= cur_offer_endtime)
# Determine whether the current offer was successful
cur_offer_successful =\
select_offer_completed.sum() > 0 and select_offer_viewed.sum() > 0
# Select customer transcations that occurred within the current offer
# valid time window
cur_offer_transactions = customer_transaction_data[select_transaction]
# Initialize a dictionary that describes the current customer offer
cur_row = {'offerid': cur_offer_id,
'customerid': customer_id,
'time': cur_offer_startime,
'offersuccessful': int(cur_offer_successful),
'totalamount': cur_offer_transactions['amount'].sum()}
cur_row.update(cur_offer.iloc[0,1:].to_dict())
cur_row.update(cur_customer.iloc[0,1:].to_dict())
# Update a list of dictionaries that describes the effectiveness of
# offers to a specific customer
rows.append(cur_row)
return rows