RFM and CLTV are two methods commonly utilized to analyze customer value. CLTV or CLV represents the amount of money a customer is expected to spend in your business during their lifetime and can be used to optimize your marketing efforts. RFM is commonly used for segmenting marketing strategies for different segments. CLTV helps you determine how much money you should spend acquiring new customers and retaining existing ones.
This article will walk you through how to use Pandas and Lifetimes modules to perform RFM and CLTV analyses in Python. The main focus will be CLTV, and we will create and add our RFM segments to our data so we can interpret our analysis of CLTV more effectively.
Photo by Rick Mason on Unsplash
For today’s analysis, we will use the Online Retail 2 dataset. This data set contains all of the transactions recorded for an online retailer based and registered in the UK between 2009–12–01 and 2011–12–09. The retailer specializes in all-occasion gift items. Most of the retailer’s customers are wholesalers. The dataset has the following attributes:
InvoiceNo
: Invoice number. Nominal. A 6-digit integral number is uniquely assigned to each transaction. If this code starts with the letter ‘C’, it indicates a cancellation.StockCode
: Product (item) code. Nominal. A 5-digit integral number is uniquely assigned to each distinct product.Description
: Product (item) name. Nominal.Quantity
: The quantities of each product (item) per transaction. Numeric.InvoiceDate
: Invoice date and time. Numeric. The day and time when a transaction was generated.UnitPrice
: Unit price. Numeric. Product price per unit in sterling (£).CustomerID
: Customer number. Nominal. A 5-digit integral number is uniquely assigned to each customer.Country
: Country name. Nominal. The name of the country where a customer resides.We will focus on purchases from UK customers between 2010–2011. Findings from Exploratory Data Analysis are as follows:
Refer to the data pane via this link:
https://datapane.com/u/hokay/reports/VkGyql3/df-raw/?utm_medium=embed
First, we drop null values on CustomerID. For outliers, since there are a lot of them, we don’t want to just drop all of them. Instead, we replace our outliers with ±1.5*IQR. Then we create our TotalPrice column which we will use to calculate our Monetary.
As stated in Lifetime’s module documentation, we have to put our data in a certain shape which consists of 4 columns and we will use CustomerIDas our index.
Frequency
: Number of repeat purchases the customer has made.T
: Customer’s first purchase and the end of the period under study. We will use weekly age on our analysis.Recency
: The duration between a customer’s first purchase and their latest purchase.Monetary
: Average value of a given customer’s purchases.The last InvoiceDate available in the dataset is 2011–12–09. We have to select a proper analysis date since there have been 10 years since the last entry and then start preparing our columns. We will calculate customer lifetime(tenure) on a weekly scale that’s why we are dividing our recency and T columns by 7.
After cleaning and preparation this is how our data looks like:
https://datapane.com/u/hokay/reports/9ArDxM7/cltv-df/
Before we dive into modelling, I want to talk about the theory behind our models. As stated before CLTV is the monetary value that a customer will bring to the company during the relationship and communication with a company. The most basic definition would be:
Since we are trying to predict our CLTV using probability with time projection we have to use 2 different models. The first one will be the Beta Geometric / Negative Binomial distribution model (BG/NBD) which will calculate the Expected Number of Transactions. And the second one will be Gamma Gamma Submodel which will calculate the Average Order Value.
For modelling, we first fit our Frequency
, Recency
and T
columns to BG/NBD model and Frequency
, Monetary
columns to our Gamma Gamma Submodel. Then we call the customer_lifetime_value
function on our Gamma model with BG/NBD as the first parameter. We also state our prediction length in months using time
parameters. We state the frequency we used to calculate our Tenure which in this case is ‘W’ for weekly. After calculation, the values are still hard to interpret so we should put them on a scale. Using MinMaxScaler from the scikit-learn module we put our CLTV predictions on a 0 to 1 scale.
This is how our data looks like after calculating and scaling CLTV:
Refer to this link for the data pane: https://datapane.com/u/hokay/reports/BAmpowk/cltv-final/
For better interpretation, we will add RFM segments to our CLTV table. This way we can compare our results from both methods. Let’s first create our RFM metrics, scores and segments.
This is how our RFM results look like:
https://datapane.com/u/hokay/reports/J35lD0A/rfm/
And this is the distribution of our RFM segments:
Adding our RFM segments to our CLTV will give us a more solid foundation for our interpretation. We will also add 1 month and 12 months projections to our table which will lead us to see some interesting findings of the effect of monetary on CLTV modelling.
This is how our data looks like after adding RFM segments, 1-month CLTV and 12-months CLTV projections:
Refer to the data pane:
https://datapane.com/u/hokay/reports/E7PXrY7/final-triple/
Since we try to create better segmentation and marketing strategies based on RFM and CLTV we will also create segments based on our CLTV. ‘A’ for our best customers and ‘D’ for the worst.
Our final table looks like this:
https://datapane.com/u/hokay/reports/aAMZX63/final-segmented/
I will discuss and interpret the results based on Pareto Principle. The Pareto principle states that for many outcomes, roughly 80% of consequences come from 20% of causes. In our case, we expect to see 80% of our income generated from our top 20% of customers. If we take a look at our segments from both analyses:
Refer to the below link for the data pane:
https://datapane.com/u/hokay/reports/O7vZKl7/top20rfmclv/
Most of our Champions, Loyal Customers and Potential Loyalists belong to A segment from our CLTV segmentation which makes sense. And if we look at how much value they brought to the business we can see Champions, Loyal Customers are generating roughly %80 of our income. For our top customers in these segments I would recommend the following:
Some important notes should be taken when using CLTV.
And since this is a model, there should be some metrics to evaluate the performance. To create a Frequency/Recency matrix that computes the expected number of transactions an artificial customer is to make in the next time period, given her recency and frequency:
from lifetimes.plotting import plot_frequency_recency_matrix plot_frequency_recency_matrix(bgf)
And to compare your data versus artificial data simulated with your fitted model’s parameters:
from lifetimes.plotting import plot_period_transactions plot_period_transactions(bgf)
CLTV helps you determine how much money you can afford to spend acquiring new customers and retaining existing ones and RFM can be used to segment your customers to better target your marketing efforts. All the efforts to perform these analyses have only one goal which is to make better business decisions based on data. Using CLTV and RFM simultaneously and interpreting data based on both analyses can help businesses grow.
Performing these analyses using Python and Lifetimes modules is not the most important and complicated part. Knowing your business, having domain knowledge and using that knowledge to produce useful results from these analyses is the key to a business’s success.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.