北科電子大三上機器學習 使用回歸預測房價

筆記說明

此筆記用途在於台北科技大學電子大三上機器學習作業紀錄
並非所有人都適用,部分對我而言稍加容易的內容並不會寫在此內。
這是學習後心得的筆記,可能不太適用會未來的學生

由於我沒有學習過裡面的理論,因此這是資財大三學生透過網路與自身理解的筆記,學習價值並不高、且可能擁有大量錯誤。

台北科大深度學習 - 房價預測回歸模型

題目說明

使用 keras 中的 sequence 進行房價預測,在 kaggle Private Leaderboard 獲得 136 名

查看房價預測中的所有欄位說明

輸出欄位要求必須為

輸入資料

1
2
3
4
5
6
7
8
9
10
11
12
13
#輸入資料
#train csv
train = pd.read_csv("./regression_data/train-v3.csv")

X_train = train.drop(["price", "id"], axis=1)
Y_train = train["price"].values
#print(X_train)

valid = pd.read_csv("./regression_data/valid-v3.csv")
#print(train)
X_valid = valid.drop(["price", "id"], axis=1)
Y_valid = valid["price"].values
#print(X_valid)

資料預處理

這邊我使用了幾種不同方式的資料預處理,但發現其實都沒有比不處理來的好

將內部相關係數大於 0.7 的選擇一個移除

先來檢測內部參數相關性,如果太相關那我們就選一個拔除,避免回歸時某些類型被重複強調。

其中我將以下相關性過高的欄位進行移除

  • bathrooms 與 sqft_living 有相關性,刪除 bathrooms
  • grade 與 sqft_living 有相關性,刪除 grade
  • sqft_above 與 sqft_living 有相關性,刪除 sqft_above
  • sqft_living15 與 sqft_living 有相關性,刪除 sqft_living15
  • sqft_lot15 與 sqft_lot 有相關性,刪除 sqft_lot15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import seaborn as sns 

train_cor = train.corr(method = "pearson").round(2)
print(train_cor)
plt.subplots(figsize=(13, 16))
sns.heatmap(train_cor, annot = True, cmap = "Blues")


#bathrooms 與 sqft_living 有相關性,刪除 bathrooms
#grade 與 sqft_living 有相關性,刪除 grade
#sqft_above 與 sqft_living 有相關性,刪除 sqft_above
#sqft_living15 與 sqft_living 有相關性,刪除 sqft_living15
#sqft_lot15 與 sqft_lot 有相關性,刪除 sqft_lot15
train_cor = train.drop(["bathrooms", "grade", "sqft_above", "sqft_living15", "sqft_lot15"], axis=1)
train_cor = train_cor.corr(method = "pearson").round(2)
plt.subplots(figsize=(12, 9))
sns.heatmap(train_cor, annot = True, cmap = "Blues")


X_train = X_train.drop(["bathrooms", "grade", "sqft_above", "sqft_living15", "sqft_lot15"], axis=1) #共線性檢視完畢
X_valid = X_valid.drop(["bathrooms", "grade", "sqft_above", "sqft_living15", "sqft_lot15"], axis=1) #共線性檢視完畢

但沒有比不做處理就直接回歸來的好

將 year、month、day 進行移除

由於我認為時間序列在回歸中會搗亂預測、讓預測模糊因此我 drop 這三個 column 然後進行回歸。

1
2
3
4
5
from sklearn.preprocessing import StandardScaler


X_train = X_train.drop(["sale_yr", "sale_month", "sale_day"], axis=1) #共線性檢視完畢
X_valid = X_valid.drop(["sale_yr", "sale_month", "sale_day"], axis=1) #共線性檢視完畢

但沒有比不做處理就直接回歸來的好

開始回歸

使用 sequential 然後我們加入隱藏層並 unit 從 256 不斷除 2,在不斷加入在 model 上

1
2
3
4
5
6
7
8
9
model = Sequential()
model.add(Dense(250, input_dim =X_train.shape[1], kernel_initializer= "random_normal", activation="relu" ))
model.add(Dense(125, kernel_initializer= "random_normal", activation="relu" ))
model.add(Dense(60, kernel_initializer= "random_normal", activation="relu" ))
model.add(Dense(30, kernel_initializer= "random_normal", activation="relu" ))
model.add(Dense(15, kernel_initializer= "random_normal", activation="relu" ))
model.add(Dense(1, kernel_initializer= "random_normal", activation="relu" ))

model.compile(loss="mean_squared_error", optimizer="adam") #設定 model 的損失函數、優化器

訓練開始

設定好 model epoch 並且將訓練集、測試集資料放入,就等一段時間開始訓練拉

1
2
3
4
5
6
7
8
9
10

from keras.callbacks import ModelCheckpoint
call = ModelCheckpoint("regression.model",
monitor = "val_loss",
)

fit = model.fit(X_train, Y_train,
validation_data = (X_valid, Y_valid),
callbacks = [call],
epochs = 320)

查看差異,1~100 筆預設與實務差異

我們訓練完資料後可以看看預測跟實際上的資料差多少

其中我們可以看到我的預測都較保守,實際答案都比我的預測超出許多,有些甚至到了超級多。

  • 藍色線表示 predict
  • 橘色線表示 valid

總結

  • 不正確的資料預處理並不會導致 kaggle MSE 分數降低
  • 像今天上課同學報告的一樣,zipcode 比較沒有甚麼意義,也許可以變成頻率分析
  • 可以自己加入一些加減乘除的變數,例如室內面積 / 浴室面積比,會有一些參考價值存在
  • 每次 model 完成後,可以畫出 model accuracy、model loss 圖,方便自己更了解問題所在

我的 colab 供大家參考

參考連結

Display Deep Learning Model Training History in Keras 教學劃出 model accuracy、model loss by Jason Brownlee
ModelCheckpoint by Keras
Keras深度学习笔记 非线性回归 by csdn imxlw00
Sequential 模型 API by Keras
损失函数的使用 by Keras
ReLu Function in Python by Keras
Adam优化器 by 機器之心
python3.x-seaborn.heatmap随笔 by wiliken
pandas.DataFrame.drop by pandas
fit_transform,fit,transform区别和作用详解!!!!!! by 九点澡堂子
Python NumPy numpy.shape() 函式 by DelftStack

  • 版權聲明: 本部落格所有文章除有特別聲明外,均採用 Apache License 2.0 許可協議。轉載請註明出處!
  • © 2020-2024 John Doe
  • Powered by Hexo Theme Ayer
  • PV: UV: