# -*- coding: utf-8 -*-
"""
Created on Sat Jul 7 12:44:06 2018
@author: Times
"""
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# In[ ]:
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
os.chdir(r"D:\我的文件\文档\280_Ben_八大直播八大案例配套课件\第三章\HW4")
auto_ins=pd.read_csv(r"auto_ins.csv",encoding="gbk")
# In[]:
def is_lipei(amount):
if amount >0 :
x=1
elif amount ==0:
x=0
return x
# In[]:
auto_ins["loss_flag"]=auto_ins.Loss.map(is_lipei)
#auto_ins.loss_flag.agg(['mean','median','sum','std','skew'])
#auto_ins["loss_flag"].describe(include='all')
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
auto_ins.loss_flag.value_counts().plot(kind='bar')
auto_ins.loss_flag.hist(bins=50)
# In[]:
#年龄
auto_ins.Age.groupby(auto_ins.loss_flag).mean().plot(kind='bar')
sns.boxplot(x='loss_flag',y='Age',data=auto_ins)
#驾龄
auto_ins.vAge.groupby(auto_ins.loss_flag).mean().plot(kind='bar')
sns.boxplot(x='loss_flag',y='vAge',data=auto_ins)
#性别
Gender_temp=pd.crosstab(auto_ins.Gender,auto_ins.loss_flag)
Gender_temp["total"]=Gender_temp[[0,1]].apply(lambda x:x[0]+x[1],axis=1)
Gender_temp[0]=Gender_temp[[0,"total"]].apply(lambda x:x[0]/x[1],axis=1)
Gender_temp[1]=Gender_temp[[0]].apply(lambda x:1-x,axis=1)
del Gender_temp["total"]
Gender_temp.plot(kind='bar')
#婚姻
Gender_temp=pd.crosstab(auto_ins.Marital,auto_ins.loss_flag)
Gender_temp["total"]=Gender_temp[[0,1]].apply(lambda x:x[0]+x[1],axis=1)
Gender_temp[0]=Gender_temp[[0,"total"]].apply(lambda x:x[0]/x[1],axis=1)
Gender_temp[1]=Gender_temp[[0]].apply(lambda x:1-x,axis=1)
del Gender_temp["total"]
Gender_temp.plot(kind='bar')
Gender_temp.dtypes
#owner
Gender_temp=pd.crosstab(auto_ins.Owner,auto_ins.loss_flag)
Gender_temp["total"]=Gender_temp[[0,1]].apply(lambda x:x[0]+x[1],axis=1)
Gender_temp[0]=Gender_temp[[0,"total"]].apply(lambda x:x[0]/x[1],axis=1)
Gender_temp[1]=Gender_temp[[0]].apply(lambda x:1-x,axis=1)
del Gender_temp["total"]
Gender_temp.plot(kind='bar')