用Python研究了三千套房子,揭秘是什么抬高了房价?

发表于:2018-1-24 10:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:佚名    来源:Python中文社区

#
Python
分享:
  关于房价,一直都是全民热议的话题,毕竟不少人终其一生都在为之奋斗。
  房地产的泡沫究竟有多大不得而知?今天我们抛开泡沫,回归房屋最本质的内容,来分析一下房价的影响因素究竟是什么?
  1、导入数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn
import missingno as msno
%matplotlib inline
train = pd.read_csv('train.csv',index_col=0)
#导入训练集
test = pd.read_csv('test.csv',index_col=0)
#导入测试
train.head(3)
print('train训练集缺失数据分布图')
msno.matrix(train)
print('test测试集缺失数据分布图')
msno.matrix(test)
  从上面的数据缺失可视化图中可以看出,部分特征的数据缺失十分严重,下面我们来对特征的缺失数量进行统计。
  2、目标Y值分析
  ##分割Y和X数据
  y=train['SalePrice']
  #看一下y的值分布
  prices = pd.DataFrame({'price':y,'log(price+1)':np.log1p(y)})
  prices.hist()
  观察目标变量y的分布和取对数后的分布看,取完对数后更倾向于符合正太分布,故我们对y进行对数转化。
  y = np.log1p(y) #+1的目的是防止对数转化后的值无意义
  3、合并数据 缺失处理
#合并训练特征和测试集
all_df = pd.concat((X,test),axis=0)
print('all_df缺失数据图')
msno.matrix(all_df)
#定义缺失统计函数
def show_missing(feature):
missing = feature.columns[feature.isnull().any()].tolist()
return  missing
print('缺失特征的数据缺失量统计:')
all_df[show_missing(all_df)].isnull().sum()
#先处理numeric数值型数据
#挨个儿看一下分布
fig,axs = plt.subplots(3,2,figsize=(16,9))
all_df['BsmtFinSF1'].hist(ax = axs[0,0])#众数填充
all_df['BsmtFinSF2'].hist(ax = axs[0,1])#众数
all_df['BsmtUnfSF'].hist(ax =  axs[1,0])#中位数
all_df['TotalBsmtSF'].hist(ax = axs[1,1])#均值填充
all_df['BsmtFullBath'].hist(ax = axs[2,0])#众数
all_df['BsmtHalfBath'].hist(ax = axs[2,1])#众数
#lotfrontage用均值填充
mean_lotfrontage = all_df.LotFrontage.mean()
all_df.LotFrontage.hist()
print('用均值填充:')
cat_input(all_df,'LotFrontage',mean_lotfrontage)
cat_input(all_df,'BsmtFinSF1',0.0)
cat_input(all_df,'BsmtFinSF2',0.0)
cat_input(all_df,'BsmtFullBath',0.0)
cat_input(all_df,'BsmtHalfBath',0.0)
cat_input(all_df,'BsmtUnfSF',467.00)
cat_input(all_df,'TotalBsmtSF',1051.78)
#在处理字符型,同样,挨个看下分布
fig,axs = plt.subplots(4,2,figsize=(16,9))
all_df['MSZoning'].hist(ax = axs[0,0])#众数填充
all_df['Utilities'].hist(ax = axs[0,1])#众数
all_df['Exterior1st'].hist(ax =  axs[1,0])#众数
all_df['Exterior2nd'].hist(ax = axs[1,1])#众数填充
all_df['KitchenQual'].hist(ax = axs[2,0])#众数
all_df['Functional'].hist(ax = axs[2,1])#众数
all_df['SaleType'].hist(ax = axs[3,0])#众数
cat_input(all_df,'MSZoning','RL')
cat_input(all_df,'Utilities','AllPub')
cat_input(all_df,'Exterior1st','VinylSd')
cat_input(all_df,'Exterior2nd','VinylSd')
cat_input(all_df,'KitchenQual','TA')
cat_input(all_df,'Functional','Typ')
cat_input(all_df,'SaleType','WD')
#再看一下缺失分布
msno.matrix(all_df)
  binggo,数据干净啦!下面开始处理特征,经过上述略微复杂的处理,数据集中所有的缺失数据都已处理完毕,可以开始接下来的工作啦!
  缺失处理总结:在本篇文章所使用的数据集中存在比较多的缺失,缺失数据包括数值型和字符型,处理原则主要有两个:
  一、根据绘制数据分布直方图,观察数据分布的状态,采取合适的方式填充缺失数据;
  二、非常重要的特征描述,认真阅读,按照特征描述填充可以解决大部分问题。
  4、特征处理
  让我们在重新仔细审视一下数据有没有问题?仔细观察发现MSSubClass特征实际上是分类特征,但是数据显示是int类型,这个需要改成str。
#观察特征属性发现,MSSubClass是分类特征,但是数据给的是数值型,需要对其做转换
all_df['MSSubClass']=all_df['MSSubClass'].astype(str)
#将分类变量转变成数值变量
all_df = pd.get_dummies(all_df)
print('分类变量转换完成后有{}行{}列'.format(*all_df.shape))
分类变量转换完成后有2919行316列
#标准化处理
numeric_cols = all_df.columns[all_df.dtypes !='uint8']
#x-mean(x)/std(x)
numeric_mean = all_df.loc[:,numeric_cols].mean()
numeric_std = all_df.loc[:,numeric_cols].std()
all_df.loc[:,numeric_cols] = (all_df.loc[:,numeric_cols]-numeric_mean)/numeric_std
  再把数据拆分到训练集和测试集
  train_df = all_df.ix[0:1460]#训练集
  test_df = all_df.ix[1461:]#测试集

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号