"""
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()
收入预测分析_practice
最新推荐文章于 2022-06-02 10:35:17 发布