一、项目背景
- 流失用户指的使用过产品因为某些原因不再使用该产品。随着产品的更新迭代,都会存在一定的流失情况,这时正常现象。流失用户的比例和变化趋势能够反映该产品当前是否存在问题以及未来的发展趋势。
- 当用户群体庞大时,有限的人力和精力不能为每个用户都投入大量的时间。如果公司可以预测哪些用户可能提前流失,这样就能将主要精力聚焦于这些用户,实施有效的用户挽留策略,提高用户粘性。
- 本项目旨在通过分析特征属性确定用户流失的原因,以及哪些因素可能导致用户流失。建立预测模型来判断用户是否流失,并提出用户流失预警策略。
二、数据集介绍
数据集中总计7043条数据,21个特征字段,最终分类特征为Churn:用户是否流失
字段含义
- customerID:用户ID
- gender:性别
- SeniorCitizen:是否是老人
- Partner:是否有伴侣
- Dependents:是否有需要抚养的孩子
- tenure:任职
- PhoneService:是否办理电话服务
- MultipleLines:是否开通了多条线路
- InternetService:是否开通网络服务和开通的服务类型(光纤、电话拨号)
- TechSupport:是否办理技术支持服务
- OnlineBackup:是否办理在线备份服务
- OnlineSecurity:是否办理在线安全服务
- DeviceProtection:是否办理设备保护服务
- StreamingTV:是否办理电视服务
- StreamingMovies:是否办理电影服务
- Contract:签订合约的时长
- PaperlessBilling:是否申请了无纸化账单
- PaymentMethod:付款方式(电子支票、邮寄支票、银行自动转账、信用卡自动转账)
- MonthlyCharges:月消费
- TotalCharges:总消费
- Churn:用户是否流失
三、数据预处理
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#导入数据
filepath = './dataFile/Telco-Customer-Churn.csv'
df = pd.read_csv(filepath)
#查看前5行数据信息
df.head(5)
customerID | gender | SeniorCitizen | Partner | Dependents | tenure | PhoneService | MultipleLines | InternetService | OnlineSecurity | ... | DeviceProtection | TechSupport | StreamingTV | StreamingMovies | Contract | PaperlessBilling | PaymentMethod | MonthlyCharges | TotalCharges | Churn | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 7590-VHVEG | Female | 0 | Yes | No | 1 | No | No phone service | DSL | No | ... | No | No | No | No | Month-to-month | Yes | Electronic check | 29.85 | 29.85 | No |
1 | 5575-GNVDE | Male | 0 | No | No | 34 | Yes | No | DSL | Yes | ... | Yes | No | No | No | One year | No | Mailed check | 56.95 | 1889.5 | No |
2 | 3668-QPYBK | Male | 0 | No | No | 2 | Yes | No | DSL | Yes | ... | No | No | No | No | Month-to-month | Yes | Mailed check | 53.85 | 108.15 | Yes |
3 | 7795-CFOCW | Male | 0 | No | No | 45 | No | No phone service | DSL | Yes | ... | Yes | Yes | No | No | One year | No | Bank transfer (automatic) | 42.30 | 1840.75 | No |
4 | 9237-HQITU | Female | 0 | No | No | 2 | Yes | No | Fiber optic | No | ... | No | No | No | No | Month-to-month | Yes | Electronic check | 70.70 | 151.65 | Yes |
5 rows × 21 columns
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7043 entries, 0 to 7042
Data columns (total 21 columns):
customerID 7043 non-null object
gender 7043 non-null object
SeniorCitizen 7043 non-null int64
Partner 7043 non-null object
Dependents 7043 non-null object
tenure 7043 non-null int64
PhoneService 7043 non-null object
MultipleLines 7043 non-null object
InternetService 7043 non-null object
OnlineSecurity 7043 non-null object
OnlineBackup 7043 non-null object
DeviceProtection 7043 non-null object
TechSupport 7043 non-null object
StreamingTV 7043 non-null object
StreamingMovies 7043 non-null object
Contract 7043 non-null object
PaperlessBilling 7043 non-null object
PaymentMethod 7043 non-null object
MonthlyCharges 7043 non-null float64
TotalCharges 7043 non-null object
Churn 7043 non-null object
dtypes: float64(1), int64(2), object(18)
memory usage: 1.1+ MB
df.describe()
SeniorCitizen | tenure | MonthlyCharges | |
---|---|---|---|
count | 7043.000000 | 7043.000000 | 7043.000000 |
mean | 0.162147 | 32.371149 | 64.761692 |
std | 0.368612 | 24.559481 | 30.090047 |
min | 0.000000 | 0.000000 | 18.250000 |
25% | 0.000000 | 9.000000 | 35.500000 |
50% | 0.000000 | 29.000000 | 70.350000 |
75% | 0.000000 | 55.000000 | 89.850000 |
max | 1.000000 | 72.000000 | 118.750000 |
- 注意到没有缺失数据,但是字段TotalCharge总消费金额应该是float类型,但数据集中显示为object,猜测应该是存在空字符串,进行查看
df['TotalCharges']= df['TotalCharges'].replace(" ",0).astype('float32')
df['TotalCharges'].describe()
count 7043.000000
mean 2279.732178
std 2266.794434
min 0.000000
25% 398.549988
50% 1394.550049
75% 3786.599976
max 8684.799805
Name: TotalCharges, dtype: float64
四、探索性分析
分析特征发现,影响用户流失的因素主要可以分成三个方面:
- 产品本身
- 用户属性
- 用户的消费行为和使用行为
1. 产品角度
从产品角度出发,电信公司提供的服务中主要包含两类:电话服务和网络服务(也可以看作是需要网络和不需要网络服务的,电影服务,电视服务,技术设备服务都是在订购网络服务的附加服务)
#查看这两种主营业务的订购情况
print("两种业务都开通的人数有")
print(df.loc[(df['PhoneService']=='Yes')&(df['InternetService']!='No')].shape[0])
print("只开通电话的人数有")
print(df.loc[(df['PhoneService']=='Yes')&(df['InternetService']=='No')].shape[0])
print("只开通网络的人数有")
print(df.loc[(df['InternetService']!='No')&(df['PhoneService']=='No')].shape[0])
两种业务都开通的人数有
4835
只开通电话的人数有
1526
只开通网络的人数有
682
- 由此可见,用户流失可能是由电话服务和网络服务这两种服务造成的
#设置正常显示中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(8,4),dpi=100)
sns.countplot(data=df,x='PhoneService',hue='Churn')
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.ylabel('Counts',fontsize=12)
plt.xlabel('Phone Service',fontsize=12)
plt.title("电话服务办理情况对用户流失的影响")
plt.show()
- 可以发现,办理与未办理电话服务的用户在流失情况上并未出现较大差异,因此,可能并不是电话服务业务导致的用户流失。
plt.figure(figsize=(8,4),dpi=100)
sns.countplot(data=df,x='InternetService',hue='Churn')
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.ylabel('Counts',fontsize=12)
plt.xlabel('Internet Service',fontsize=12)
plt.title("网络服务办理情况对用户流失的影响")
plt.show()
- 可以发现,办理电话拨号网络服务的用户流失情况要明显低于办理光纤网络服务的用户,因此可以猜测光纤服务可能存在问题,是造成用户流失的一个比较大的因素,需要采取措施调整
- 同时发现,未办理网络服务的用户流失情况要明显低于未办理电话服务的用户流失情况,说明用户在这两个主营业务中认为电话服务更重要
确定网络服务中存在问题,对网络服务的细分服务进行分析,查看它们对用户流失的影响
#定义函数,计算电信公司提供的各种服务的使用情况:
def produce_using(data,cols):
'''
功能:计算某种服务用户使用情况,即统计改字段为“Yes”的人数
data:数据集
cols:需要统计的字段列表
'''
for i in cols:
num = data[data[i]=='Yes'][i].count()
print("办理{}的人数有{}".format(i,num))
print("办理{}的总人数有{}".format('InternetService',df[df['InternetService']!='No']['InternetService'].count()))
print("网络服务的附加服务办理情况:")
produce_using(df,['OnlineSecurity','OnlineBackup','DeviceProtection','TechSupport','StreamingTV','StreamingMovies'])
#办理网络服务的有两种类型可选择,光纤和电话拨号
# print("办理{}的总人数有{}".format('InternetService',df[df['InternetService']!='No']['InternetService']