This file contains hidden or 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
| params_mean = pm.summary(trace_trunc, var_names = ["r", "alpha", "a", "b"])["mean"] | |
| params_mean |
This file contains hidden or 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 pymc3.math import log, exp, where | |
| import pymc3 as pm | |
| import numpy as np | |
| # We use the "calibration" portion of the dataset to train the model | |
| N = rfm_cal_holdout.shape[0] # number of customers | |
| x = rfm_cal_holdout['frequency_cal'].values # repeat purchase frequency | |
| t_x = rfm_cal_holdout['recency_cal'].values # recency | |
| T = rfm_cal_holdout['T_cal'].values # time since first purchase (T) |
This file contains hidden or 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 sklearn.metrics import mean_squared_error | |
| RMSE = mean_squared_error(y_true = rfm_cal_holdout["n_transactions_holdout_real"], | |
| y_pred = rfm_cal_holdout["n_transactions_holdout_pred"], | |
| squared = False) | |
| RMSE # = 1.3536793286521 |
This file contains hidden or 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
| # the real number of transactions in the observation period, which equals frequency_holdout + 1 | |
| rfm_cal_holdout["n_transactions_holdout_real"] = rfm_cal_holdout["frequency_holdout"] + 1 | |
| # the predicted number of transactions in the next 26 weeks (length of the observation period) | |
| rfm_cal_holdout["n_transactions_holdout_pred"] = bgf.predict(t=26, | |
| frequency=rfm_cal_holdout['frequency_cal'], | |
| recency=rfm_cal_holdout['recency_cal'], | |
| T=rfm_cal_holdout['T_cal']) | |
| # comparison of the real and predicted transactions |
This file contains hidden or 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
| VALUE_10_PRED_THRESHOLD = 10 | |
| # filtering for high-value customers | |
| rfm_cal_holdout.loc[rfm_cal_holdout["value_10_pred"]>VALUE_10_PRED_THRESHOLD, ["value_10_pred"]] |
This file contains hidden or 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 seaborn as sns | |
| fig, ax = plt.subplots(figsize = (12, 7)) | |
| ax = sns.histplot(rfm_cal_holdout["value_10_pred"], | |
| kde=False, | |
| binwidth = 2) | |
| ax.set_title(f'Customer value histogram') | |
| ax.set_xlabel(r'Customer value estimate for 10 periods ($)') | |
| ax.set_ylabel(r'Number of customers in each bin') | |
| ax.set_xlim(-2,20) |
This file contains hidden or 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_cal_holdout["value_10_pred"].describe() |
This file contains hidden or 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
| # the predicted number of transactions in the next 10 weeks | |
| rfm_cal_holdout["n_transactions_10_pred"] = bgf.predict(t=10, | |
| frequency=rfm_cal_holdout['frequency_cal'], | |
| recency=rfm_cal_holdout['recency_cal'], | |
| T=rfm_cal_holdout['T_cal']) | |
| # the probability of being alive | |
| rfm_cal_holdout["alive_prob"] = bgf.conditional_probability_alive(frequency=rfm_cal_holdout['frequency_cal'], |
This file contains hidden or 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_probability_alive_matrix | |
| _ = plot_probability_alive_matrix(bgf) |
This file contains hidden or 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_frequency_recency_matrix | |
| _ = plot_frequency_recency_matrix(bgf) |
NewerOlder