收入预测分析_practice

"""
Created on Tue Jun  4 17:40:28 2019

@author: wuxian
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
##数据读取
income = pd.read_excel(r'C:\Users\wuxian\Desktop\personal_python\从零开始学Python--数据分析与挖掘\第2章 从收入预测分析开始\income.xlsx')
income.head()
income.columns
income.sex.value_counts()
income.age.value_counts()
income.info()
income.drop(['education','fnlwgt'],axis=1,inplace=True)
income.mean(axis=1)#计算每一列的均值
#查看数据集是否存在缺失值
income.apply(lambda x:np.sum(x.isnull()))

#如何判断字段是离散型
object_v=[]
for feature in income.columns:
    if income[feature].dtype=='object':
        object_v.append(income[feature].name)
object_v    
         
#mode()众数使用
income.fillna(value={'workclass':income.workclass.mode()[0],
                     'occupation':income.occupation.mode()[0],
                     'native-country':income['native-country'].mode()[0]},inplace = True)


#数据的探索性分析
#数值型描述性分析    
income.describe()
#离散型描述性分析
income.describe(include=['object'])
income.describe(include=['object']).education



# count(age) group by race,income
# =============================================================================
#构造不同收入水平下各种族人数的数据
race = pd.DataFrame(income.groupby(by = ['race','income']).aggregate(np.size).loc[:,'age'])
#重设行索引
race = race.reset_index()
#变量重命名
race.rename(columns={'age':'counts'},inplace=True)
#排序
race.sort_values(by = ['race','counts'],ascending=False,inplace=True) #不需要再赋值
# =============================================================================


#柱状图
#设置图框比例,并绘图
plt.figure(figsize=(9,5))
sns.barplot(x='race',y='counts',hue='income',data=race)
plt.show()


#离散变量的重编码
#getdummies()

object_w=[]
for feature in income.columns:
    if income[feature].dtype=='object':
        income[feature] = pd.Categorical(income[feature]).codes
        object_w.append(income[feature])
  
object_w    
    




#导入绘图模块
import matplotlib.pyplot as plt
#设置绘图风格
plt.style.use('ggplot')
#设置多图形的组合
fig,axes = plt.subplots(2,1)
#绘制不同收入水平下的年龄和密度图
#在生成的线型图的代码中加上kind='bar'(垂直树状图)或 kind='barch'(水平柱状图)即可生成柱状图
#密度图,它是通过计算可能会产生观测数据的连续概率分布的估计而产生的。一般过程是将该分布近似为一组核分布,
#因此,密度图也被称作KDE图,调用plt时加上kind='kde'即可生成一张密度图(折线图)。
income.age[income.income == ' <=50K'].plot(kind = 'kde', label = '<=50k', ax = axes[0], legend = True, linestyle = '-')
income.age[income.income == ' >50K'].plot(kind = 'kde', label = '>50k', ax = axes[0], legend = True, linestyle = '--')

# 绘制不同收入水平下的周工作小时数和密度图
income['hours-per-week'][income.income == ' <=50K'].plot(kind = 'kde', label = '<=50K', ax = axes[1], legend = True, linestyle = '-')
income['hours-per-week'][income.income == ' >50K'].plot(kind = 'kde', label = '>50K', ax = axes[1], legend = True, linestyle = '--')
# 显示图形
plt.show()



# 散点图矩阵
# 导入模块
import matplotlib.pyplot as plt
import seaborn
# 绘制散点图矩阵
seaborn.pairplot(Profit_New.ix[:,['RD_Spend','Administration','Marketing_Spend','Profit']])
# 显示图形
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值