Lifetimes

0.11.3 · active · verified Thu Apr 16

Lifetimes is a Python library for measuring customer lifetime value (CLV) using probabilistic models. It provides implementations of various statistical models like BG/NBD, Pareto/NBD, and Gamma-Gamma, along with utilities for data preparation, fitting, and prediction. The current version is 0.11.3, and it has a moderate release cadence with several updates per year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to prepare transaction data into a Recency, Frequency, Monetary (RFM) format using `summary_data_from_transaction_data`, fit a `BetaGeoFitter` model, and then use the fitted model to predict future purchases and calculate the probability of customers being 'alive'.

import pandas as pd
from lifetimes import BetaGeoFitter
from lifetimes.utils import summary_data_from_transaction_data

# Sample transaction data (replace with your actual data)
transactions = pd.DataFrame({
    'customer_id': ['A', 'A', 'B', 'B', 'C', 'D'],
    'transaction_id': [1, 2, 3, 4, 5, 6],
    'date': pd.to_datetime(['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-10', '2023-03-01', '2023-01-05']),
    'price': [10.0, 20.0, 15.0, 25.0, 30.0, 5.0]
})

# Convert transaction data to RFM (Recency, Frequency, Monetary) format
# The observation_period_end can be adjusted to your data's last date
rfm_data = summary_data_from_transaction_data(
    transactions, 
    customer_id_col='customer_id', 
    datetime_col='date',
    observation_period_end=pd.to_datetime('2023-03-31')
)

print("RFM Data:\n", rfm_data.head())

# Initialize and fit the BetaGeoFitter model
bgf = BetaGeoFitter(penalizer_coef=0.1) # Add a penalizer for stability
bgf.fit(rfm_data['frequency'], rfm_data['recency'], rfm_data['T'])

print("\nModel Parameters:\n", bgf.params_)

# Predict future purchases for the next 7 periods
# (e.g., if T is in days, this is 7 days)
prediction_days = 7
predicted_purchases = bgf.predict(
    prediction_days, rfm_data['frequency'], rfm_data['recency'], rfm_data['T']
)

print(f"\nPredicted purchases in the next {prediction_days} days:\n", predicted_purchases.head())

# Calculate customer probability of being 'alive'
# This is useful for understanding customer churn/retention
alive_prob = bgf.conditional_probability_of_being_alive(
    rfm_data['frequency'], rfm_data['recency'], rfm_data['T']
)
print("\nProbability of being alive:\n", alive_prob.head())

view raw JSON →