Skip to content

Commit

Permalink
adding example for customer history to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Apr 21, 2015
1 parent a0bbb81 commit 821e010
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -181,7 +181,7 @@ With this dataset, we can perform fitting on the `_cal` columns, and test on the

#### Customer Predicitions

Basic on customer history, we can predict what an individuals future purchases might look like:
Based on customer history, we can predict what an individuals future purchases might look like:

t = 10 #predict purchases in 10 periods
individual = summary.iloc[20]
Expand All @@ -190,6 +190,21 @@ Basic on customer history, we can predict what an individuals future purchases m
# 0.0576511


### Customer Probability Histories

Given a customer transaction history, we can calculate their historical probability of being alive, according to
our trained model. For example:

from lifetimes.plotting import plot_history_alive

id = 35
days_since_birth = 200
sp_trans = transaction_data.ix[transaction_data['id'] == id]
plot_history_alive(bgf, days_since_birth, sp_trans, 'date')

![history](http://i.imgur.com/y45tum4.png)


## Questions? Comments?

Drop me a line at [`@cmrn_dp`](https://twitter.com/Cmrn_DP)!
Expand Down
2 changes: 1 addition & 1 deletion lifetimes/plotting.py
Expand Up @@ -198,7 +198,7 @@ def plot_history_alive(model, t, transactions, datetime_col, freq='D', **kwargs)
customer_history = customer_history.resample(freq, how='sum').reset_index()

# plot alive_path
path = calculate_alive_path(model, transactions, datetime_col, units, freq)
path = calculate_alive_path(model, transactions, datetime_col, t, freq)
path_dates = pd.date_range(start=min(transactions[datetime_col]), periods=len(path), freq=freq)
plt.plot(path_dates, path, '-', label='P_alive')

Expand Down
2 changes: 1 addition & 1 deletion lifetimes/utils.py
Expand Up @@ -16,7 +16,7 @@ def coalesce(*args):


def calibration_and_holdout_data(transactions, customer_id_col, datetime_col, calibration_period_end,
observation_period_end=datetime.today(), freq='D', datetime_format=None,):
observation_period_end=datetime.today(), freq='D', datetime_format=None):
"""
This function creates a summary of each customer over a calibration and holdout period (training and testing, respectively).
It accepts transition data, and returns a Dataframe of sufficient statistics.
Expand Down
18 changes: 9 additions & 9 deletions tests/test_plotting.py
Expand Up @@ -6,9 +6,9 @@
from lifetimes import BetaGeoFitter


bfg = BetaGeoFitter()
bgf = BetaGeoFitter()
data = pd.read_csv('lifetimes/datasets/cdnow_customers.csv', index_col=[0])
bfg.fit(data['frequency'], data['recency'], data['T'], iterative_fitting=0)
bgf.fit(data['frequency'], data['recency'], data['T'], iterative_fitting=0)

@pytest.mark.plottest
@pytest.mark.skipif("DISPLAY" not in os.environ, reason="requires display")
Expand All @@ -18,34 +18,34 @@ def test_plot_period_transactions(self):
from matplotlib import pyplot as plt

plt.figure()
plotting.plot_period_transactions(bfg)
plotting.plot_period_transactions(bgf)

plt.figure()
plotting.plot_period_transactions(bfg, bins=range(5))
plotting.plot_period_transactions(bgf, bins=range(5))

plt.figure()
plotting.plot_period_transactions(bfg, label=['A', 'B'])
plotting.plot_period_transactions(bgf, label=['A', 'B'])
plt.show()

def test_plot_frequency_recency_matrix(self):
from matplotlib import pyplot as plt

plt.figure()
plotting.plot_frequency_recency_matrix(bfg)
plotting.plot_frequency_recency_matrix(bgf)

plt.figure()
plotting.plot_frequency_recency_matrix(bfg, max_recency=100, max_frequency=50)
plotting.plot_frequency_recency_matrix(bgf, max_recency=100, max_frequency=50)

plt.show()

def test_plot_expected_repeat_purchases(self):
from matplotlib import pyplot as plt

plt.figure()
plotting.plot_expected_repeat_purchases(bfg)
plotting.plot_expected_repeat_purchases(bgf)

plt.figure()
plotting.plot_expected_repeat_purchases(bfg, label='test label')
plotting.plot_expected_repeat_purchases(bgf, label='test label')

plt.show()

Expand Down

0 comments on commit 821e010

Please sign in to comment.