Predicting customer churns without any labeled data in E-Commerce. | by Tony Liu | Towards Data Science

Do you ever feel confused or even frustrated when you are putting huge amount of effort into engaging your customers but you don’t really know who is going to engage? It all feels collapsed when you are going to spend 10k$ but only to get 5k$ revenue (potentially, to make it even worse). One thing that we found extremely useful in helping you to limit marketing cost is defining and predicting your churned/lapsed customers. With a precise model, you will be able to slash up to 80% of the cost and reduce a huge part of the uncertainty in your campaign strategy. Let’s jump right into today’s topic!
First, we want to talk about why it is hard to predict churns. But before that, we need to touch a bit of two different types of 2C (to customer) business models. No1 is where people subscribe and pre-pay which is called subscription model. No2 is where people come and buy without leaving any hint about when they will return for the next order, this is called transactional model.
What are some examples of a subscription model? I will pause 30 seconds for you to think of at least 2. My answer would be Apple Music, Spotify, Amazon Prime, New York Times, even your electricity, gas, garbage, sewer, cable is counted towards subscription. What is distinguished about a subscription model is an explicit contract. That means both parties know the money and time. This is a relatively easy case in churn modeling because of that.
What are some examples of a transactional model? Every brand that sells tangible products without signed repetition is a transactional model. You will now instantly understand why it is harder to model churns in this case because churn is really invisible and opaque.
We are going to talk about churns in a transactional model because we love challenges. But to overcome challenges, we need to be smarter. We know it is gonna be impossible to solve the question in a supervised learning framework. Why? Because as an employee, 99% of the time you find yourself struggling to ask for one more day for a project. When you have a supervised learning problem but you don’t have enough labeled data, usually the right solution is to find another approach rather than self-label 1000, 2000 data for yourself. Time matters. (see my other post about some pitfalls you will definitely fall into as a junior data scientist.)
So how do we tackle this problem? We need to turn around to another problem in marketing that is more broadly known and used, Customer Lifetime Value (CLV).
What does CLV do? CLV is rather a simple model that can estimate how much you can get from a customer given his/her historical behaviors. What makes it simple and effective is there are only three components in CLV analysis, frequency, recency, and monetary value. Frequency is the number of orders so far. Recency is the number of days between the first order and the last order. Almost everyone has a hard time remembering this definition because recency is related to “recent”, which in nature stands for what is the last time you did something? This is the opposite of the definition of recency. You need to flip it to remember it. Monetary value is the average price of all orders. In our churn model, we don’t care about money so much so we will leave it out for now.
Before proceeding, I want to ask the readers to pause for 30 seconds to think about why this model is so useful and popular in marketing. My answers would be a) it makes sense to everyone b) because it makes senses intuitively, it must have captured something in the nature of human behavior.
Now it is time to get real about the model. I would defer all the math to the original paper because not everyone here is signed up for that complexity. We care more about usability rather than theories. The basic idea for the model is this: we observe each customer by some time unit (day), each customer has some probability to order at any given day, and each customer has some probability to churn at any given day. We assume that those two probabilities are independent and constant over time. Then we can build some equations to depict transactional behaviors and use math to estimate the probabilities. Using Python, we can make the best of this package for it.
First, you need to install this package using this command:
pip install lifetimes
This package includes 4 models that can serve our churn models and if you are interested, you can check out GammaGammaFitter for a full evaluation of customer lifetimes value modeling journey.
I personally recommend using BetaGeoFitter or ModifiedBetaGeoFitter in your initial attempts. The reason is that they are relatively easier to converge and faster to train (this is also one of the important reasons why the original authors developed them instead of the classic ParetoNBDFitter). My experience with BetaGeoBetaBinomFitter told me that this model also takes more time than the other two models to train.
We need some training data now, you can get by typing this command in python
from lifetimes.datasets import load_cdnow_summarydata = load_cdnow_summary(index_col=[0])
I have some other data sources that I used for this post, but you can expect similar plots, graphs, and numbers generated using this example dataset.
Looking at the dataset, we have the following structure:
print(data.head())"""     frequency   recency      TID1    2           30.43       38.86"""
Frequency is the number of historical orders a certain customer has made. Recency is the number of days between the first discovery and the last purchase. T is the number of days a customer has been discovered. If you use your own dataset, you need to organize your data in the same way.
I am going to use ModifiedBetaGeoFitter in my post.
mbgf = ModifiedBetaGeoFitter(penalizer_coef=0.001)# penalizer_coef is helpful in converging the trainingmbgf.fit(summary_train['frequency'], summary_train['recency'], summary_train['T'])# display some parameters of the modeldisplay(mbgf)# <lifetimes.ModifiedBetaGeoFitter: fitted with xxx subjects, a: 1.10, alpha: 3.27, b: 0.05, r: 0.85>
So the model has been successfully trained and we want to get some visual senses of what it looks like:
plot_probability_alive_matrix(mbgf)
This graph depicts the relationships between Recency and Frequency against the likelihood of churn. Given a certain frequency, higher recency increases the likelihood of not churn. This makes sense because high recency means there are recent activities of the customer showing that they stick with the brand. Give a certain level of recency, higher frequency increases the likelihood of churn. This might look unintuitive because we often think that the more a customer bought the more likely they like the brand and the less likely they will go away. But it works otherwise if I explain it to you:
Let’s say that you run a supermarket and you have some loyal customers that you see at least every other day. Let’s call one of them Bella. She started to come since one year ago and she never missed a single day so you really think that she is loyal. One day you did not see her and the next day you did not see her again, and you start to talk to yourself “is everything going well with her? Am I going to lose her forever?”. Your worries instantly come about when you stop seeing some loyal customers since one or two days ago. As you care a lot about your dedicated people, you wouldn’t care much about someone who has only stopped by once a year. Now think about this likelihood of returning in terms of how much you worry if they will ever come back. Someone loyal stops visiting you is a much stronger signal of churning than you don’t see someone in 7 months but you only expect to see them once a year. It is this change of behavior that causes our worries and we really sense that.
Now we fully understand what projection this model can give us and next will be what our real projections are:
summary_train['mbgf_lh'] = mbgf.conditional_probability_alive(summary_train['frequency'], summary_train['recency'], summary_train['T'])summary_train['mbgf_lh'].plot.hist()
This plot shows the distribution of the likelihood of returning in the future. We notice that most people will be very unlikely to come back (this is mostly true in E-Commerce, most people just buy and leave and never come back). Now I have kept my promise and led you to the end of this journey.
I want to leave a homework for you and you can comment below: how can you use this plot to manage your E-Commerce business? Can you make money out of it? If so, how?
Some useful resources:
Professor Peter Fader talks about Customer Lifetime Value in Wharton.
His paper:
Counting Your Customers: Who Are They and What Will They Do Next?,