0 前言
?? 優(yōu)質(zhì)競(jìng)賽項(xiàng)目系列,今天要分享的是
?? 大數(shù)據(jù)房?jī)r(jià)預(yù)測(cè)分析與可視
??學(xué)長(zhǎng)這里給一個(gè)題目綜合評(píng)分(每項(xiàng)滿(mǎn)分5分)
- 難度系數(shù):3分
- 工作量:3分
- 創(chuàng)新點(diǎn):4分
該項(xiàng)目較為新穎,適合作為競(jìng)賽課題方向,學(xué)長(zhǎng)非常推薦!
?? 更多資料, 項(xiàng)目分享:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-835364.html
https://gitee.com/dancheng-senior/postgraduate
1 課題背景
Ames數(shù)據(jù)集包含來(lái)自Ames評(píng)估辦公室的2930條記錄。
該數(shù)據(jù)集具有23個(gè)定類(lèi)變量,23個(gè)定序變量,14個(gè)離散變量和20個(gè)連續(xù)變量(以及2個(gè)額外的觀察標(biāo)識(shí)符) - 總共82個(gè)特征。
可以在包含的codebook.txt文件中找到每個(gè)變量的說(shuō)明。
該信息用于計(jì)算2006年至2010年在愛(ài)荷華州艾姆斯出售的個(gè)別住宅物業(yè)的評(píng)估價(jià)值。實(shí)際銷(xiāo)售價(jià)格中增加了一些噪音,因此價(jià)格與官方記錄不符。
分別分為訓(xùn)練和測(cè)試集,分別為2000和930個(gè)觀測(cè)值。 在測(cè)試集中保留實(shí)際銷(xiāo)售價(jià)格。 此外,測(cè)試數(shù)據(jù)進(jìn)一步分為公共和私有測(cè)試集。
本次練習(xí)需要圍繞以下目的進(jìn)行:
- 理解問(wèn)題 : 觀察每個(gè)變量特征的意義以及對(duì)于問(wèn)題的重要程度
- 研究主要特征 : 也就是最終的目的變量----房?jī)r(jià)
- 研究其他變量 : 研究其他多變量對(duì)“房?jī)r(jià)”的影響的他們之間的關(guān)系
- 基礎(chǔ)的數(shù)據(jù)清理 : 對(duì)一些缺失數(shù)據(jù)、異常點(diǎn)和分類(lèi)數(shù)據(jù)進(jìn)行處理
- 擬合模型: 建立一個(gè)預(yù)測(cè)房屋價(jià)值的模型,并且準(zhǔn)確預(yù)測(cè)房?jī)r(jià)
2 導(dǎo)入相關(guān)的數(shù)據(jù)
1.導(dǎo)入相關(guān)的python包
?
? import numpy as np
?
import pandas as pd
from pandas.api.types import CategoricalDtype
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
from 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'] = 12
2. 導(dǎo)入訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集
?
? 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)
? #設(shè)置value的顯示長(zhǎng)度為100,默認(rèn)為50
? pd.set_option('max_colwidth',100)
? training_data.head(7)
3 觀察各項(xiàng)主要特征與房屋售價(jià)的關(guān)系
該數(shù)據(jù)集具有46個(gè)類(lèi)別型變量,34個(gè)數(shù)值型變量,整理到excel表格中,用于篩選與房?jī)r(jià)息息相關(guān)的變量。從中篩選出以下幾個(gè)與房?jī)r(jià)相關(guān)的變量:
類(lèi)別型變量:
-
Utilities : 可用設(shè)施(電、天然氣、水)
-
Heating (Nominal): 暖氣類(lèi)型
-
Central Air (Nominal): 是否有中央空調(diào)
-
Garage Type (Nominal): 車(chē)庫(kù)位置
-
Neighborhood (Nominal): Ames市區(qū)內(nèi)的物理位置(地圖地段)
-
Overall Qual (Ordinal): 評(píng)估房屋的整體材料和光潔度
數(shù)值型變量:
-
Lot Area(Continuous):地皮面積(平方英尺)
-
Gr Liv Area (Continuous): 地面以上居住面積平方英尺
-
Total Bsmt SF (Continuous): 地下面積的總面積
-
TotRmsAbvGrd (Discrete): 地面上全部房間數(shù)目
分析最重要的變量"SalePrice"
?
training_data['SalePrice'].describe()
從上面的描述性統(tǒng)計(jì)可以看出房?jī)r(jià)的平均值、標(biāo)準(zhǔn)差、最小值、25%分位數(shù)、50%分位數(shù)、75%分位數(shù)、最大值等,并且SalePrice沒(méi)有無(wú)效或者其他非數(shù)值的數(shù)據(jù)。
#繪制"SalePrice"的直方圖
sns.distplot(training_data['SalePrice'])
#計(jì)算峰度和偏度
print("Skewness: %f" % training_data['SalePrice'].skew())
print("Kurtosis: %f" % training_data['SalePrice'].kurt())
從直方圖中可以看出"SalePrice"成正態(tài)分布,峰度為4.838055,偏度為1.721408,比正態(tài)分布的高峰更加陡峭,偏度為右偏,長(zhǎng)尾拖在右邊。
2.類(lèi)別型變量
(1)Utilities與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
?
? #類(lèi)別型變量
? #1.Utilities
? var = 'Utilities'
? data = pd.concat([training_data['SalePrice'], training_data[var]], axis=1)
? fig = sns.boxplot(x=var, y="SalePrice", data=data)
? fig.axis(ymin=0, ymax=800000)
從圖中可以看出,配備全套設(shè)施(水、電、天然氣)的房子價(jià)格普遍偏高
(2)Heating與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]], axis=1)
? fig = sns.boxplot(x=var, y="SalePrice", data=data)
? fig.axis(ymin=0, ymax=800000)
從圖中可以看出擁有GasA、GasW的房子價(jià)格較高,并且有GasA的房子價(jià)格變動(dòng)較大,房屋價(jià)格較高的房子一般都有GasA制暖裝置。
(3)Central_Air與SalePrice
? #3.Central_Air
? var = 'Central_Air'
? data = pd.concat([training_data['SalePrice'], training_data[var]], axis=1)
? fig = sns.boxplot(x=var, y="SalePrice", data=data)
? fig.axis(ymin=0, ymax=800000)
由中央空調(diào)的房子能給用戶(hù)更好的體驗(yàn),因此一般價(jià)格較高,房屋價(jià)格較高的房子一般都有中央空調(diào)。
(4)Gabage_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]], axis=1)
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000)
車(chē)庫(kù)越便捷,一般房屋價(jià)格越高,臨近房屋以及房屋內(nèi)置的車(chē)庫(kù)這兩種價(jià)格較高。
(5)Neighborhood與SalePrice
Neighborhood為房屋位于Ames市內(nèi)的具體的地段,越臨近繁華市區(qū)、旅游風(fēng)景區(qū)、科技園區(qū)、學(xué)園區(qū)的房屋,房屋價(jià)格越貴
?
? #5.Neighborhood
? fig, axs = plt.subplots(nrows=2)
?
sns.boxplot(
x='Neighborhood',
y='SalePrice',
data=training_data.sort_values('Neighborhood'),
ax=axs[0]
)
sns.countplot(
x='Neighborhood',
data=training_data.sort_values('Neighborhood'),
ax=axs[1]
)
# Draw median price
axs[0].axhline(
y=training_data['SalePrice'].median(),
color='red',
linestyle='dotted'
)
# 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), ha='center', va='bottom')
# Format x-axes
axs[1].set_xticklabels(axs[1].xaxis.get_majorticklabels(), rotation=90)
axs[0].xaxis.set_visible(False)
# Narrow the gap between the plots
plt.subplots_adjust(hspace=0.01)
從上圖結(jié)果可以看出,我們訓(xùn)練數(shù)據(jù)集中Neighborhood這一列數(shù)據(jù)不均勻,NAmes有299條數(shù)據(jù),而B(niǎo)lueste只有4條數(shù)據(jù),Gilbert只有6條數(shù)據(jù),GmHill只有2條數(shù)據(jù),這樣造成數(shù)據(jù)沒(méi)那么準(zhǔn)確。
(6)Overall Qual 與SalePrice
總體評(píng)價(jià)越高,應(yīng)該房屋的價(jià)格越高
#Overall Qual
var = 'Overall_Qual'
data = pd.concat([training_data['SalePrice'], training_data[var]], axis=1)
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000)
3.數(shù)值型變量
(1) Lot Area與SalePrice
?
#數(shù)值型變量
#1.Lot Area
sns.jointplot(
x='Lot_Area',
y='SalePrice',
data=training_data,
stat_func=None,
kind="reg",
ratio=4,
space=0,
scatter_kws={
's': 3,
'alpha': 0.25
},
line_kws={
'color': 'black'
}
)
看起來(lái)沒(méi)有什么明顯的趨勢(shì),散點(diǎn)圖主要集中在前半部分,不夠分散
(2)Gr_Liv_Area與SalePrice
Gr_Liv_Area代表建筑在土地上的房屋的面積
猜測(cè)兩者應(yīng)該成正相關(guān),即房屋面積越大,房屋的價(jià)格越高
?
sns.jointplot(
x='Gr_Liv_Area',
y='SalePrice',
data=training_data,
stat_func=None,
kind="reg",
ratio=4,
space=0,
scatter_kws={
's': 3,
'alpha': 0.25
},
line_kws={
'color': 'black'
}
)
結(jié)果:兩者的確呈現(xiàn)正相關(guān)的線(xiàn)性關(guān)系,發(fā)現(xiàn)Gr_ Liv _ Area中有處于5000以上的異常值
編寫(xiě)函數(shù),將5000以上的Gr_ Liv _ Area異常值移除
?
? def remove_outliers(data, variable, lower=-np.inf, upper=np.inf):
? """
? Input:
? data (data frame): the table to be filtered
? variable (string): the column with numerical outliers
? lower (numeric): observations with values lower than this will be removed
? upper (numeric): observations with values higher than this will be removed
?
Output:
a winsorized data frame with outliers removed
"""
data=data[(data[variable]>lower)&(data[variable]
再次繪圖
兩者的確呈現(xiàn)正相關(guān)的線(xiàn)性關(guān)系
(3)Total_Bsmt_SF與SalePrice
? #3.Total Bsmt SF
? sns.jointplot(
? x='Total_Bsmt_SF',
? y='SalePrice',
? data=training_data,
? stat_func=None,
? kind="reg",
? ratio=4,
? space=0,
? scatter_kws={
? 's': 3,
? 'alpha': 0.25
? },
? line_kws={
? 'color': 'black'
? }
? )
(4)TotRms_AbvGrd與SalePrice
#4.TotRmsAbvGrd
sns.jointplot(
x='TotRms_AbvGrd',
y='SalePrice',
data=training_data,
stat_func=None,
kind="reg",
ratio=4,
space=0,
scatter_kws={
's': 3,
'alpha': 0.25
},
line_kws={
'color': 'black'
}
)
4. 繪制相關(guān)性矩陣
#繪制相關(guān)性矩陣
corrmat = training_data.corr()
f, ax = plt.subplots(figsize=(40, 20))
sns.heatmap(corrmat, vmax=0.8,square=True,cmap="PiYG",center=0.0)
其中數(shù)值型變量中,Overall_Qual(房屋的整體評(píng)價(jià)) 、Year_Built(房屋建造年份)、Year_Remod/Add(房屋整修年份)、Mas
Vnr Area(房屋表層砌體模型)、Total_ Bsmt _ SF(地下總面積)、1stFlr_SF(一樓總面積) 、 Gr_ L
iv_Area(地上居住面積)、Garage_Cars (車(chē)庫(kù)數(shù)量)、Garage_Area(車(chē)庫(kù)面積)都與呈正相關(guān)
最后從Year_Built(房屋建造年份)、Year_Remod/Add(房屋整修年份)中選取Year_Built,從1stFlr_SF(一樓總面積)
、 Gr_ L iv_Area(地上居住面積)中選取Gr_ L iv_Area,從Garage_Cars
(車(chē)庫(kù)數(shù)量)、Garage_Area(車(chē)庫(kù)面積)中選取Garage_Cars (車(chē)庫(kù)數(shù)量)。
6. 擬合模型
sklearn中的回歸有多種方法,廣義線(xiàn)性回歸集中在linear_model庫(kù)下,例如普通線(xiàn)性回歸、Lasso、嶺回歸等;另外還有其他非線(xiàn)性回歸方法,例如核svm、集成方法、貝葉斯回歸、K近鄰回歸、決策樹(shù)回歸、隨機(jī)森林回歸方法等,通過(guò)測(cè)試各個(gè)算法的
(1)加載相應(yīng)包
?
? #擬合數(shù)據(jù)
? 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 np
(2)查看各列缺失值
#查看各列缺失值
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())
發(fā)現(xiàn)Total_Bsmt_SF和Mas_Vnr_Area兩列有缺失值
?
? #用均值填補(bǔ)缺失值
? training_data.Total_Bsmt_SF=training_data.Total_Bsmt_SF.fillna(training_data.Total_Bsmt_SF.mean())
? training_data.Mas_Vnr_Area=training_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)擬合模型
? # 獲取數(shù)據(jù)
? from sklearn import metrics
? cols = ['Overall_Qual','Gr_Liv_Area', 'Garage_Cars','Total_Bsmt_SF', 'Year_Built','Mas_Vnr_Area']
? x = training_data[cols].values
? y = training_data['SalePrice'].values
? X_train,X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
?
clf = RandomForestRegressor(n_estimators=400)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
計(jì)算MSE:
print(metrics.mean_squared_error(y_test,y_pred))
(4)繪制預(yù)測(cè)結(jié)果的散點(diǎn)圖
?
? import numpy as np
? x = np.random.rand(660)
? plt.scatter(x,y_test, alpha=0.5)
? plt.scatter(x,y_pred, alpha=0.5,color="G")
(5)加載測(cè)試集數(shù)據(jù)
test_data=pd.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())
#用均值填補(bǔ)缺失值
test_data.Garage_Cars=training_data.Garage_Cars.fillna(training_data.Garage_Cars.mean())
print(test_data.Garage_Cars.isnull().any())
(6)預(yù)測(cè)測(cè)試集的房?jī)r(jià)
? #預(yù)測(cè)
? cols = ['Overall_Qual','Gr_Liv_Area', 'Garage_Cars','Total_Bsmt_SF', 'Year_Built','Mas_Vnr_Area']
? x_test_value= test_data[cols].values
? test_pre=clf.predict(x_test_value)
? #寫(xiě)入文件
? prediction = pd.DataFrame(test_pre, columns=['SalePrice'])
? result = pd.concat([test_data['Id'], prediction], axis=1)
? result.to_csv('./Predictions.csv', index=False)
?
test_data.Garage_Cars=training_data.Garage_Cars.fillna(training_data.Garage_Cars.mean())
print(test_data.Garage_Cars.isnull().any())
(6)預(yù)測(cè)測(cè)試集的房?jī)r(jià)
?
? #預(yù)測(cè)
? cols = ['Overall_Qual','Gr_Liv_Area', 'Garage_Cars','Total_Bsmt_SF', 'Year_Built','Mas_Vnr_Area']
? x_test_value= test_data[cols].values
? test_pre=clf.predict(x_test_value)
? #寫(xiě)入文件
? prediction = pd.DataFrame(test_pre, columns=['SalePrice'])
? result = pd.concat([test_data['Id'], prediction], axis=1)
? result.to_csv('./Predictions.csv', index=False)
4 最后
該項(xiàng)目較為新穎,適合作為競(jìng)賽課題方向,學(xué)長(zhǎng)非常推薦!
?? 更多資料, 項(xiàng)目分享:
[https://gitee.com/dancheng-senior/postgraduate](文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-835364.html
到了這里,關(guān)于互聯(lián)網(wǎng)加競(jìng)賽 大數(shù)據(jù)房?jī)r(jià)預(yù)測(cè)分析與可視的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!