Survival analysis in Python for customer churn and LTV

Most churn tracking in WordPress and SaaS treats the question as binary classification: did they cancel or not? That framing is cheap to build, and it wrecks your forecasting accuracy, because it throws away the one dimension you care about most, which is time.

If you forecast customer lifetime value (LTV) with OLS or plain logistic regression, you are working from a stacked deck. Those models only really see the people who already left, which is survivorship bias in its purest form. The alternative is a Survival Analysis Python workflow built on time-to-event modeling.

Why censored customers break your models

Standard regression wants a finished story. In a live business, most of the stories are still running. A customer at 10 months who has not cancelled is not a zero in the not-churned column, they are censored. All you actually know is that they lasted at least 10 months.

Survival analysis models right censoring directly, so what you get is the probability that churn happens at a particular point in time instead of one flat score. If you already have a Python development workflow for data work, these models drop into it without much ceremony.

The Kaplan-Meier estimator and its staircase curve

Kaplan-Meier is the easiest place to start with Survival Analysis Python. It is non-parametric, so it makes no assumption about how your data is distributed. What comes out is a step-shaped survival curve: the cumulative probability that a customer is still with you at month one, month two, and onward.

# Implementing Kaplan-Meier with Lifelines
from lifelines import KaplanMeierFitter
import matplotlib.pyplot as plt

# kmf is the standard fitter
kmf = KaplanMeierFitter()
kmf.fit(durations=df['subscription_months'], 
        event_observed=df['churn_event'])

# Plotting the survival function
kmf.plot_survival_function()
plt.title('Customer Survival Curve')
plt.show()

Cox proportional hazards

Kaplan-Meier draws a good picture but it cannot take covariates, meaning the variables that actually move churn: support tickets opened, monthly spend, that sort of thing. That is what the Cox Proportional Hazard (CPH) model is for. The thinking is close to robust credit scoring models, where you also want to know how much each factor multiplies the risk.

CPH gives you a Hazard Ratio per variable. If complaints come back at 5.36, a customer who complains is 5.36 times more likely to churn at any given moment than one who does not. That is a number you can hand to a business owner and expect them to act on.

# Fitting the Cox Proportional Hazard Model
from lifelines import CoxPHFitter

cph = CoxPHFitter(penalizer=0.1)
cph.fit(df_model, 
        duration_col='Subscription_Length', 
        event_col='Churn')

# Inspect the coefficients and Hazard Ratios
cph.print_summary()
cph.plot()

Reading the output

  • Hazard Ratio > 1: the variable pushes churn risk up.
  • Hazard Ratio < 1: it protects the customer instead.
  • P-value: whether the variable means anything statistically.

The same models predict expected remaining life for a single customer. Rather than guessing who is about to leave, you can see when their risk peaks and fire retention hooks in WooCommerce through the REST API on that schedule.

If this Survival Analysis Python work is eating your dev hours, I can take it over. I have been working with WordPress since the 4.x days.

Final refactor

Going from binary classification to Survival Analysis Python models is like trading a paper map for a GPS: you get the when as well as the if. The lifelines library will take messy subscription data and give you LTV forecasts you can plan against. The Lifelines documentation has the implementation details.

author avatar
Ahmad Wael
I'm a WordPress and WooCommerce developer with 15+ years of experience building custom e-commerce solutions and plugins. I specialize in PHP development, following WordPress coding standards to deliver clean, maintainable code. Currently, I'm exploring AI and e-commerce by building multi-agent systems and SaaS products that integrate technologies like Google Gemini API with WordPress platforms, approaching every project with a commitment to performance, security, and exceptional user experience.