行車出險客戶畫像(是否出險和年齡、駕齡、性別、婚姻狀態等變量之間的關係)

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()  #簡單查看數據

  EngSize Age Gender Marital exp Owner vAge Garage AntiTFD import Loss
0 2.0 56 已婚 20 公司 10 有防盜裝置 進口 0.0
1 1.8 41 已婚 20 公司 9 無防盜裝置 國產 0.0
2 2.0 44 未婚 20 公司 8 有防盜裝置 國產 0.0
3 1.6 56 已婚 20 公司 7 有防盜裝置 國產 0.0
4 1.8 45 已婚 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() #查看數據處理後的數據
  EngSize Age Gender Marital exp Owner vAge Garage AntiTFD import Loss loss_flag loss_flag1
0 2.0 56 已婚 20 公司 10 有防盜裝置 進口 0.0 0 f
1 1.8 41 已婚 20 公司 9 無防盜裝置 國產 0.0 0 f
2 2.0 44 未婚 20 公司 8 有防盜裝置 國產 0.0 0 f
3 1.6 56 已婚 20 公司 7 有防盜裝置 國產 0.0 0 f
4 1.8 45 已婚 20 公司 7 無防盜裝置 國產 0.0 0 f

 

#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')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章