Customer Segmentation in Python Chapter2
Customer Segmentation in Python Chapter2
Introduction to RFM
segmentation
Karolis Urbonas
Head of Data Science, Amazon
DataCamp Customer Segmentation in Python
Recency (R)
Frequency (F)
Monetary Value (M)
DataCamp Customer Segmentation in Python
data['Spend_Quartile'] = spend_quartiles
data.sort_values('Spend')
DataCamp Customer Segmentation in Python
Assigning labels
Highest score to the best metric - best is not always highest e.g. recency
In this case, the label is inverse - the more recent the customer, the better
DataCamp Customer Segmentation in Python
Assigning labels
# Create numbered labels
r_labels = list(range(4, 0, -1))
Assigning labels
As you can see, the quartile labels are reversed, since the more recent customers
are more valuable.
DataCamp Customer Segmentation in Python
Custom labels
We can define a list with string or any other values, depending on the use case.
Custom labels
Recency, Frequency,
Monetary Value calculation
Karolis Urbonas
Head of Data Science, Amazon
DataCamp Customer Segmentation in Python
Definitions
Recency - days since last customer transaction
Frequency - number of transactions in the last 12 months
Monetary Value - total spend in the last 12 months
DataCamp Customer Segmentation in Python
We're starting with a pre-processed online DataFrame with only the latest 12
months of data:
print('Min:{}; Max:{}'.format(min(online.InvoiceDate),
max(online.InvoiceDate)))
Min:2010-12-10; Max:2011-12-09
Karolis Urbonas
Head of Data Science, Amazon
DataCamp Customer Segmentation in Python
Data
Dataset we created previously
Will calculate quartile value for each column and name then R, F, M
DataCamp Customer Segmentation in Python
Recency quartile
r_labels = range(4, 0, -1)
datamart['RFM_Score'] = datamart[['R','F','M']].sum(axis=1)
DataCamp Customer Segmentation in Python
Final result
DataCamp Customer Segmentation in Python
Karolis Urbonas
Head of Data Science, Amazon
DataCamp Customer Segmentation in Python
Use RFM score to group customers into Gold, Silver and Bronze segments.
def segment_me(df):
if df['RFM_Score'] >= 9:
return 'Gold'
elif (df['RFM_Score'] >= 5) and (df['RFM_Score'] < 9):
return 'Silver'
else:
return 'Bronze'
datamart.groupby('General_Segment').agg({
'Recency': 'mean',
'Frequency': 'mean',
'MonetaryValue': ['mean', 'count']
}).round(1)
DataCamp Customer Segmentation in Python