1.数据描述与探索
import matplotlib.pyplot as plt import numpy as np import pandas as pd #对数据进行基本的探索 #返回缺失值个数以及最大最小值 import pandas as pd datafile = './air_data.csv'#航空公司原始数据,第一行是属性名 result = 'explore.xlsx' data = pd.read_csv(datafile, encoding='utf-8') explore = data.describe( percentiles = [],include = 'all').T explore['null'] = len(data)-explore['count'] explore1 = explore[['null','max','min']] explore1.columns = [u'空值数',u'最大值',u'最小值']#重命名列名 explore1.to_excel(result)
(部分数据)
# from datetime import datetime datafile = './air_data.csv'#航空公司原始数据,第一行是属性名 ffp = data['FFP_DATE'].apply(lambda x:datetime.strptime(x,'%Y/%m/%d')) ffp_year = ffp.map(lambda x : x.year) import matplotlib.pyplot as plt fig = plt.figure(figsize=(10,6)) # 设置图框大小尺寸 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示中文标签 plt.hist(ffp_year,bins = 'auto',color = '#0488bb') plt.xlabel('年份') plt.ylabel('入会人数') plt.title('3148-Tang各年份会员入会人数',fontsize=20) plt.show()
from datetime import datetime datafile = './air_data.csv'#航空公司原始数据,第一行是属性名 male = pd.value_counts(data['GENDER'])['男'] female = pd.value_counts(data['GENDER'])['女'] #绘制饼图 fig = plt.figure(figsize=(10,6)) # 设置图框大小尺寸 plt.pie([male,female],labels = ['男','女'],colors = ['lightskyblue','lightcoral'],autopct = '%1.1f%%')# 绘制饼图 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.title('3148-Tang会员性别比例(饼图)',fontsize=15) plt.axis('equal') plt.show()
lv_four = pd.value_counts(data['FFP_TIER'])[4] lv_five = pd.value_counts(data['FFP_TIER'])[5] lv_six = pd.value_counts(data['FFP_TIER'])[6] fig = plt.figure(figsize=(8,5)) plt.bar(x=range(3),height=[lv_four,lv_five,lv_six],width=0.4,alpha=0.8,color='skyblue') plt.xticks([index for index in range(3)],['4','5','6']) plt.xlabel('会员等级') plt.ylabel('会员人数') plt.title('会员各级别人数3148-tang')
age = data['AGE'].dropna() age = age.astype('int64') fig = plt.figure(figsize = (5,10)) plt.boxplot(age, patch_artist = True, labels = ['会员年龄'], boxprops = {'facecolor':'lightblue'}) plt.title('会员年龄分布箱型图3148-tang') plt.grid(axis='y') plt.show() plt.close
lte = data['LAST_TO_END'] fc = data['FLIGHT_COUNT'] sks = data['SEG_KM_SUM'] #乘机时长 fig = plt.figure(figsize=(10,6)) plt.boxplot(lte, patch_artist=True, labels = ['时长'], boxprops = {'facecolor':'yellow'}) plt.title('会员最后乘机至结束时长分布箱型图3148-tang') plt.grid(axis='y') plt.show() plt.close fig = plt.figure(figsize=(10,6)) plt.boxplot(fc, patch_artist=True, labels = ['飞行次数'], boxprops={'facecolor':'lightblue'}) plt.title('会员飞行次数分布箱型图3148-tang') plt.grid(axis='y') plt.show() plt.close fig = plt.figure(figsize=(10,6)) plt.boxplot(sks, patch_artist=True, labels=['总飞行公里数'], boxprops = {'facecolor':'green'}) plt.title('客户总飞行公里数箱型图3148-tang') plt.grid(axis='y') plt.show() plt.close
ec = data['EXCHANGE_COUNT'] fig = plt.figure(figsize=(8,5)) plt.hist(ec,bins=5,color='#0504aa') plt.xlabel('兑换次数') plt.ylabel('会员人数') plt.title('会员兑换积分次数分布直方图3148-tang') plt.show() plt.close ps = data['Points_Sum'] fig = plt.figure(figsize=(5,8)) plt.boxplot(ps, patch_artist=True, labels=['总累计积分'], boxprops = {'facecolor':'green'}) plt.title('客户总累计积分箱型图3148-tang') plt.grid(axis='y') plt.show() plt.close
data_corr = data[['FFP_TIER','FLIGHT_COUNT','LAST_TO_END','SEG_KM_SUM','EXCHANGE_COUNT','Points_Sum']] age1 = data['AGE'].fillna(0) data_corr['AGE'] = age1.astype('int64') data_corr['ffp_year'] = ffp_year # 计算相关性矩阵 dt_corr = data_corr.corr(method='pearson') # 绘制热力图 import seaborn as sns plt.subplots(figsize=(10,10)) sns.heatmap(dt_corr,annot=True,vmax=1,square=True,cmap='Accent') plt.show() plt.title('相关性热力图-Tang') plt.close
2.清洗数据
import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('./air_data.csv',header=0) # 行数、列数 df.shape # 预览数据 df.head()
# 查看每个特征的总条数以及五值分布:平均值、最大值、最小值、标准差、四分位数。
df.describe().T
# 将需要保留的记录筛选出来,丢弃异常数据 # 筛选 的记录 df_notnull = df.loc[df['SUM_YR_1'].notnull() & df['SUM_YR_2'].notnull(),:] #选取总票价非空的记录 idx_tmp1 = df_notnull['SUM_YR_1'] !=0 # Y1 票价不为0的 index idx_tmp2 = df_notnull['SUM_YR_2'] !=0 # Y2 票价不为0的 index idx_tmp3 = (df_notnull['avg_discount'] !=0) & (df_notnull['SEG_KM_SUM']>0) # 平均折扣>0 且飞行里程>0的 index idx_tmp4 = df_notnull['AGE'] >= 100 # 年龄≥100的异常数据的index # 丢弃异常数据,并将数据存入csv文件 df_data_clean = df_notnull[(idx_tmp1|idx_tmp2) & idx_tmp3 & ~idx_tmp4] df_data_clean.to_csv('data_clean.csv') df_data_clean.shape df_data_clean.describe().T
# 重新检查清洗后的数据 df_data_clean = pd.read_csv('./data_clean.csv', header=0) # df_data_clean.isnull().sum().sort_values(ascending=False) # 用年龄平均值填充年龄缺失值 df_data_clean['AGE'].fillna(df_data_clean['AGE'].mean(), inplace=True) df_data_clean.isnull().sum().sort_values(ascending=False)
3.属性构造
data_clean = pd.read_csv('./data_clean.csv', header=0) # 选择 LRFMC模型的6个属性 data_select = data_clean[['FFP_DATE','LOAD_TIME','LAST_TO_END', 'FLIGHT_COUNT','SEG_KM_SUM','avg_discount']] # 构造指标L L = pd.to_datetime(data_select['LOAD_TIME']) - pd.to_datetime(data_select['FFP_DATE']) # 日期差,但有 " days" 单位尾缀,需要删除单位 # L 值中存在" days"单位子串 L = L.astype('str').str.split().str[0] L=pd.to_numeric(L) # object -> int64 # 构造 LRFMC 模型指标 mdl_LRFMC = pd.concat([L, data_select.iloc[:, 2:]], axis=1) mdl_LRFMC.columns = ['L','R','F','M','C'] mdl_LRFMC # mdl_LRFMC.to_csv('LRFMC.csv')
mdl_LRFMC.describe().loc[['min','max','mean','std','50%']].T
# 数据标准化 def zscore_data(data): data2=(data-data.mean(axis=0))/data.std(axis=0) data2.columns=["Z"+i for i in data.columns] return data2 z_LRFMC = zscore_data(mdl_LRFMC) z_LRFMC.head()
%matplotlib inline import matplotlib.pyplot as plt # 客户分群雷达图 labels = ['ZL','ZR','ZF','ZM','ZC'] legen = ['客户群' + str(i + 1) for i in cluster_center.index] # 客户群命名,作为雷达图的图例 lstype = ['-','--',(0, (3, 5, 1, 5, 1, 5)),':','-.'] kinds = list(cluster_center.iloc[:, 0]) # 由于雷达图要保证数据闭合,因此再添加L列,并转换为 np.ndarray cluster_center = pd.concat([cluster_center, cluster_center[['ZL']]], axis=1) centers = np.array(cluster_center.iloc[:, 0:]) # 分割圆周长,并让其闭合 n = len(labels) angle = np.linspace(0, 2 * np.pi, n, endpoint=False) angle = np.concatenate((angle, [angle[0]])) # 绘图 fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111, polar=True) # 以极坐标的形式绘制图形 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 # 画线 for i in range(len(kinds)): ax.plot(angle, centers[i], linestyle=lstype[i], linewidth=2, label=kinds[i]) # 添加属性标签 ax.set_thetagrids(angle * 180 / np.pi, labels) plt.title('客户特征分析雷达图3148-tang') plt.legend(legen) plt.show() plt.close
4.预测
import numpy as np import pandas as pd #from pyecharts.charts import * #from pyecharts import options as opts import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 #读取文件 df = pd.read_csv('./WA_Fn-UseC_-Telco-Customer-Churn.csv') for i in df.columns: if i == 'SeniorCitizen': print(i,df[i].isnull().sum()) elif i == 'tenure': print(i,df[i].isnull().sum()) elif i == 'MonthlyCharges': print(i,df[i].isnull().sum()) else: print(i,df[df[i] == ' '][i].count()) # 查看分类变量的所有取值 for i in df.columns: if i not in ('customerID','MonthlyCharges','TotalCharges'): print(i,df[i].unique()) # 离散型变量分布 pic = plt.figure(figsize=(9,3),dpi=80) pic.add_subplot(1,2,1)#第一个子图(行数,列数,本子图位置) plt.boxplot(df["MonthlyCharges"]) plt.title('MonthlyCharges3148-tang') pic.add_subplot(1,2,2)#第一个子图(行数,列数,本子图位置) plt.boxplot(df["TotalCharges"]) plt.title('TotalCharges3148-tang') # 子图间距 plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=0.5) plt.show()
pic = plt.figure(figsize=(12,6),dpi=80) pic.subplots_adjust(wspace=0.3,hspace=0.3) for i,j in zip(df.iloc[:,1:5].columns,range(1,5)): df_count = df.pivot_table(index='Churn',columns=i,values='customerID',aggfunc='count') df_ratio = df_count.div(df_count.sum()) #print(df_ratio) pic.add_subplot(2,2,j) plt.title('Churn by '+ i+ '3148-tang') plt.bar([0,1],df_ratio.iloc[0,:],label = df_ratio.index.values[0],width=0.5) plt.bar([0,1],df_ratio.iloc[1,:],bottom = df_ratio.iloc[0,:],label=df_ratio.index.values[1],width=0.5) plt.xticks([0,1],df_count.columns.values) for a,b in zip(range(df_ratio.shape[1]),df_ratio.iloc[1,:]): plt.text(a,0.88,round(b,4),va='bottom',ha='center',fontsize=12) plt.legend() plt.show()
---服务属性
pic = plt.figure(figsize=(16,9),dpi=80) pic.subplots_adjust(wspace=0.3,hspace=0.3) for i,j in zip(df.iloc[:,6:15].columns,range(1,10)): df_count = df.pivot_table(index='Churn',columns=i,values='customerID',aggfunc='count') df_ratio = df_count.div(df_count.sum()) pic.add_subplot(3,3,j) plt.title('Churn by '+ i+ '3148-tang') plt.bar(range(df_ratio.shape[1]),df_ratio.iloc[0,:],label = df_ratio.index.values[0],width=0.5) plt.bar(range(df_ratio.shape[1]),df_ratio.iloc[1,:],bottom = df_ratio.iloc[0,:],label=df_ratio.index.values[1],width=0.5) plt.xticks(range(df_ratio.shape[1]),df_count.columns.values) for a,b in zip(range(df_ratio.shape[1]),df_ratio.iloc[1,:]): plt.text(a,0.88,round(b,4),va='bottom',ha='center',fontsize=12) if i=='PhoneService': plt.legend() plt.show()
----消费属性
pic = plt.figure(figsize=(20,9),dpi=120) pic.subplots_adjust(wspace=0.3,hspace=0.3) for i,j in zip(df.iloc[:,15:18].columns,range(1,4)): df_count = df.pivot_table(index='Churn',columns=i,values='customerID',aggfunc='count') df_ratio = df_count.div(df_count.sum()) pic.add_subplot(2,2,j) plt.title('Churn by '+ i + '3148-tang') plt.bar(range(df_ratio.shape[1]),df_ratio.iloc[0,:],label = df_ratio.index.values[0],width=0.5) plt.bar(range(df_ratio.shape[1]),df_ratio.iloc[1,:],bottom = df_ratio.iloc[0,:],label=df_ratio.index.values[1],width=0.5) plt.xticks(range(df_ratio.shape[1]),df_count.columns.values) for a,b in zip(range(df_ratio.shape[1]),df_ratio.iloc[1,:]): plt.text(a,0.95,round(b,4),va='bottom',ha='center',fontsize=12) if i=='PaperlessBilling': plt.legend() plt.show()
import seaborn as sns sns.distplot(df.query("Churn == 'Yes'")["MonthlyCharges"]) plt.title("MonthlyCharges3148-tang") plt.show() sns.distplot(df.query("Churn == 'Yes'")["TotalCharges"]) plt.title("TotalCharges3148tang") plt.show()
标签:plt,ratio,pd,航空,title,df,---,第七章,data From: https://www.cnblogs.com/2020310148tjy/p/17209443.html