行车出险客户画像(是否出险和年龄、驾龄、性别、婚姻状态等变量之间的关系)

import os
import pandas as pd

os.chdir(r'H:\2019-2-3新华书店笔记以及资料\资料\HW4')

au=pd.read_csv(r'auto_ins.csv',encoding='gbk')
au.head()  #简单查看数据

 EngSizeAgeGenderMaritalexpOwnervAgeGarageAntiTFDimportLoss
02.056已婚20公司10有防盗装置进口0.0
11.841已婚20公司9无防盗装置国产0.0
22.044未婚20公司8有防盗装置国产0.0
31.656已婚20公司7有防盗装置国产0.0
41.845已婚20公司7无防盗装置国产0.0

def codeMy(x): #编写函数,对数据处理

    if x>0:return 1else:return 0
au['loss_flag']=au['Loss'].apply(codeMy)
au["loss_flag1"]= au.Loss.map(lambda x: 'shi' if x >0 else 'f')
au.head() #查看数据处理后的数据
 EngSizeAgeGenderMaritalexpOwnervAgeGarageAntiTFDimportLossloss_flagloss_flag1
02.056已婚20公司10有防盗装置进口0.00f
11.841已婚20公司9无防盗装置国产0.00f
22.044未婚20公司8有防盗装置国产0.00f
31.656已婚20公司7有防盗装置国产0.00f
41.845已婚20公司7无防盗装置国产0.00f

 

#2、对loss_flag分布情况进行描述分析
au.loss_flag1.value_counts()#查看这一列的数据分布
f      3028
shi    1205
Name: loss_flag1, dtype: int64au.loss_flag1.value_counts()/au.Loss.count()
f      0.715332
shi    0.284668
Name: loss_flag1, dtype: float64
au.loss_flag1.value_counts().plot(kind='bar') #这列数据用图形展示

 

# 3、分析是否出险和年龄、驾龄、性别、婚姻状态等变量之间的关系
import matplotlib.pyplot as plt
import seaborn as sns
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
#是否出险和年龄
sns.boxplot(x = 'loss_flag1',y = 'Age',data = au, ax = ax1)
#是否出险和驾龄
sns.boxplot(x = 'loss_flag1',y = 'exp',data = au, ax = ax2)  #查看是否出险和年龄,驾龄的关系何须图
  

pd.crosstab(au['Gender'],au.loss_flag1).plot(kind='bar') #性别和出险的关系对比

 

 
au['Gender'].value_counts().plot(kind='bar')#男女出险的对比

#是否出险和性别

from stack2dim import *

stack2dim(au,'Gender','loss_flag1') #调用以前写的库,画出性别和出险的关系图(柱状图的粗细代表数量,纵坐标代表百分比)

 

#是否出险和婚姻状态
stack2dim(au,'Marital','loss_flag1')


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部