Beispiel #1
0
    def test_plot_incremental_transactions(self):
        """Test plotting incremental transactions with CDNOW example."""
        transactions = load_dataset('CDNOW_sample.txt', header=None, sep='\s+')
        transactions.columns = [
            'id_total', 'id_sample', 'date', 'num_cd_purc', 'total_value'
        ]
        t = 39
        freq = 'W'

        transactions_summary = utils.summary_data_from_transaction_data(
            transactions,
            'id_sample',
            'date',
            datetime_format='%Y%m%d',
            observation_period_end='19970930',
            freq=freq)

        bgf = BetaGeoFitter(penalizer_coef=0.01)
        bgf.fit(transactions_summary['frequency'],
                transactions_summary['recency'], transactions_summary['T'])

        plt.figure()
        plotting.plot_incremental_transactions(bgf,
                                               transactions,
                                               'date',
                                               'id_sample',
                                               2 * t,
                                               t,
                                               freq=freq,
                                               xlabel='week',
                                               datetime_format='%Y%m%d')
        return plt.gcf()
    def test_plot_incremental_transactions(self, cdnow_transactions, bgf_transactions):
        """Test plotting incremental transactions with CDNOW example."""
        t = 39
        freq = 'W'

        plt.figure()
        plotting.plot_incremental_transactions(
            bgf_transactions, cdnow_transactions, 'date', 'id_sample', 2 * t, t, freq=freq,
            xlabel='week', datetime_format='%Y%m%d')
        return plt.gcf()
def evaluation_plots(plot_type):
    """
    Evaluation Plots:
    - Tracking Cumulative Transactions
    - Tracking Daily Transactions
    - Frequency of Repeated Transactions
    - Calibration vs Holdout.

    Parameters
    ----------
        plot_type: str.
            "tracking" - Tracking Cumulative and Tracking Daily Transactions.
            "repeated" - Frequency of Repeated Transactions.
            "calibration_holdout" - Calibration vs Holdout Purchases.
    """
    # Loading Calibration Model.
    cal_bg_nbd = BetaGeoFitter(penalizer_coef=0.0)
    cal_bg_nbd.load_model(path="models/calibration_model.pkl")

    # Loading summary_cal_holdout dataset.
    summary_cal_holdout = pd.read_csv("datasets/summary_cal_holdout.csv")

    # Loading Transactions.
    transactions = pd.read_csv("datasets/transactions.csv")

    if plot_type == "tracking":
        fig = plt.figure(figsize=(20, 4))
        plot_cumulative_transactions(model=cal_bg_nbd,
                                     transactions=transactions,
                                     datetime_col="order_purchase_timestamp",
                                     customer_id_col="customer_unique_id",
                                     t=604,
                                     t_cal=512,
                                     freq="D",
                                     ax=fig.add_subplot(121))

        plot_incremental_transactions(model=cal_bg_nbd,
                                      transactions=transactions,
                                      datetime_col="order_purchase_timestamp",
                                      customer_id_col="customer_unique_id",
                                      t=604,
                                      t_cal=512,
                                      freq="D",
                                      ax=fig.add_subplot(122))
    elif plot_type == "repeated":
        plot_period_transactions(model=cal_bg_nbd)

    elif plot_type == "calibration_holdout":
        plot_calibration_purchases_vs_holdout_purchases(
            model=cal_bg_nbd, calibration_holdout_matrix=summary_cal_holdout)
    return
Beispiel #4
0
    def test_plot_incremental_transactions(self, cdnow_transactions,
                                           bgf_transactions):
        """Test plotting incremental transactions with CDNOW example."""
        actual = [
            np.nan, 3.0, 14.0, 27.0, 23.0, 55.0, 51.0, 67.0, 73.0, 62.0, 91.0,
            89.0, 100.0, 84.0, 86.0, 76.0, 69.0, 63.0, 58.0, 68.0, 58.0, 60.0,
            48.0, 42.0, 77.0, 84.0, 56.0, 48.0, 43.0, 66.0, 72.0, 33.0, 48.0,
            60.0, 48.0, 49.0, 43.0, 47.0, 54.0, 53.0, 51.0, 45.0, 57.0, 51.0,
            71.0, 59.0, 54.0, 49.0, 54.0, 66.0, 55.0, 31.0, 42.0, 50.0, 40.0,
            34.0, 42.0, 41.0, 44.0, 42.0, 57.0, 61.0, 56.0, 56.0, 61.0, 36.0,
            39.0, 40.0, 29.0, 38.0, 41.0, 30.0, 32.0, 40.0, 45.0, 52.0, 28.0,
            34.0
        ]
        predicted = [
            np.nan, 10.79, 17.52, 24.76, 32.23, 40.38, 47.16, 54.12, 61.71,
            68.88, 75.35, 80.49, 81.0, 79.08, 77.38, 75.85, 74.47, 73.21,
            72.06, 71.0, 70.01, 69.09, 68.24, 67.43, 66.68, 65.97, 65.29,
            64.66, 64.05, 63.47, 62.92, 62.4, 61.9, 61.41, 60.95, 60.51, 60.08,
            59.67, 59.28, 58.9, 58.53, 58.17, 57.82, 57.49, 57.17, 56.85,
            56.55, 56.25, 55.96, 55.68, 55.41, 55.15, 54.89, 54.64, 54.39,
            54.15, 53.92, 53.69, 53.47, 53.25, 53.04, 52.83, 52.62, 52.42,
            52.23, 52.04, 51.85, 51.67, 51.48, 51.31, 51.13, 50.96, 50.8,
            50.63, 50.47, 50.31, 50.16, 50.0
        ]
        labels = ['actual', 'predicted']
        t = 39
        freq = 'W'

        ax = plotting.plot_incremental_transactions(bgf_transactions,
                                                    cdnow_transactions,
                                                    'date',
                                                    'id_sample',
                                                    2 * t,
                                                    t,
                                                    freq=freq,
                                                    xlabel='week',
                                                    datetime_format='%Y%m%d')

        lines = ax.lines
        legend = ax.legend_

        actual_y = lines[0].get_data()[1]
        predicted_y = lines[1].get_data()[1]

        assert_allclose(actual, actual_y, rtol=0.01)
        assert_allclose(predicted, predicted_y, rtol=0.01)
        assert_array_equal([e.get_text() for e in legend.get_texts()], labels)
        assert_equal(ax.title.get_text(), "Tracking Daily Transactions")
        assert_equal(ax.xaxis.get_label().get_text(), "week")
        assert_equal(ax.yaxis.get_label().get_text(), "Transactions")
        plt.close()