快速上手客户流失简单建模分析

快速上手客户流失模型分析

1、处理客户流失数据集
客户流失数据集是一个记录电信公司现有的和曾经的客户的数据文件,有1个输出变量和20个输入变量。输出变量是一个布尔型变量(True/False),表示客户是否已经流失。输入变量是客户的电话计划和通话行为的特征,包括状态、账户时间、区号、电话号码、是否有国际通话计划、是否有语音信箱、语音信箱消息数量、白天通话时长、白天通话次数、白天通话费用、傍晚通话时长、傍晚通话次数、傍晚通话费用、夜间通话时长、夜间通话次数、夜间通话费用、国际通话时长、国际通话次数、国际通话费用和客户服务通话次数。
数据集地址:https://raw.githubusercontent.com/EricChiang/churn/master/data/churn.csv

#导入需要使用的包
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#statsmodels提供对许多不同统计模型估计的类和函数
import statsmodels.api as sm  
import statsmodels.formula.api as smf

创建一个新列churn01,并使用numpy的where函数根据churn这一列中的值用1或0来填充它。churn这一列中的值不是True就是False,所以如果churn中的值是True,那么churn01中的值就是1,如果churn中的值是False,那么churn01中的值就是 0。

# 使用pandas读取数据集
churn = pd.read_csv('churn.csv', sep=',', header=0)
churn.columns = [heading.lower() for heading in \
churn.columns.str.replace(' ', '_').str.replace("\'", "").str.strip('?')]
churn['churn01'] = np.where(churn['churn'] == 'True.', 1., 0.)
churn.head(16)
stateaccount_lengtharea_codephoneintl_planvmail_planvmail_messageday_minsday_callsday_charge...eve_chargenight_minsnight_callsnight_chargeintl_minsintl_callsintl_chargecustserv_callschurnchurn01
0KS128415382-4657noyes25265.111045.07...16.78244.79111.0110.032.701False.0.0
1OH107415371-7191noyes26161.612327.47...16.62254.410311.4513.733.701False.0.0
2NJ137415358-1921nono0243.411441.38...10.30162.61047.3212.253.290False.0.0
3OH84408375-9999yesno0299.47150.90...5.26196.9898.866.671.782False.0.0
4OK75415330-6626yesno0166.711328.34...12.61186.91218.4110.132.733False.0.0
5AL118510391-8027yesno0223.49837.98...18.75203.91189.186.361.700False.0.0
6MA121510355-9993noyes24218.28837.09...29.62212.61189.577.572.033False.0.0
7MO147415329-9001yesno0157.07926.69...8.76211.8969.537.161.920False.0.0
8LA117408335-4719nono0184.59731.37...29.89215.8909.718.742.351False.0.0
9WV141415330-8173yesyes37258.68443.96...18.87326.49714.6911.253.020False.0.0
10IN65415329-6603nono0129.113721.95...19.42208.81119.4012.763.434True.1.0
11RI74415344-9403nono0187.712731.91...13.89196.0948.829.152.460False.0.0
12IA168408363-1107nono0128.89621.90...8.92141.11286.3511.223.021False.0.0
13MT95510394-8006nono0156.68826.62...21.05192.31158.6512.353.323False.0.0
14IA62415366-9238nono0120.77020.52...26.11203.0999.1413.163.544False.0.0
15NY161415351-7269nono0332.96756.59...27.01160.61287.235.491.464True.1.0

16 rows × 22 columns

2、选用逻辑斯蒂回归(logistic regression)简单建模
在这个数据集中,因变量是一个二值变量,表示客户是否已经流失。因为因变量是一个二值变量,所以需要将预测值限制在0和1之间。逻辑斯蒂回归可以满足这个要求。逻辑斯蒂回归通过使用逻辑函数(或称逻辑斯蒂函数)的反函数估计概率的方式来测量自变量和二值型因变量之间的关系。这个函数可以将连续值转换为0和1之间的值,这是个必要条件,因为预测值表示概率,而概率必须在0和1之间。

对客户服务通话次数这部分数据进行了摘要分析,先按照一个新变量 total_charges 中的值使用等宽分箱法将数据分成 5 个组,然后为每个分组计算 5 个统计量:总数、最小值、均值、最大值和标准差。为了完成这些操作,第一行代码创建一个新变量total_charges,表示白天、傍晚、夜间和国际通话费用的总和。

churn['total_charges'] = churn['day_charge'] + churn['eve_charge'] + \
                         churn['night_charge'] + churn['intl_charge']
dependent_variable = churn['churn01']
independent_variables = churn[['account_length', 'custserv_calls', 'total_charges']]
independent_variables_with_constant = sm.add_constant(independent_variables, prepend=True)
logit_model = sm.Logit(dependent_variable, independent_variables_with_constant).fit()
print(logit_model.summary2())
# print("\nQuantities you can extract from the result:\n%s" % dir(logit_model))
print("\nCoefficients:\n%s" % logit_model.params)
print("\nCoefficient Std Errors:\n%s" % logit_model.bse)
Optimization terminated successfully.
         Current function value: 0.363480
         Iterations 7
                         Results: Logit
=================================================================
Model:              Logit            Pseudo R-squared: 0.122     
Dependent Variable: churn01          AIC:              2430.9594 
Date:               2020-04-07 16:12 BIC:              2455.4060 
No. Observations:   3333             Log-Likelihood:   -1211.5   
Df Model:           3                LL-Null:          -1379.1   
Df Residuals:       3329             LLR p-value:      2.2343e-72
Converged:          1.0000           Scale:            1.0000    
No. Iterations:     7.0000                                       
-----------------------------------------------------------------
                  Coef.  Std.Err.    z     P>|z|   [0.025  0.975]
-----------------------------------------------------------------
const            -7.2205   0.3944 -18.3093 0.0000 -7.9935 -6.4476
account_length    0.0012   0.0013   0.9274 0.3537 -0.0014  0.0038
custserv_calls    0.4443   0.0366  12.1290 0.0000  0.3725  0.5161
total_charges     0.0729   0.0054  13.4479 0.0000  0.0623  0.0835
=================================================================


Coefficients:
const            -7.220520
account_length    0.001222
custserv_calls    0.444323
total_charges     0.072914
dtype: float64

Coefficient Std Errors:
const             0.394363
account_length    0.001317
custserv_calls    0.036633
total_charges     0.005422
dtype: float64

3、最后使用这个拟合模型来预测,变量 y_predicted中包含着16个预测值。为了使输出更简单易懂,可以将预测值保
留两位小数。

new_observations = churn.loc[churn.index.isin(range(16)), independent_variables.columns]
new_observations_with_constant = sm.add_constant(new_observations, prepend=True)
y_predicted = logit_model.predict(new_observations_with_constant)
y_predicted_rounded = [round(score, 2) for score in y_predicted]
print(y_predicted_rounded)
[0.25, 0.09, 0.08, 0.2, 0.12, 0.1, 0.49, 0.03, 0.22, 0.24, 0.2, 0.05, 0.03, 0.19, 0.26, 0.81]
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值