Skip to content

Instantly share code, notes, and snippets.

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")}
)
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)
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' )
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)
@meraldo-aliz
meraldo-aliz / bgf.py
Created March 2, 2022 03:49
lifetimes
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'])
bgf.summary
from lifetimes.plotting import plot_period_transactions
_ = plot_period_transactions(bgf)
# 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
@meraldo-aliz
meraldo-aliz / predict.py
Last active March 2, 2022 04:12
lifetimes
# 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
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