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

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 1
    else:
        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: int64

au.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') #这列数据用图形展示
<matplotlib.axes._subplots.AxesSubplot at 0x996e160>

 

# 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)  #查看是否出险和年龄,驾龄的关系何须图
<matplotlib.axes._subplots.AxesSubplot at 0xb947320>  

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

<matplotlib.axes._subplots.AxesSubplot at 0xbb3ad30>

 

 
au['Gender'].value_counts().plot(kind='bar')#男女出险的对比
<matplotlib.axes._subplots.AxesSubplot at 0xbdbd400>

#是否出险和性别

from stack2dim import *

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

 

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值