Logistic Regression forecast of Customer Churn
Overview
Customer churn occurs when a customer exits its relationship with a firm. That is, typically this means that the
customer has an account with the firm, and she closes the account.
Customer exit is an event, that is, for any period it can be indicated with a 0,1 variable.
(
Bernoulli Distribution)
A common tool for forecasting Bernmoulli type variabes is
Logistic Regression.
Tools
import sample as py
from datetime import datetime
import numpy as np
from sklearn import linear_model
customers, accounts = py.sample()
exited = [x for x in customers if 'closed' in x]
lengths = [py.months_between_iso(x['closed'], x['date']) for x in exited]
def logit(data, xs):
x = []
y = []
for record in data:
nrecord = []
for factor in xs:
nrecord.append(record[factor])
x.append(nrecord)
nrecord = []
if record['status'] == 'closed': nrecord.append(1)
else: nrecord.append(0)
y.append(nrecord)
pass
X = np.array(x)
Y = np.array(y)
clf = linear_model.LogisticRegression()
clf.fit(X,Y)
return clf
clf = logit(accounts,['age'])
print(clf.coef_)
print(clf.intercept_)
clf2 = logit(accounts,['age','account_age'])
print(clf2.coef_)
print(clf2.intercept_)
pass
Desktop