This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CustomerID | RFM Segment | Recency | T | Frequency | Monetary | 1M CLV | 1M Scaled CLV | 12M CLV | 12M Scaled CLV | |
---|---|---|---|---|---|---|---|---|---|---|
16000 | potential_loyalists | 0.0 | 0.428571 | 3 | 2055.786667 | 3843.408761 | 0.258216 | 39233.195047 | 0.239831 | |
15061 | champions | 52.57143 | 53.28571 | 48 | 1108.30781 | 3670.30953 | 0.24659 | 40347.77563 | 0.24664 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final["cltv_segment"] = pd.qcut(final["6m_scaled_clv"], 4, labels=["D", "C", "B", "A"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final = rfm[['rfm_segment']].merge(cltv_final, on='Customer ID') # Merging RFM | |
final.rename(columns={'scaled_clv': '6m_scaled_clv', 'clv': '6m_clv'}, | |
inplace=True) | |
final = add_clv(final, cltv_df, 1) | |
final = add_clv(final, cltv_df, 12) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rfm = create_rfm(df) # Creating RFM | |
rfm.rename(columns={'segment': 'rfm_segment'}, inplace=True) | |
rfm.index = rfm.index.astype('int64') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Models | |
bgf = BetaGeoFitter(penalizer_coef=0.001) | |
bgf.fit(cltv_df['frequency'], | |
cltv_df['recency'], | |
cltv_df['T']) | |
ggf = GammaGammaFitter(penalizer_coef=0.01) | |
ggf.fit(cltv_df['frequency'], cltv_df['monetary']) | |
cltv = ggf.customer_lifetime_value(bgf, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
replace_with_thresholds(df, 'Price') | |
replace_with_thresholds(df, 'Quantity') | |
df["TotalPrice"] = df["Quantity"] * df["Price"] | |
today_date = dt.datetime(2011, 12, 11) # Data we use is old. We need a proper analysis date. | |
cltv_df = df.groupby('Customer ID').agg( | |
{'InvoiceDate': [lambda date: (date.max() - date.min()).days, | |
lambda date: (today_date - date.min()).days], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Unique Sources and Frequencies | |
df.SOURCE.value_counts() | |
# Unique Prices and Frequencies | |
df.PRICE.value_counts() | |
# Unique Countries and Frequencies | |
df.COUNTRY.value_counts() | |
# Total Income Accumulated by Countries |