-
Notifications
You must be signed in to change notification settings - Fork 0
/
PROJECT_03_CLTV_PREDICTION.py
244 lines (191 loc) · 9.31 KB
/
PROJECT_03_CLTV_PREDICTION.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
##############################################################
# BG-NBD ve Gamma-Gamma ile CLTV Prediction
##############################################################
# 1.Data Preperation
# 2.BG/NBD ile Expected Sale Forecasting değerlerini hesapla
# 3.Gamma-Gamma ile Expected Average Profit değerlerini hesapla
# 4.BG/NBD ve Gamma Gamma modeli ile CLTV hesapla
##############################################################
# 1. Data Preperation
##############################################################
import datetime as dt
import pandas as pd
import matplotlib.pyplot as plt
from lifetimes import BetaGeoFitter
from lifetimes import GammaGammaFitter
from lifetimes.plotting import plot_period_transactions
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', 10)
pd.set_option('display.float_format', lambda x: '%.5f' % x)
def outlier_thresholds(dataframe, variable):
quartile1 = dataframe[variable].quantile(0.01)
quartile3 = dataframe[variable].quantile(0.99)
interquantile_range = quartile3 - quartile1
up_limit = quartile3 + 1.5 * interquantile_range
low_limit = quartile1 - 1.5 * interquantile_range
return low_limit, up_limit
def replace_with_thresholds(dataframe, variable):
low_limit, up_limit = outlier_thresholds(dataframe, variable)
# dataframe.loc[(dataframe[variable] < low_limit), variable] = low_limit
dataframe.loc[(dataframe[variable] > up_limit), variable] = up_limit
df_ =pd.read_excel("datasets/online_retail_II.xlsx",
sheet_name="Year 2010-2011")
df = df_.copy()
df.shape # (541910, 8)
df.head()
df.tail()
df.info()
df.describe([0.05, 0.25, 0.50, 0.75, 0.95, 0.99]).T
df.dropna(inplace=True)
df = df[~df["Invoice"].str.contains("C", na=False)]
df = df[df["Quantity"] > 0]
replace_with_thresholds(df, "Quantity")
replace_with_thresholds(df, "Price")
df.describe([0.05, 0.25, 0.50, 0.75, 0.95, 0.99, 1]).T
df["TotalPrice"] = df["Price"] * df["Quantity"]
df["InvoiceDate"].max()
today_date = dt.datetime(2011, 12, 11)
# UK müşterilerini seçme
df = df[df["Country"] == "United Kingdom"]
# df = df[df["Country"].str.contains("United Kingdom")]
df.head()
#############################################
# RFM Table
#############################################
# metrikleri oluşturma
rfm = df.groupby('Customer ID').agg({'InvoiceDate': [lambda date: (date.max()-date.min()).days,
lambda date: (today_date-date.min()).days],
'Invoice': lambda num: num.nunique(),
'TotalPrice': lambda price: price.sum()})
rfm.columns = rfm.columns.droplevel(0)
# sütunları isimlendirme
rfm.columns = ['recency_cltv_p', 'tenure', 'frequency', 'monetary']
# monetary avg hesaplama --> Gamma Gamma modeli bu şekilde istiyor
rfm["monetary"] = rfm["monetary"] / rfm["frequency"]
rfm.rename(columns={"monetary": "monetary_avg"}, inplace=True)
# recency ve tenure değişkenlerini haftalığa çevirme
rfm["recency_weekly_p"] = rfm["recency_cltv_p"] / 7
rfm["tenure_weekly_p"] = rfm["tenure"] / 7
# kontroller
rfm = rfm[rfm["monetary_avg"] > 0]
rfm = rfm[rfm["frequency"] > 1]
rfm["frequency"] = rfm["frequency"].astype(int)
##############################################################
# 2. BG/NBD modelinin kurulması
##############################################################
bgf = BetaGeoFitter(penalizer_coef=0.001)
bgf.fit(rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'])
# 6 ay içinde en çok satın alma beklediğimiz 10 müşteri kimdir?
bgf.conditional_expected_number_of_purchases_up_to_time(24,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p']).sort_values(ascending=False).head(10)
rfm["exp_sales_6_month"] = bgf.predict(24,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'])
rfm.sort_values("exp_sales_6_month", ascending=False).head(20)
# 6 Ayda Tüm Şirketin Beklenen Satış Sayısı Nedir? 952.4548865072431
bgf.predict(4*6,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p']).sum()
# Tahmin Sonuçlarının Değerlendirilmesi
plot_period_transactions(bgf)
plt.show()
##############################################################
# 3. GAMMA-GAMMA modelinin kurulması
##############################################################
ggf = GammaGammaFitter(penalizer_coef=0.01)
ggf.fit(rfm['frequency'], rfm['monetary_avg'])
ggf.conditional_expected_average_profit(rfm['frequency'],
rfm['monetary_avg']).sort_values(ascending=False).head(10)
rfm["expected_average_profit"] = ggf.conditional_expected_average_profit(rfm['frequency'],
rfm['monetary_avg'])
rfm.sort_values("expected_average_profit", ascending=False).head(20)
# geçmiş ayların ortalama satış sayılarını bulma?
##############################################################
# 4. BG-NBD ve GG modeli ile CLTV'nin hesaplanması.
##############################################################
# GÖREV - 1
# 2010-2011 UK müşterileri için 6 aylık CLTV prediction
cltv = ggf.customer_lifetime_value(bgf,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'],
rfm['monetary_avg'],
time=6,
freq="W",
discount_rate=0.01)
cltv.head()
cltv.shape
cltv = cltv.reset_index()
cltv.sort_values(by="clv", ascending=False).head(50)
rfm_cltv_final = rfm.merge(cltv, on="Customer ID", how="left")
rfm_cltv_final.sort_values(by="clv", ascending=False).head(7)
rfm_cltv_final[rfm_cltv_final["Customer ID"] == 12748.00000]
# GÖREV - 2
# 2010-2011 UK müşterileri için 1 aylık ve 12 aylık CLTV prediction
# - 1 aylık CLTV'de en yüksek olan 10 kişi ile 12 aylık'taki en yüksek 10 kişiyi analiz ediniz.
# - Fark var mı? Varsa sizce neden olabilir?
# 1 aylık
cltv = ggf.customer_lifetime_value(bgf,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'],
rfm['monetary_avg'],
time=1, # 1ay
freq="W",
discount_rate=0.01)
# 1 aylık CLTV'de en yüksek olan 10 kişi
cltv.head(10)
cltv.shape
cltv = cltv.reset_index()
cltv.sort_values(by="clv", ascending=False).head(50)
rfm_cltv_final = rfm.merge(cltv, on="Customer ID", how="left")
rfm_cltv_final.sort_values(by="clv", ascending=False).head()
# 12 aylık
cltv = ggf.customer_lifetime_value(bgf,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'],
rfm['monetary_avg'],
time=12, # 12ay
freq="W",
discount_rate=0.01)
# 12 aylık CLTV'de en yüksek olan 10 kişi
cltv.head(10)
cltv.shape
cltv = cltv.reset_index()
cltv.sort_values(by="clv", ascending=False).head(50)
rfm_cltv_final = rfm.merge(cltv, on="Customer ID", how="left")
rfm_cltv_final.sort_values(by="clv", ascending=False).head()
# GÖREV - 3
# 2010-2011 UK müşterileri için 6 aylık CLTV'ye göre tüm müşterilerinizi 3 gruba (segmente) ayırınız ve
# grup isimlerini veri setine ekleyiniz. Örneğin (C, B, A)
# CLTV'ye göre en iyi yüzde 20'yi seçiniz. Ve bunlara top_flag yazınız. yüzde 20'ye 1.
# diğerlerine 0 yazınız.
# 3 grubu veri setindeki diğer değişkenler açısıdan analiz ediniz.
# 3 grup için yönetime 6 aylık aksiyon önerilerinde bulununuz. Kısa kısa.
cltv = ggf.customer_lifetime_value(bgf,
rfm['frequency'],
rfm['recency_weekly_p'],
rfm['tenure_weekly_p'],
rfm['monetary_avg'],
time=6,
freq="W",
discount_rate=0.01)
cltv.head()
rfm_cltv_final = rfm.merge(cltv, on="Customer ID", how="left")
rfm_cltv_final["Segment"] = pd.qcut(rfm_cltv_final["clv"], 3, labels=["C", "B", "A"])
rfm_cltv_final.sort_values(by="clv", ascending=False).head(10)
cltv["clv"]
pareto = rfm_cltv_final.sort_values(by="clv", ascending=False)
pareto["clv"].shape[0] # 2570
pareto["clv"].shape[0] * 0.20 # 514.0
pareto["top_flag"] = pareto["clv"].shape[0]
pareto["top_flag"] = 0
pareto["top_flag"].iloc[0:514] = 1
rfm_cltv_final.groupby("Segment").agg({"sum", "count", "max"})