第三章-1

浏览: 1205

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

推荐 0
本文由 times 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册