#7.1数据探索
import matplotlib.pyplot as plt
import pandas as pd
datafile=r"C:\Users\admin\Desktop\39984b862fb97f6326368ad28c7b6a4e_a8b8b4ac463b459805927f22a40e654f_8.csv"
resultfile=r"C:\Users\admin\Desktop\新建 XLS 工作表.csv"
data=pd.read_csv(datafile,encoding="utf8")
explore=data.describe(percentiles=[],include='all').T
explore['null']=len(data)-explore['count']
explore=explore[['null','max','min']]
explore.columns=[u'空数值',u'最大值',u'最小值']
#7.2探索客户的基本信息分布情况
from datetime import datetime
ffp=data['FFP_DATE'].apply(lambda x:datetime.strptime(x, '%Y/%m/%d'))
ffp_year=ffp.map(lambda x :x.year)
fig=plt.figure(figsize=(8,5))
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus']=False
plt.hist(ffp_year,bins='auto',color='#0504aa')
plt.xlabel('年份')
plt.ylabel('各年份会员入会人数')
plt.show()
plt.close
male=pd.value_counts(data['GENDER'])['男']
female=pd.value_counts(data['GENDER'])['女']
fig=plt.figure(figsize=(7,4))
plt.pie([male,female],labels=['男','女'],colors=['lightskyblue','lightcoral'],autopct='%1.1f%%')
plt.title('会员性别比例')
plt.show()
plt.close
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('会员各级别人数')
plt.show()
plt.close
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('会员年龄分布箱形图')
plt.grid(axis='y')
plt.show()
plt.close
#7.3探索客户乘机信息分布情况
lte=data['LAST_TO_END']
fc=data['FLIGHT_COUNT']
sks=data['SEG_KM_SUM']
fig=plt.figure(figsize=(5,8))
plt.boxplot(lte,patch_artist=True,labels=['时长'],boxprops={'facecolor':'lightblue'})
plt.title('会员最后乘机至结束时分布箱形图')
plt.grid(axis='y')
plt.show()
plt.close
fig=plt.Figure(figsize=(5,10))
plt.boxplot(fc,patch_artist=True,labels=['飞行时间'],boxprops={'facecolor':'lightblue'})
plt.title('会员飞行次数分布箱形图')
plt.grid(axis='y')
plt.show()
plt.close
fig=plt.Figure(figsize=(5,12))
plt.boxplot(sks,patch_artist=True,labels=['总飞行公里数'],boxprops={'facecolor':'lightblue'})
plt.title('客户总飞行公里数箱形图')
plt.grid(axis='y')
plt.show()
plt.close
#7.4探索客户的积分信息分布情况
ec=data['EXCHANGE_COUNT']
fig=plt.Figure(figsize=(8,5))
plt.hist(ec, bins=5,color=('#0504aa'))
plt.xlabel('兑换次数')
plt.ylabel('会员人数')
plt.title('会员兑换积分次数分布直方图')
plt.show()
plt.close
ps=data['Points_Sum']
fig=plt.Figure(figsize=(5,8))
plt.boxplot(ps,patch_artist=True,labels=['总累计积分'],boxprops={'facecolor':'lightblue'})
plt.title('客户总累计积分箱形图')
plt.grid(axis='y')
plt.show()
plt.close
#7.5相关系数矩阵与热力图
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')
print('相关性矩阵为:\n',dt_corr)
import seaborn as sns
plt.subplots(figsize=(10,10))
sns.heatmap(dt_corr, annot=True,vmax=(1),square=True,cmap='Blues')
plt.show()
plt.close
标签:plt,show,航空公司,客户,fig,close,价值,data,figsize From: https://www.cnblogs.com/zty666ya/p/17191995.html