zillow房价预测比赛_数据分析实战——美国King County房价预测

数据介绍

该项目是Data Castle上的美国King County房价预测训练赛,用到的数据取自于kaggle datasets,由@harlfoxem提供并分享,但是只选取了其中的子集,并对数据做了一些预处理使数据更加符合回归分析比赛的要求。

数据主要包括2014年5月至2015年5月美国King County的房屋销售价格以及房屋的基本信息。 数据分为训练数据和测试数据,分别保存在kc_train.csv和kc_test.csv两个文件中。 其中训练数据主要包括10000条记录,14个字段,主要字段说明如下:

  • 第一列“销售日期”("SaleDate"):2014年5月到2015年5月房屋出售时的日期
  • 第二列“销售价格”("SalePrice"):房屋交易价格,单位为美元,是目标预测值
  • 第三列“卧室数”("BedroomNum"):房屋中的卧室数目
  • 第四列“浴室数”("BathroomNum"):房屋中的浴室数目
  • 第五列“房屋面积”("LivingArea"):房屋里的生活面积
  • 第六列“停车面积”("ParkingArea"):停车坪的面积
  • 第七列“楼层数”("Floor"):房屋的楼层数
  • 第八列“房屋评分”("Rating"):King County房屋评分系统对房屋的总体评分
  • 第九列“建筑面积”("BuildingArea"):除了地下室之外的房屋建筑面积
  • 第十列“地下室面积”("BasementArea"):地下室的面积
  • 第十一列“建筑年份”("BuildingYear"):房屋建成的年份
  • 第十二列“修复年份”("RepairYear"):房屋上次修复的年份
  • 第十三列"纬度"("Latitude"):房屋所在纬度
  • 第十四列“经度”("Longitude"):房屋所在经度

测试数据主要包括3000条记录,13个字段,跟训练数据的不同是测试数据并不包括房屋销售价格,需要通过由训练数据所建立的模型以及所给的测试数据,得出测试数据相应的房屋销售价格预测值。

# 导入数据分析需要的包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.style.use('ggplot')

# 分别导入训练数据和测试数据,并按照数据介绍中的内容给每一列加上列名
trainDf = pd.read_csv('kc_train.csv', header = None, 
                  names = ['SaleDate', 'SalePrice', 'BedroomNum', 'BathroomNum', 'LivingArea',
                           'ParkingArea', 'Floor', 'Rating', 'BuildingArea', 'BasementArea',
                           'BuildingYear', 'RepairYear', 'Latitude', 'Longitude'])
testDf = pd.read_csv('kc_test.csv', header = None, 
                  names = ['SaleDate', 'BedroomNum', 'BathroomNum', 'LivingArea',
                           'ParkingArea', 'Floor', 'Rating', 'BuildingArea', 'BasementArea',
                           'BuildingYear', 'RepairYear', 'Latitude', 'Longitude'])

# 将训练集和测试集合并,便于对数据进行统一处理
fullDf = trainDf.append(testDf, ignore_index = True )

# 查看数据信息
trainDf.info()
# 以下为输出结果
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000 entries, 0 to 9999
Data columns (total 14 columns):
SaleDate        10000 non-null int64
SalePrice       10000 non-null int64
BedroomNum      10000 non-null int64
BathroomNum     10000 non-null float64
LivingArea      10000 non-null int64
ParkingArea     10000 non-null int64
Floor           10000 non-null float64
Rating          10000 non-null int64
BuildingArea    10000 non-null int64
BasementArea    10000 non-null int64
BuildingYear    10000 non-null int64
RepairYear      10000 non-null int64
Latitude        10000 non-null float64
Longitude       10000 non-null float64
dtypes: float64(4), int64(10)
memory usage: 1.1 MB

训练集一共有10000条数据,且数据集中没有缺失值。可以看到数据类型全是数值型。

下面对某些数据进行处理:将销售日期转换为日期格式,并提取出年份和月份;将建筑年份分区,转化为分类变量。

# 将销售日期转换为日期格式,并提取出年份和月份
fullDf['SaleDate_year'] = pd.to_datetime(fullDf['SaleDate'], format='%Y%m%d').dt.year
fullDf['SaleDate_month'] = pd.to_datetime(fullDf['SaleDate'], format='%Y%m%d').dt.month

# 建筑年份从1900年到2015年,每隔15年分一组
temp_list = [i for i in range(1900, 2021)]
bins = temp_list[::15]
fullDf['BuildingYearBins'] = pd.cut(fullDf['BuildingYear'], bins=bins)

# 使用pandas的get_dummies方法进行one-hot编码,得到虚拟变量,添加前缀‘Year’
BuildingYearBinsDf = pd.get_dummies(fullDf['BuildingYearBins'], prefix='Year')

# 添加虚拟变量到数据集中
fullDf = pd.concat([fullDf, BuildingYearBinsDf], axis=1)

# 将建筑年份和销售日期两列删除
fullDf.drop(['BuildingYear', 'SaleDate'], axis=1, inplace=True)

# 查看房价的分布情况
%matplotlib notebook
trainDf['SalePrice'].hist(bins=30)

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值