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
import pandas as pd | |
from lifetimes.datasets import load_dataset | |
transactions = load_dataset( | |
filename='CDNOW_sample.txt', | |
header=None, | |
delim_whitespace=True, | |
names=['customer_id', 'customer_index', 'date', 'quantity', 'amount'], | |
converters={'date': lambda x: pd.to_datetime(x, format="%Y%m%d")} | |
) |
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
from lifetimes.utils import summary_data_from_transaction_data | |
rfm = summary_data_from_transaction_data(transactions=transactions, | |
customer_id_col='customer_id', | |
datetime_col='date', | |
monetary_value_col = 'amount', | |
observation_period_end=pd.to_datetime('1998-06-30'), | |
freq='W') | |
rfm.head(3) |
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
from lifetimes.utils import calibration_and_holdout_data | |
rfm_cal_holdout = calibration_and_holdout_data(transactions=transactions, | |
customer_id_col='customer_id', | |
datetime_col='date', | |
monetary_value_col = 'amount', | |
freq='W', | |
calibration_period_end='1998-01-01', | |
observation_period_end='1998-06-30' ) |
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
from lifetimes.utils import summary_data_from_transaction_data | |
rfm = summary_data_from_transaction_data(transactions=transactions, | |
customer_id_col='customer_id', | |
datetime_col='date', | |
monetary_value_col = 'amount', | |
observation_period_end=pd.to_datetime('1998-06-30'), | |
freq='W') | |
rfm.head(3) |
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
from lifetimes import BetaGeoFitter | |
# instantiation of BG-NBD model | |
bgf = BetaGeoFitter(penalizer_coef=0.0) | |
# fitting of BG-NBD model | |
bgf.fit(frequency=rfm_cal_holdout['frequency_cal'], | |
recency=rfm_cal_holdout['recency_cal'], | |
T=rfm_cal_holdout['T_cal']) |
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
bgf.summary |
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
from lifetimes.plotting import plot_period_transactions | |
_ = plot_period_transactions(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
# First, we choose a sample customer. | |
sample_customer = rfm_cal_holdout.iloc[20] | |
# Let's inspect his frequency, recency and T both for the calibration and observation periods: | |
sample_customer |
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
# This function calculates the conditional expected number of transactions in the given time length | |
n_transactions_pred = bgf.predict(t=26, # we set it to 26 weeks (the length of the observation period) | |
frequency=sample_customer['frequency_cal'], | |
recency=sample_customer['recency_cal'], | |
T=sample_customer['T_cal']) | |
n_transactions_pred # = 0.7647440846242359 |
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
alive_prob = bgf.conditional_probability_alive(frequency=sample_customer['frequency_cal'], | |
recency=sample_customer['recency_cal'], | |
T=sample_customer['T_cal']) | |
alive_prob # = 0.57089896 |
OlderNewer