海天建设集团公司网站,新发布手机,怎么给网站做动图,茶叶营销策划方案文章目录 0 前言1 课题背景2 导入相关的数据 3 观察各项主要特征与房屋售价的关系4 最后 0 前言
#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升#xff0c;传统的毕设题目缺少创新和亮点#xff0c;往往达不到毕业答辩的要求#xff0c;这两年不断有学弟… 文章目录 0 前言1 课题背景2 导入相关的数据 3 观察各项主要特征与房屋售价的关系4 最后 0 前言 这两年开始毕业设计和毕业答辩的要求和难度不断提升传统的毕设题目缺少创新和亮点往往达不到毕业答辩的要求这两年不断有学弟学妹告诉学长自己做的项目系统达不到老师的要求。
为了大家能够顺利以及最少的精力通过毕设学长分享优质毕业设计项目今天要分享的是 大数据房价预测分析与可视
学长这里给一个题目综合评分(每项满分5分)
难度系数3分工作量3分创新点3分
1 课题背景
Ames数据集包含来自Ames评估办公室的2930条记录。 该数据集具有23个定类变量23个定序变量14个离散变量和20个连续变量以及2个额外的观察标识符 - 总共82个特征。 可以在包含的codebook.txt文件中找到每个变量的说明。 该信息用于计算2006年至2010年在爱荷华州艾姆斯出售的个别住宅物业的评估价值。实际销售价格中增加了一些噪音因此价格与官方记录不符。
分别分为训练和测试集分别为2000和930个观测值。 在测试集中保留实际销售价格。 此外测试数据进一步分为公共和私有测试集。
本次练习需要围绕以下目的进行
理解问题 观察每个变量特征的意义以及对于问题的重要程度研究主要特征 也就是最终的目的变量----房价研究其他变量 研究其他多变量对“房价”的影响的他们之间的关系基础的数据清理 对一些缺失数据、异常点和分类数据进行处理拟合模型 建立一个预测房屋价值的模型并且准确预测房价 2 导入相关的数据
1.导入相关的python包
import numpy as npimport pandas as pd
from pandas.api.types import CategoricalDtype%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as snsfrom sklearn import linear_model as lm
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold# Plot settings
plt.rcParams[figure.figsize] (12, 9)
plt.rcParams[font.size] 122. 导入训练数据集和测试数据集
training_data pd.read_csv(ames_train.csv)
test_data pd.read_csv(ames_test.csv)
pd.set_option(display.max_columns, None)
#显示所有行
pd.set_option(display.max_rows, None)
#设置value的显示长度为100默认为50
pd.set_option(max_colwidth,100)
training_data.head(7)3 观察各项主要特征与房屋售价的关系
该数据集具有46个类别型变量34个数值型变量整理到excel表格中用于筛选与房价息息相关的变量。从中筛选出以下几个与房价相关的变量
类别型变量 Utilities : 可用设施电、天然气、水 Heating (Nominal): 暖气类型 Central Air (Nominal): 是否有中央空调 Garage Type (Nominal): 车库位置 Neighborhood (Nominal): Ames市区内的物理位置地图地段 Overall Qual (Ordinal): 评估房屋的整体材料和光洁度
数值型变量 Lot AreaContinuous地皮面积平方英尺 Gr Liv Area (Continuous): 地面以上居住面积平方英尺 Total Bsmt SF (Continuous): 地下面积的总面积 TotRmsAbvGrd (Discrete): 地面上全部房间数目
分析最重要的变量SalePrice
training_data[SalePrice].describe()从上面的描述性统计可以看出房价的平均值、标准差、最小值、25%分位数、50%分位数、75%分位数、最大值等并且SalePrice没有无效或者其他非数值的数据。
#绘制SalePrice的直方图
sns.distplot(training_data[SalePrice])
#计算峰度和偏度
print(Skewness: %f % training_data[SalePrice].skew())
print(Kurtosis: %f % training_data[SalePrice].kurt())从直方图中可以看出SalePrice成正态分布峰度为4.838055偏度为1.721408比正态分布的高峰更加陡峭偏度为右偏长尾拖在右边。
2.类别型变量
1Utilities与SalePrice
Utilities (Ordinal): Type of utilities available
AllPub All public Utilities (E,G,W, S)
NoSewr Electricity, Gas, and Water (Septic Tank)
NoSeWa Electricity and Gas Only
ELO Electricity only
#类别型变量
#1.Utilities
var Utilities
data pd.concat([training_data[SalePrice], training_data[var]], axis1)
fig sns.boxplot(xvar, ySalePrice, datadata)
fig.axis(ymin0, ymax800000)从图中可以看出配备全套设施水、电、天然气的房子价格普遍偏高
2Heating与SalePrice
Heating (Nominal): Type of heating
Floor Floor Furnace
GasA Gas forced warm air furnace
GasW Gas hot water or steam heat
Grav Gravity furnace
OthW Hot water or steam heat other than gas
Wall Wall furnace
#2.Heating
var Heating
data pd.concat([training_data[SalePrice], training_data[var]], axis1)
fig sns.boxplot(xvar, ySalePrice, datadata)
fig.axis(ymin0, ymax800000)从图中可以看出拥有GasA、GasW的房子价格较高并且有GasA的房子价格变动较大房屋价格较高的房子一般都有GasA制暖装置。
3Central_Air与SalePrice
#3.Central_Air
var Central_Air
data pd.concat([training_data[SalePrice], training_data[var]], axis1)
fig sns.boxplot(xvar, ySalePrice, datadata)
fig.axis(ymin0, ymax800000)由中央空调的房子能给用户更好的体验因此一般价格较高房屋价格较高的房子一般都有中央空调。
4Gabage_type与SalePrice
Garage Type (Nominal): Garage location
2Types More than one type of garage
Attchd Attached to home
Basment Basement Garage
BuiltIn Built-In (Garage part of house - typically has room above garage)
CarPort Car Port
Detchd Detached from home
NA No Garage
#4.Gabage_type
var Garage_Type
data pd.concat([training_data[SalePrice], training_data[var]], axis1)
fig sns.boxplot(xvar, ySalePrice, datadata)
fig.axis(ymin0, ymax800000)车库越便捷一般房屋价格越高临近房屋以及房屋内置的车库这两种价格较高。
5Neighborhood与SalePrice
Neighborhood为房屋位于Ames市内的具体的地段越临近繁华市区、旅游风景区、科技园区、学园区的房屋房屋价格越贵
#5.Neighborhood
fig, axs plt.subplots(nrows2)sns.boxplot(xNeighborhood,ySalePrice,datatraining_data.sort_values(Neighborhood),axaxs[0]
)sns.countplot(xNeighborhood,datatraining_data.sort_values(Neighborhood),axaxs[1]
)# Draw median price
axs[0].axhline(ytraining_data[SalePrice].median(), colorred,linestyledotted
)# Label the bars with counts
for patch in axs[1].patches:x patch.get_bbox().get_points()[:, 0]y patch.get_bbox().get_points()[1, 1]axs[1].annotate(f{int(y)}, (x.mean(), y), hacenter, vabottom)# Format x-axes
axs[1].set_xticklabels(axs[1].xaxis.get_majorticklabels(), rotation90)
axs[0].xaxis.set_visible(False)# Narrow the gap between the plots
plt.subplots_adjust(hspace0.01)从上图结果可以看出我们训练数据集中Neighborhood这一列数据不均匀NAmes有299条数据而Blueste只有4条数据Gilbert只有6条数据GmHill只有2条数据这样造成数据没那么准确。
6Overall Qual 与SalePrice
总体评价越高应该房屋的价格越高
#Overall Qual
var Overall_Qual
data pd.concat([training_data[SalePrice], training_data[var]], axis1)
fig sns.boxplot(xvar, ySalePrice, datadata)
fig.axis(ymin0, ymax800000)3.数值型变量
1 Lot Area与SalePrice
#数值型变量
#1.Lot Area
sns.jointplot(xLot_Area, ySalePrice, datatraining_data,stat_funcNone,kindreg,ratio4,space0,scatter_kws{s: 3,alpha: 0.25},line_kws{color: black}
)看起来没有什么明显的趋势散点图主要集中在前半部分不够分散
2Gr_Liv_Area与SalePrice
Gr_Liv_Area代表建筑在土地上的房屋的面积
猜测两者应该成正相关即房屋面积越大房屋的价格越高
sns.jointplot(xGr_Liv_Area, ySalePrice, datatraining_data,stat_funcNone,kindreg,ratio4,space0,scatter_kws{s: 3,alpha: 0.25},line_kws{color: black}
)结果两者的确呈现正相关的线性关系发现Gr_ Liv_ Area中有处于5000以上的异常值
编写函数将5000以上的Gr_ Liv_ Area异常值移除
def remove_outliers(data, variable, lower-np.inf, uppernp.inf):Input:data (data frame): the table to be filteredvariable (string): the column with numerical outlierslower (numeric): observations with values lower than this will be removedupper (numeric): observations with values higher than this will be removedOutput:a winsorized data frame with outliers removeddatadata[(data[variable]lower)(data[variable]再次绘图 两者的确呈现正相关的线性关系
3Total_Bsmt_SF与SalePrice
#3.Total Bsmt SF
sns.jointplot(xTotal_Bsmt_SF, ySalePrice, datatraining_data,stat_funcNone,kindreg,ratio4,space0,scatter_kws{s: 3,alpha: 0.25},line_kws{color: black}
)(4)TotRms_AbvGrd与SalePrice
#4.TotRmsAbvGrd
sns.jointplot(xTotRms_AbvGrd, ySalePrice, datatraining_data,stat_funcNone,kindreg,ratio4,space0,scatter_kws{s: 3,alpha: 0.25},line_kws{color: black}
)4. 绘制相关性矩阵
#绘制相关性矩阵
corrmat training_data.corr()
f, ax plt.subplots(figsize(40, 20))
sns.heatmap(corrmat, vmax0.8,squareTrue,cmapPiYG,center0.0)其中数值型变量中Overall_Qual房屋的整体评价 、Year_Built房屋建造年份、Year_Remod/Add房屋整修年份、Mas Vnr Area房屋表层砌体模型、Total_ Bsmt_ SF地下总面积、1stFlr_SF一楼总面积 、 Gr_ L iv_Area地上居住面积、Garage_Cars 车库数量、Garage_Area车库面积都与呈正相关
最后从Year_Built房屋建造年份、Year_Remod/Add房屋整修年份中选取Year_Built从1stFlr_SF一楼总面积 、 Gr_ L iv_Area地上居住面积中选取Gr_ L iv_Area从Garage_Cars 车库数量、Garage_Area车库面积中选取Garage_Cars 车库数量。
6. 拟合模型
sklearn中的回归有多种方法广义线性回归集中在linear_model库下例如普通线性回归、Lasso、岭回归等另外还有其他非线性回归方法例如核svm、集成方法、贝叶斯回归、K近邻回归、决策树回归、随机森林回归方法等通过测试各个算法的
1加载相应包
#拟合数据
from sklearn import preprocessing
from sklearn import linear_model, svm, gaussian_process
from sklearn.ensemble import RandomForestRegressor
from sklearn.cross_validation import train_test_split
import numpy as np2查看各列缺失值
#查看各列缺失值print(training_data.Overall_Qual.isnull().any())print(training_data.Gr_Liv_Area.isnull().any())print(training_data.Garage_Cars.isnull().any())print(training_data.Total_Bsmt_SF.isnull().any())print(training_data.Year_Built.isnull().any())print(training_data.Mas_Vnr_Area.isnull().any())发现Total_Bsmt_SF和Mas_Vnr_Area两列有缺失值
#用均值填补缺失值training_data.Total_Bsmt_SFtraining_data.Total_Bsmt_SF.fillna(training_data.Total_Bsmt_SF.mean())training_data.Mas_Vnr_Areatraining_data.Mas_Vnr_Area.fillna(training_data.Mas_Vnr_Area.mean())print(training_data.Total_Bsmt_SF.isnull().any())print(training_data.Mas_Vnr_Area.isnull().any())3拟合模型
# 获取数据from sklearn import metricscols [Overall_Qual,Gr_Liv_Area, Garage_Cars,Total_Bsmt_SF, Year_Built,Mas_Vnr_Area]x training_data[cols].valuesy training_data[SalePrice].valuesX_train,X_test, y_train, y_test train_test_split(x, y, test_size0.33, random_state42)clf RandomForestRegressor(n_estimators400)clf.fit(X_train, y_train)y_pred clf.predict(X_test)计算MSEprint(metrics.mean_squared_error(y_test,y_pred))4绘制预测结果的散点图
import numpy as np
x np.random.rand(660)
plt.scatter(x,y_test, alpha0.5)
plt.scatter(x,y_pred, alpha0.5,colorG)5加载测试集数据
test_datapd.read_csv(ames_test.csv)
test_data.head(5)查看缺失值
#查看各列缺失值
print(test_data.Overall_Qual.isnull().any())
print(test_data.Gr_Liv_Area.isnull().any())
print(test_data.Garage_Cars.isnull().any())
print(test_data.Total_Bsmt_SF.isnull().any())
print(test_data.Year_Built.isnull().any())
print(test_data.Mas_Vnr_Area.isnull().any())
#用均值填补缺失值
test_data.Garage_Carstraining_data.Garage_Cars.fillna(training_data.Garage_Cars.mean())
print(test_data.Garage_Cars.isnull().any())(6)预测测试集的房价
#预测cols [Overall_Qual,Gr_Liv_Area, Garage_Cars,Total_Bsmt_SF, Year_Built,Mas_Vnr_Area]x_test_value test_data[cols].valuestest_preclf.predict(x_test_value)#写入文件prediction pd.DataFrame(test_pre, columns[SalePrice])result pd.concat([test_data[Id], prediction], axis1)result.to_csv(./Predictions.csv, indexFalse)test_data.Garage_Carstraining_data.Garage_Cars.fillna(training_data.Garage_Cars.mean())print(test_data.Garage_Cars.isnull().any())(6)预测测试集的房价
#预测
cols [Overall_Qual,Gr_Liv_Area, Garage_Cars,Total_Bsmt_SF, Year_Built,Mas_Vnr_Area]
x_test_value test_data[cols].values
test_preclf.predict(x_test_value)
#写入文件
prediction pd.DataFrame(test_pre, columns[SalePrice])
result pd.concat([test_data[Id], prediction], axis1)
result.to_csv(./Predictions.csv, indexFalse)4 最后