-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARL.py
186 lines (129 loc) · 6.59 KB
/
ARL.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
import datetime as dt
import pandas as pd
import pymysql
from sqlalchemy import create_engine
from sklearn.preprocessing import MinMaxScaler
from lifetimes import BetaGeoFitter
from lifetimes import GammaGammaFitter
from mlxtend.frequent_patterns import apriori, association_rules
pd.set_option('display.max_columns', None)
df= pd.read_excel("dataset/online_retail_II.xlsx",
sheet_name="Year 2010-2011")
df.head()
#Preprocess Data with the crm_data_prep Function
check_df(df)
df_prep = crm_data_prep(df)
check_df(df_prep)
# Creating Predictive CLTV Segments with the create_cltv_p Function
def create_cltv_p(dataframe):
today_date = dt.datetime(2011, 12, 11)
## recency for users dinamic.
rfm = dataframe.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 TotalPrice: TotalPrice.sum()})
rfm.columns = rfm.columns.droplevel(0)
## recency_cltv_p
rfm.columns = ['recency_cltv_p', 'T', 'frequency', 'monetary']
## simplified monetary_avg
rfm["monetary"] = rfm["monetary"] / rfm["frequency"]
rfm.rename(columns={"monetary": "monetary_avg"}, inplace=True)
# BGNBD CALCULATE WEEKLY RECENCY AND WEEKLY T for
## recency_weekly_cltv_p
rfm["recency_weekly_cltv_p"] = rfm["recency_cltv_p"] / 7
rfm["T_weekly"] = rfm["T"] / 7
# CONTROL
rfm = rfm[rfm["monetary_avg"] > 0]
## recency filtre (cltv_p for much better calculation)
rfm = rfm[(rfm['frequency'] > 1)]
rfm["frequency"] = rfm["frequency"].astype(int)
# BGNBD
bgf = BetaGeoFitter(penalizer_coef=0.01)
bgf.fit(rfm['frequency'],
rfm['recency_weekly_cltv_p'],
rfm['T_weekly'])
# exp_sales_1_month
rfm["exp_sales_1_month"] = bgf.predict(4,
rfm['frequency'],
rfm['recency_weekly_cltv_p'],
rfm['T_weekly'])
# exp_sales_3_month
rfm["exp_sales_3_month"] = bgf.predict(12,
rfm['frequency'],
rfm['recency_weekly_cltv_p'],
rfm['T_weekly'])
# expected_average_profit
ggf = GammaGammaFitter(penalizer_coef=0.01)
ggf.fit(rfm['frequency'], rfm['monetary_avg'])
rfm["expected_average_profit"] = ggf.conditional_expected_average_profit(rfm['frequency'],
rfm['monetary_avg'])
# 6 months cltv_p
cltv = ggf.customer_lifetime_value(bgf,
rfm['frequency'],
rfm['recency_weekly_cltv_p'],
rfm['T_weekly'],
rfm['monetary_avg'],
time=6,
freq="W",
discount_rate=0.01)
rfm["cltv_p"] = cltv
# minmaxscaler
scaler = MinMaxScaler(feature_range=(1, 100))
scaler.fit(rfm[["cltv_p"]])
rfm["cltv_p"] = scaler.transform(rfm[["cltv_p"]])
# cltv_p_segment
rfm["cltv_p_segment"] = pd.qcut(rfm["cltv_p"], 3, labels=["C", "B", "A"])
## recency_cltv_p, recency_weekly_cltv_p
rfm = rfm[["recency_cltv_p", "T", "monetary_avg", "recency_weekly_cltv_p", "T_weekly",
"exp_sales_1_month", "exp_sales_3_month", "expected_average_profit",
"cltv_p", "cltv_p_segment"]]
return rfm
cltv_p = create_cltv_p(df_prep)
check_df(cltv_p)
cltv_p.head()
cltv_p.groupby("cltv_p_segment").agg({"count", "mean"})
# Reducing the data set according to the user ids of the desired segments.
#receiving ids
a_segment_ids = cltv_p[cltv_p["cltv_p_segment"] == "A"].index
b_segment_ids = cltv_p[cltv_p["cltv_p_segment"] == "B"].index
c_segment_ids = cltv_p[cltv_p["cltv_p_segment"] == "C"].index
# reduction of df's according to these ids
a_segment_df = df_prep[df_prep["Customer ID"].isin(a_segment_ids)]
b_segment_df = df_prep[df_prep["Customer ID"].isin(b_segment_ids)]
c_segment_df = df_prep[df_prep["Customer ID"].isin(c_segment_ids)]
# Creating association rules for each segment
def create_rules(dataframe, country=False, head=5):
if country:
dataframe = dataframe[dataframe['Country'] == country]
dataframe = create_invoice_product_df(dataframe)
frequent_itemsets = apriori(dataframe, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="support", min_threshold=0.01)
print(rules.sort_values("lift", ascending=False).head(head))
else:
dataframe = create_invoice_product_df(dataframe)
frequent_itemsets = apriori(dataframe, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="support", min_threshold=0.01)
print(rules.sort_values("lift", ascending=False).head(head))
return rules
rules_a = create_rules(a_segment_df)
product_a = int(rules_a["consequents"].apply(lambda x: list(x)[0]).astype("unicode")[0])
rules_b = create_rules(b_segment_df)
product_b = int(rules_b["consequents"].apply(lambda x: list(x)[0]).astype("unicode")[0])
rules_c = create_rules(c_segment_df)
product_c = int(rules_c["consequents"].apply(lambda x: list(x)[0]).astype("unicode")[0])
def check_id(stock_code):
product_name = df_prep[df_prep["StockCode"] == stock_code][["Description"]].values[0].tolist()
return print(product_name)
check_id(20719)
#Recommendations to German Customers by Segments
gr_ids = df_prep[df_prep["Country"] == "Germany"]["Customer ID"].drop_duplicates()
cltv_p["recommended_product"] = ""
cltv_p.loc[cltv_p.index.isin(gr_ids)]
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "A")]
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "A"), "recommended_product"] = product_a
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "A")]
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "B"), "recommended_product"] = product_b
cltv_p.loc[(cltv_p.index.isin(gr)) & (cltv_p["cltv_p_segment"] == "B")]
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "C"), "recommended_product"] = product_c
cltv_p.loc[(cltv_p.index.isin(gr_ids)) & (cltv_p["cltv_p_segment"] == "C")]
cltv_p.loc[cltv_p.index.isin(gr_ids)]