Understanding what drives customer churn is critical to business success. While there is always going to be natural churn that you can’t prevent, the most common reasons for churn are invariably linked to your operational service quality, and how your customer service team handles problems when they arise.
If you can identify the specific causes of churn, you can try to improve in those areas, and your retention rate should go up. One way to achieve this is through survival analysis, which looks at the factors associated with the loss of customers over time.
In this project, we’ll use Cox’s Proportional Hazards model to examine a contractual churn dataset to quickly identify the factors associated with the loss of customers. This only scratches the surface of what you can do, but it’s often all that’s needed to find what needs fixing. Here’s how it’s done.
I’m using a contractual churn dataset for this project. These are easier to work with than non-contractual churn datasets, because the time of a customer’s “death” (in CLV terms, not literally) is measurable. If you’re in a non-contractual business, such as online retail, you will need to model the time of death using a model such as BG/NBD.
Each row in your dataset needs to represent a customer. You’ll require a column containing your target variable (i.e.
churn) and a time-based metric, such as
tenure based on the number of days between “birth” and “death”, plus a wide range of features. Certain features are commonly associated with churn, so try to extract these if you can.
|Service quality||Customers who experience operational service quality issues, such as damages, delays, incorrect or incomplete orders, and even returns or service that was just "meh", are commonly associated with churn. Capturing data on these factors can show you how they're linked.|
|Service contact||Customers generally only make contact with your customer service team when something goes wrong (usually for one of the reasons above) so it makes sense that the number of service contacts, and their type, are associated with churn.
In settings where customers interact online, they generally expect to have issues resolved online, so making them ring a call centre is often a turn off. Measure how they made contact and how many times it happened.
|Customer service quality||Next there's the quality of customer service they received when they made contact. Online retailers often love to knock Amazon, but they're actually bloody brilliant when it comes to this. Staff are polite, they actually apologise for the issue (even when not their fault), they give refunds almost instantaneously, and rarely quibble.
If customers complain about your policies, you definitely need to record it, as it's usually highly correlated with churn. I've yet to run a service recovery test where it hasn't made a massive contribution towards reversing the trend, so record this too.
|Pricing||Finally, there are price-related issues. These often go unnoticed because the people who set prices are generally thinking about margin and not customer retention, but putting up product or delivery prices can mean customers don't come back. They are, however, much harder to capture, especially when multiple products are purchased.|
Customers who call are more likely to churn. Picture by Pixabay, Pexels.
Open a new Python script or Jupyter notebook and import the
matplotlib packages. Any packages you don’t have can be installed by entering
pip3 install package-name in your terminal. Lifelines will be doing the bulk of the work here.
import pandas as pd import numpy as np from lifelines import CoxPHFitter import matplotlib.pyplot as plt
You can use your own dataset for this if you like. I’m using a churn dataset from a telecoms provider. This comprises one row per customer and includes a boolean
churn metric, and a range of features that we can examine to see what influences churn and retention the most.
df = pd.read_csv('train.csv') df.head().T
To prepare the data for use by the model we need to binarise the boolean values stored in the
voice_mail_plan columns. Rather than one-hot encoding them, which would introduce multi-collinearity (since yes is the opposite of no). I’ve used
np.where() to replace the
yes with a
df['churn'] = np.where(df['churn'].str.contains('yes'), 1, 0) df['international_plan'] = np.where(df['international_plan'].str.contains('yes'), 1, 0) df['voice_mail_plan'] = np.where(df['voice_mail_plan'].str.contains('yes'), 1, 0)
To simplify the output, I have dropped the
state column, but there is actually useful data in here. It simply makes the dataframes and plots too big to see easily. Finally, I’ve used
get_dummies() to one-hot encode the area code field, which is quite low cardinality.
df = df.drop(columns=['state']) df = pd.get_dummies(df)
Next we can fit Cox’s Proportional Hazards model to our data using the
CoxPHFitter(). When you fit the model, you’ll need to pass in your dataframe, the duration column (i.e.
account_length, and the event column (i.e.
If you run into issues with model convergence, you may need to pass in a
penalizer value as a workaround. The Lifelines documentation provides guidance on resolving this.
cph = CoxPHFitter(penalizer=0.1) cph.fit(df, 'account_length', event_col='churn')
<lifelines.CoxPHFitter: fitted with 4250 total observations, 3652 right-censored observations>
If you use
print_summary() on your
CoxPHFitter model object you’ll get a detailed breakdown of the model configuration used and the coefficients identied. As you can see by the high value for
international_plan, this has a
0.95 coefficient and is highly correlated with attrition. It’s easier to examine these factors in a plot, so we’ll tackle that next.
|number of observations||4250|
|number of events observed||598|
|time fit was run||2021-01-19 16:56:16 UTC|
|coef||exp(coef)||se(coef)||coef lower 95%||coef upper 95%||exp(coef) lower 95%||exp(coef) upper 95%||z||p||-log2(p)|
|log-likelihood ratio test||395.89 on 19 df|
|-log2(p) of ll-ratio test||237.53|
By appending the
plot() function to our
cph object containing the fitted model we can visualise the coefficients.
plt.figure(figsize=(10,10)) cox = cph.plot()
This is only a very basic example of what you can do (partly limited by the lack of time series data in this dataset), but it also shows you how easy it is to spot what’s going wrong, so you can investigate and fix the issues. Here’s what we found and what action needs to be taken.
|International calls||Customers on the international plan are significantly more likely to churn. Obviously, there's also a link with the total international charges metric, so perhaps they're leaving because the prices are too high.|
|Customer service calls||The next big issue is with the number of customer of service calls placed. This is generally one of the top reasons for churn anywhere, so it's vital you try to identify what went wrong and caused these customers to call.
Could customers have resolved their issue online? Were they on hold for ages? Did it take several calls to get an adequate resolution? Did staff actually apologise for the problem? Was service recovery used to try and turn things around?
|Voice mail plan||Customers on the voice mail plan are least likely to churn, so there might be something the company can do here. Perhaps aiming to acquire voice mail customers, encouraging others to move to this plan, or examining why the customers here are less likely to churn than the rest would help.|
Matt Clarke, Sunday, March 14, 2021
Learn the fundamentals of gradient boosting and build state-of-the-art machine learning models using XGBoost to solve classification and regression problems.Start course for FREE