-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
195 lines (164 loc) · 7.48 KB
/
model.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
##########################################################################################################################################
### RFM MODEL ###
##########################################################################################################################################
import plotly.express as px
import statsmodels.api as sm
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
data = pd.read_csv('transaction_data.csv', encoding='ISO-8859-1')
##########################################################################################################################################
### Create the Customer Table ###
##########################################################################################################################################
# Calculate Sales Value
data['sales_value'] = data['Quantity'] * data['UnitPrice']
# group columns by customer_id
rfmTable = data.groupby(
['CustomerID'], as_index=False
).agg(
{
'sales_value' :sum
, 'InvoiceNo': pd.Series.nunique
, 'InvoiceDate': max
}
)
# Calculate recency
rfmTable['InvoiceDate'] = pd.to_datetime(rfmTable['InvoiceDate'])
rfmTable['InvoiceDate'] = rfmTable.InvoiceDate.dt.date
today = rfmTable.InvoiceDate.max() #use the latest date in the dataset - in the real world this will be todays system date
rfmTable['Recency'] = (today - rfmTable['InvoiceDate']).dt.days #Days since last order
##########################################################################################################################################
### Stats Tests ###
##########################################################################################################################################
# first rename the columns to a more user friendly format
rfmTable = rfmTable.rename(columns={
'sales_value':'MonetaryValue', 'InvoiceNo':'Frequency', 'InvoiceDate':'LastOrderDate'
}
)
#show distribution of values
#recency
fig = px.histogram(rfmTable, x="Recency", y="CustomerID", marginal="box", # or violin, rug
hover_data=rfmTable.columns, title='Recency Plot')
fig.show()
#frequency
fig = px.histogram(rfmTable, x="Frequency", y="CustomerID", marginal="box", # or violin, rug
hover_data=rfmTable.columns, title='Frequency Plot')
fig.show()
#monetary value
fig = px.histogram(rfmTable, x="MonetaryValue", y="CustomerID", marginal="box", # or violin, rug
hover_data=rfmTable.columns, title='Monetary Value Plot')
fig.show()
#Q-Q plot of the quantiles of x versus the quantiles/ppf of a distribution.
# set up the plot figure
from statsmodels.graphics.gofplots import qqplot
from matplotlib import pyplot as plt
f, axes = plt.subplots(2, 2, figsize=(20,12))
#define distribution graphs
qqplot(rfmTable.Recency, line='r', ax=axes[0,0], label='Recency')
qqplot(rfmTable.Frequency, line='r', ax=axes[0,1], label='Frequency')
qqplot(rfmTable.MonetaryValue, line='r', ax=axes[1,0], label='MonetaryValue')
#plot all
plt.tight_layout()
##########################################################################################################################################
### RFM Score Function ###
##########################################################################################################################################
# Detemine the dataset quantiles
q = np.arange(0, 1, 0.10).tolist()
quantiles = rfmTable.quantile(q=np.around(q,decimals=2))
# Send the quantiles to the dictionary
quantiles = quantiles.to_dict()
# Start creating the RFM segmentation table
rfmSegmentation = rfmTable[['CustomerID','MonetaryValue','Frequency','Recency']]
# We created to classes where high recency is bad and high frequency/ money is good
# 1. Arguments (x = value, work on intervals of 90 days)
def RClass(x):
if x <= 60:
return 1
elif x <= 120:
return 2
elif x <= 180:
return 3
elif x <= 360:
return 4
elif x <= 540:
return 5
else:
return 6
# 2. Arguments (x = value, p = frequency)
def FClass(x,p,d):
if x <= d[p][0.3]:
return 6
elif x <= d[p][0.4]:
return 5
elif x <= d[p][0.6]:
return 4
elif x <= d[p][0.8]:
return 3
elif x <= d[p][0.9]:
return 2
else:
return 1
# 3. Arguments (x = value, p = monetary_value)
def MClass(x,p,d):
if x <= d[p][0.2]:
return 6
elif x <= d[p][0.4]:
return 5
elif x <= d[p][0.6]:
return 4
elif x <= d[p][0.8]:
return 3
elif x <= d[p][0.9]:
return 2
else:
return 1
# 4. Customer Segment Arguments (x = value, slice by value distribution in order to segment stage)
def CustomerSegment(x):
if x['R_Quartile'] ==1 and x['F_Quartile'] ==1 and x['M_Quartile'] ==1:
return "Champions"
elif x['R_Quartile'] <=2 and x['F_Quartile'] <=2 and x['M_Quartile'] <=2:
return "Loyal_Customers"
elif x['R_Quartile'] <=2 and x['F_Quartile'] <=3 and x['M_Quartile'] <=3:
return "Potential_Loyalists"
elif x['R_Quartile'] <=2 and x['F_Quartile'] <=4 and x['M_Quartile'] <=4:
return "Promising"
elif x['R_Quartile'] <=2 and x['F_Quartile'] <=6 and x['M_Quartile'] <=6:
return "Recent_Customers"
elif x['R_Quartile'] ==3 and x['F_Quartile'] <=3 and x['M_Quartile'] <=3:
return "Customer_Needs_Attention"
elif x['R_Quartile'] ==3 or x['R_Quartile'] ==4 and x['F_Quartile'] >=5 and x['M_Quartile'] >=5:
return "Hibernating"
elif x['R_Quartile'] ==4 and x['F_Quartile'] <=3 and x['M_Quartile'] <=3:
return "At_Risk"
elif x['R_Quartile'] ==4 and x['F_Quartile'] >=3 and x['M_Quartile'] >=3:
return "About_to_Sleep"
elif x['R_Quartile'] >=5 and x['F_Quartile'] >=3 and x['M_Quartile'] >=3:
return "Lost"
elif x['R_Quartile'] ==5 and x['F_Quartile'] <=3 and x['M_Quartile'] <=3:
return "Cant_Lose_Them"
elif x['R_Quartile'] ==6 and x['F_Quartile'] <=3 and x['M_Quartile'] <=3:
return "High_Value_Sleeping"
else:
return "Lost"
##########################################################################################################################################
### CALCULATE THE RFM SCORES ###
##########################################################################################################################################
# Scores
rfmSegmentation['R_Quartile'] = rfmSegmentation['Recency'].apply(RClass)
rfmSegmentation['F_Quartile'] = rfmSegmentation['Frequency'].apply(FClass, args=('Frequency',quantiles,))
rfmSegmentation['M_Quartile'] = rfmSegmentation['MonetaryValue'].apply(MClass, args=('MonetaryValue',quantiles,))
# Classify the RFM score for the customer base
rfmSegmentation['RFMClass'] = rfmSegmentation.R_Quartile.map(str) \
+ rfmSegmentation.F_Quartile.map(str) \
+ rfmSegmentation.M_Quartile.map(str)
# Classify customer segments based on RFM scores
rfmSegmentation['Customer Segment'] = rfmSegmentation.apply(lambda x: CustomerSegment(x), axis=1)
#scatter plot to display segments
rfm_scatter = rfmSegmentation[(rfmSegmentation['MonetaryValue'] > 0) & (rfmSegmentation['Recency'] <=360) & (rfmSegmentation['Frequency'] <= 50)]
fig = px.scatter(rfm_scatter, x="Recency", y="Frequency", color="Customer Segment",
size='MonetaryValue', hover_data=['R_Quartile', 'F_Quartile', 'M_Quartile'])
fig.show()
# Save the results to a csv file
rfmSegmentation.to_csv('rfm_segments.csv')
print('RFM Calculation Completed!')