Klantsegmentatie in Python
Karolis Urbonas
Head of Data Science, Amazon
online-dataset als in de vorige lessenTotalSum = Quantity x UnitPrice.
We starten met een voorbewerkte online-DataFrame met alleen de laatste 12 maanden aan data:
print('Min:{}; Max:{}'.format(min(online.InvoiceDate),
max(online.InvoiceDate)))
Min:2010-12-10; Max:2011-12-09
Laten we een hypothetische snapshot_day maken, alsof we nu analyseren.
snapshot_date = max(online.InvoiceDate) + datetime.timedelta(days=1)
# Aggregate data on a customer level datamart = online.groupby(['CustomerID']).agg({ 'InvoiceDate': lambda x: (snapshot_date - x.max()).days, 'InvoiceNo': 'count', 'TotalSum': 'sum'})# Rename columns for easier interpretation datamart.rename(columns = {'InvoiceDate': 'Recency', 'InvoiceNo': 'Frequency', 'TotalSum': 'MonetaryValue'}, inplace=True)# Check the first rows datamart.head()
Onze tabel voor RFM-segmentatie is klaar!

Klantsegmentatie in Python