本文数据集来自Kaggle波士顿房价预测项目https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data
1、数据导入

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
import warnings
warnings.filterwarnings('ignore')
%matplotlib inlinetrain = pd.read_csv('input/train.csv')
test = pd.read_csv('input/test.csv')

2、数据相关性分析

sample = pd.read_csv('input/sample_submission.csv')corrmat = train.corr()
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True)

3、挑选相关变量

k = 10
cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index
cm = np.corrcoef(train[cols].values.T)
sns.set(font_scale=1.25)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values, xticklabels=cols.values)
plt.show()

sns.set()
cols = ['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'FullBath', 'YearBuilt']
sns.pairplot(train[cols], size = 2.5)
plt.show()

train_Id = train['Id']
test_Id = test['Id']
len_train = train.shape[0]
len_test = test.shape[0]
housing = pd.concat([train, test], sort=False)
housing.shape
(2919, 81)
housing[cols].columns
Index(['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF','FullBath', 'YearBuilt'],dtype='object')
housing = housing[cols]

4、处理缺失值

housing.isnull().sum()
SalePrice      1459
OverallQual       0
GrLivArea         0
GarageCars        1
TotalBsmtSF       1
FullBath          0
YearBuilt         0
dtype: int64
housing.GarageCars = housing.GarageCars.fillna(housing.GarageCars.mean())
housing.TotalBsmtSF = housing.TotalBsmtSF.fillna(housing.TotalBsmtSF.mean())
train = housing[:len_train]
test = housing[len_train:]

5、训练模型

xtrain=train.drop("SalePrice",axis=1)
ytrain=train['SalePrice']
xtest=test.drop("SalePrice", axis=1)
from math import sqrt
train_X, val_X, train_y, val_y = train_test_split(xtrain, ytrain, random_state=1)
my_pipeline = XGBRegressor(n_estimators=1000, learning_rate=0.05)
my_pipeline.fit(train_X, train_y)
val_preds = my_pipeline.predict(val_X)
msel = mean_squared_error(np.log(val_preds), np.log(val_y))
print("RMSE: %2f" %sqrt(msel))
RMSE: 0.173459
test_preds = my_pipeline.predict(xtest)output = pd.DataFrame({'Id': test_Id,'SalePrice': test_preds})
output.set_index('Id').to_csv('submission.csv')

6、异常值剔除

var = 'GrLivArea'
data = pd.concat([train['SalePrice'], train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

train = train.drop(train[train.index == 1298].index)
train = train.drop(train[train.index == 523].index)
var = 'GrLivArea'
data = pd.concat([train['SalePrice'], train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));
'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'.  Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

xtrain=train.drop("SalePrice",axis=1)
ytrain=train['SalePrice']
xtest=test.drop("SalePrice", axis=1)
from math import sqrt
train_X, val_X, train_y, val_y = train_test_split(xtrain, ytrain, random_state=1)my_pipeline = XGBRegressor(n_estimators=1000, learning_rate=0.05)
my_pipeline.fit(train_X, train_y)val_preds = my_pipeline.predict(val_X)
msel = mean_squared_error(np.log(val_preds), np.log(val_y))
print("RMSE: %2f" %sqrt(msel))
RMSE: 0.162534
test_preds = my_pipeline.predict(xtest)output = pd.DataFrame({'Id': test_Id,'SalePrice': test_preds})
output.set_index('Id').to_csv('submission2.csv')

最终结果0.169,排名75%。后续优化方向,变量选择、特征工程、模型选择。

kaggle房价预测特征意思_Kaggle实战-波士顿房价预测相关推荐

  1. kaggle房价预测特征意思_Kaggle竞赛丨房价预测(House Prices)

    典型的机器学习流程如下图所示: 机器学习流程 数据收集→数据探索→数据预处理→模型训练→模型评估→性能改进→上线部署 如下视频介绍了机器学习的流程,感兴趣的同学可以点击查看: 机器学习介绍片https ...

  2. kaggle房价预测特征意思_kaggle入门之 房价预测

    背景介绍: 这个比赛总的情况就是给你79个特征然后根据这些预测房价(SalePrice),难点在于特征很多,且存在大量的缺失值.kaggle提供的data_description.txt这个文件,里面 ...

  3. Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析

    Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析 此数据源于美国某经济学杂志上,分析研究波士顿房价( Boston HousePrice)的数据集. 在这个项目中,你将利用马萨诸塞州波 ...

  4. 机器学习入门实战---波士顿房价预测

    波士顿房价预测 波士顿房价数据集介绍 波士顿房价数据说明:此数据源于美国某经济学杂志上,分析研究波士顿房价( Boston HousePrice)的数据集.数据集中的每一行数据都是对波士顿周边或城镇房 ...

  5. 波士顿房价预测python决策树_机器学习·波士顿房价预测模型

    模型评估与验证 当我们的机器学习模型建立好之后,如何训练数据以获得最优的模型参数,又用什么指标来评价模型的优劣呢?本文以波士顿房价为例,说明如何训练数据以及对模型做评估,辨别模型的优劣. 第一步:导入 ...

  6. python波士顿房价是什么数据,Python数据分析 | 波士顿房价回归分析

    分析目标: 将波士顿房价的数据集进行描述性数据分析.预测性数据分析(主要用了回归分析),可用于预测房价. 数据集介绍: 卡内基梅隆大学收集,StatLib库,1978年,涵盖了麻省波士顿的506个不同 ...

  7. kaggle房价预测特征意思_Kaggle之预测房价

    分析背景 要求购房者描述他们梦想中的房子,他们可能不会从地下室天花板的高度或靠近东西方铁路开始.但是这个游乐场比赛的数据集证明了价格谈判比卧室或白色栅栏的数量更多. 有79个解释变量描述(几乎)爱荷华 ...

  8. kaggle房价预测特征意思_Kaggle初探--房价预测案例之数据分析

    概述 在做的过程中,浏览了好多出色的报告,受益匪浅,浏览的文章主要包括: import pandas as pd import numpy as np import seaborn as sns fr ...

  9. python数据分析项目实战波士顿房价预测——手写梯度下降法

    导入所需要的库 import numpy as np import pandas as pd from matplotlib import font_manager as fm, rcParams i ...

最新文章

  1. 考考你:输入数字,判定空格和回车
  2. 美国专利商标局发布人工智能专利扩散分析报告
  3. linux上logbok实时日志_日志lombok插件安装及配置
  4. 从缓冲上看阻塞与非阻塞socket在发送接收上的区别(转载)
  5. RNN循环神经网络实现预测比特币价格过程详解
  6. 漫步数学分析十三——路径连通
  7. python不支持prelu_MTCNN(九)更改python与c代码的PReLU为ReLU
  8. Java classLoader【转】
  9. 南京理工大学计算机专业考研,2020南京理工大学计算机考研初试科目、参考书目、复试详情汇总...
  10. 女孩们,当你说没有好男人时请进来看看!
  11. ORA-22285: 对不存在的目录或文件进行 FILEOPEN 操作 ORA-06512: 在 SYS.DBMS_LOB, line 523 ORA-06512: 在 line 6 查看错误堆
  12. 区块链单笔交易字段解释
  13. string、Empty和null三者的区别(来源:网管之家bitsCN.com)
  14. java画笑脸_canvas 画笑脸
  15. 从高级语言实现ads 通信_4000通讯ADS通讯-高级语言-资源下载-读根文库
  16. Excel图表美化及样式设置教程
  17. kettle连接不上es7_kettle常见问题解决
  18. UEFI 、GPT 分区安装 Windows10
  19. l05173芯片针脚图_汽车常见易损芯片
  20. conda 配置清华源

热门文章

  1. 希望今年能看懂和写出这样的Swift代码
  2. visual studio 调试时提示 已加载“C:\Windows\SysWOW64\ntdll.dll”。无法查找或打开 PDB 文件。
  3. java小数转换成百分数_在java中如何把小数转化成百分数
  4. BUUCTF--Misc---easycap 追踪TCP流
  5. php js vbs,VBScript版的PHP extract()函数
  6. Python中list复制引发的问题
  7. Python基础教程:绑定方法和非绑定详细用法
  8. Python正则表达式的7个使用典范
  9. Python 基础教程:位运算的奥妙
  10. Python 神工具包!翻译、文字识别、语音转文字统统搞定