Skip to content

Instantly share code, notes, and snippets.

@meraldo-aliz
meraldo-aliz / plots.py
Created March 2, 2022 04:30
lifetimes
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
fig, (ax_gamma, ax_beta) = plt.subplots(ncols=1, nrows =2, figsize = (20, 16))
x_gamma = np.linspace(0, 10, 1000)
y_gamma = stats.gamma.pdf(x_gamma, a=bgf.params_["alpha"], scale=bgf.params_["r"])
ax_gamma.plot(x_gamma, y_gamma, "-")
ax_gamma.set_title(f'Gamma distribution (alpha = {bgf.params_["alpha"]:.2f}, scale (r) = {bgf.params_["r"]:.2f})')
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
@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
# 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
from lifetimes.plotting import plot_period_transactions
_ = plot_period_transactions(bgf)
bgf.summary
@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'])
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)