實戰TensorFlow房價預測
目錄
房價預測模型介紹
使用TensorFlow實現房價預測模型
使用TensorBoard可視化模型數據流圖
一、房價預測模型介紹
1.前置知識
2.單變量房價預測問題
問題描述:
根據房屋面積x來預測其銷售價格y
# 導包
# pandas是一個BSD開源協議許可的,面向python用戶的高性能和易於上手的數據結構化和數據分析工具
# seaborn是一個基於matplotlib的python數據可視化庫,它提供了更易用的高級接口,而且有易於繪製精美且信息豐富的統計圖像
import pandas as pd
import seaborn as sns
sns.set(context="notebook",style="whitegrid",palette="dark")
# 查看q前5行數據
df0 = pd.read_csv("data/data0.csv",names=["square","price"])
df0.head()
square | price | |
---|---|---|
0 | 2104 | 399900 |
1 | 1600 | 329900 |
2 | 2400 | 369000 |
3 | 1416 | 232000 |
4 | 3000 | 539900 |
# 繪圖來表示數據
# seaborn.lmplot()方法專門用於用於線性關係的可視化,適用於迴歸模型
sns.lmplot("square","price",df0,height=6,fit_reg=True)
<seaborn.axisgrid.FacetGrid at 0x1ac51644c88>
# 查看數據的詳細信息
df0.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 47 entries, 0 to 46
Data columns (total 2 columns):
square 47 non-null int64
price 47 non-null int64
dtypes: int64(2)
memory usage: 832.0 bytes
3. 多變量房價預測
a.問題描述:
根據房屋面積x1和臥室面積數量x2,預測其銷售價格y
b.查看數據
# 導包
# matplotlib是一個Python 2D繪圖庫
# mpl_toolkits.mplot3d是一個基礎3d繪圖(散點圖、平面圖、折線圖)工具集,也是matplotlib庫的一部分
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
# 讀取數據,顯示前5行數據
df1 = pd.read_csv("data/data1.csv",names=["square","bedrooms","price"])
df1.head()
square | bedrooms | price | |
---|---|---|---|
0 | 2104 | 3 | 399900 |
1 | 1600 | 3 | 329900 |
2 | 2400 | 3 | 369000 |
3 | 1416 | 2 | 232000 |
4 | 3000 | 4 | 539900 |
# 繪製3D散點圖
fig = plt.figure()
# 創建一個Axes3D object
ax = plt.axes(projection="3d")
# 設置三個座標的名稱
ax.set_xlabel("square")
ax.set_ylabel("bedrooms")
ax.set_zlabel("price")
# 繪製3D散點圖
ax.scatter3D(df1["square"],df1["bedrooms"],df1["price"],c=df1["price"],cmap="Greens")
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1ac52a5e160>
c.數據歸一化處理
然而房屋面積和臥室數量這兩個變量(特徵)在數值上差了1000倍。在這種情況下,通常先進行特徵縮放,再開始訓練,可以加速模型收斂。
# 定義歸一化函數
def normalize_feature(df):
return df.apply(lambda column:(column-column.mean()) / column.std())
# 重新查看數據
df = normalize_feature(df1)
df.head()
square | bedrooms | price | |
---|---|---|---|
0 | 0.130010 | -0.223675 | 0.475747 |
1 | -0.504190 | -0.223675 | -0.084074 |
2 | 0.502476 | -0.223675 | 0.228626 |
3 | -0.735723 | -1.537767 | -0.867025 |
4 | 1.257476 | 1.090417 | 1.595389 |
# 重新展示數據
ax = plt.axes(projection="3d")
ax.set_xlabel("square")
ax.set_ylabel("bedrooms")
ax.set_zlabel("price")
ax.scatter3D(df["square"],df["bedrooms"],df["price"],c=df["price"],cmap="Reds")
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1ac52b50438>
# 查看數據詳細信息
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 47 entries, 0 to 46
Data columns (total 3 columns):
square 47 non-null float64
bedrooms 47 non-null float64
price 47 non-null float64
dtypes: float64(3)
memory usage: 1.2 KB
d.數據處理:添加ones列(x0)
# numpy 是bsd開源協議許可的,面向python用戶的基礎科學計算庫,在多維數組上實現了線性代數、傅里葉變換和其它豐富的函數運算
# 生成一列ones,ones是n行1列的數據框,表示x0恆爲1
import numpy as np
ones = pd.DataFrame({"ones":np.ones(len(df))})
# 根據列合併數據
df = pd.concat([ones,df],axis=1)
df.head()
ones | square | bedrooms | price | |
---|---|---|---|---|
0 | 1.0 | 0.130010 | -0.223675 | 0.475747 |
1 | 1.0 | -0.504190 | -0.223675 | -0.084074 |
2 | 1.0 | 0.502476 | -0.223675 | 0.228626 |
3 | 1.0 | -0.735723 | -1.537767 | -0.867025 |
4 | 1.0 | 1.257476 | 1.090417 | 1.595389 |
# 查看詳細數據
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 47 entries, 0 to 46
Data columns (total 4 columns):
ones 47 non-null float64
square 47 non-null float64
bedrooms 47 non-null float64
price 47 non-null float64
dtypes: float64(4)
memory usage: 1.5 KB
二、使用TensorFlow實現房價預測模型
1.前期數據處理
import pandas as pd
import numpy as np
# 定義標準化的函數
def normalize_feature(df):
return df.apply(lambda column:(column-column.mean())/column.std())
df = normalize_feature(pd.read_csv("./data/data1.csv",names=["square","bedrooms","price"]))
ones = pd.DataFrame({"ones": np.ones(len(df))})
df = pd.concat([ones,df],axis=1)
df.head()
ones | square | bedrooms | price | |
---|---|---|---|---|
0 | 1.0 | 0.130010 | -0.223675 | 0.475747 |
1 | 1.0 | -0.504190 | -0.223675 | -0.084074 |
2 | 1.0 | 0.502476 | -0.223675 | 0.228626 |
3 | 1.0 | -0.735723 | -1.537767 | -0.867025 |
4 | 1.0 | 1.257476 | 1.090417 | 1.595389 |
2.獲取數據
X_data = np.array(df[df.columns[0:3]])
y_data = np.array(df[df.columns[-1]]).reshape(len(df),1)
print(X_data.shape,type(X_data))
print(y_data.shape,type(y_data))
(47, 3) <class 'numpy.ndarray'>
(47, 1) <class 'numpy.ndarray'>
3.創建線性迴歸模型
import tensorflow as tf
alpha = 0.01 # 學習率 alpha
epoch = 500 # 訓練全量數據集的輪數
with tf.name_scope('input'):
# 輸入 X,形狀[47, 3]
X = tf.placeholder(tf.float32, X_data.shape, name='X')
# 輸出 y,形狀[47, 1]
y = tf.placeholder(tf.float32, y_data.shape, name='y')
with tf.name_scope('hypothesis'):
# 權重變量 W,形狀[3,1]
W = tf.get_variable("weights",
(X_data.shape[1], 1),
initializer=tf.constant_initializer())
# 假設函數 h(x) = w0*x0+w1*x1+w2*x2, 其中x0恆爲1
# 推理值 y_pred 形狀[47,1]
y_pred = tf.matmul(X, W, name='y_pred')
with tf.name_scope('loss'):
# 損失函數採用最小二乘法,y_pred - y 是形如[47, 1]的向量。
# tf.matmul(a,b,transpose_a=True) 表示:矩陣a的轉置乘矩陣b,即 [1,47] X [47,1]
# 損失函數操作 loss
loss_op = 1 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
with tf.name_scope('train'):
# 隨機梯度下降優化器 opt
train_op = tf.train.GradientDescentOptimizer(learning_rate=alpha).minimize(loss_op)
WARNING:tensorflow:From D:\software\Anaconda\workplace\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
4.創建會話
with tf.Session() as sess:
# 初始化全局變量
sess.run(tf.global_variables_initializer())
# 創建FileWriter實例,並傳入當前會話加載的數據流圖
writer = tf.summary.FileWriter('./summary/linear-regression-1', sess.graph)
# 記錄所有損失值
loss_data = []
# 開始訓練模型
# 因爲訓練集較小,所以採用批梯度下降優化算法,每次都使用全量數據訓練
for e in range(1, epoch + 1):
_, loss, w = sess.run([train_op, loss_op, W], feed_dict={X: X_data, y: y_data})
# 記錄每一輪損失值變化情況
loss_data.append(float(loss))
if e % 10 == 0:
log_str = "Epoch %d \t Loss=%.4g \t Model: y = %.4gx1 + %.4gx2 + %.4g"
print(log_str % (e, loss, w[1], w[2], w[0]))
# 關閉FileWriter的輸出流
writer.close()
Epoch 10 Loss=0.4184 Model: y = 0.0791x1 + 0.03948x2 + 3.353e-10
Epoch 20 Loss=0.3582 Model: y = 0.1489x1 + 0.07135x2 + -5.588e-11
Epoch 30 Loss=0.3126 Model: y = 0.2107x1 + 0.09676x2 + 3.912e-10
Epoch 40 Loss=0.2778 Model: y = 0.2655x1 + 0.1167x2 + -1.863e-11
Epoch 50 Loss=0.2512 Model: y = 0.3142x1 + 0.1321x2 + 1.77e-10
Epoch 60 Loss=0.2306 Model: y = 0.3576x1 + 0.1436x2 + -4.47e-10
Epoch 70 Loss=0.2145 Model: y = 0.3965x1 + 0.1519x2 + -8.941e-10
Epoch 80 Loss=0.2018 Model: y = 0.4313x1 + 0.1574x2 + -6.24e-10
Epoch 90 Loss=0.1917 Model: y = 0.4626x1 + 0.1607x2 + -4.191e-10
Epoch 100 Loss=0.1835 Model: y = 0.4909x1 + 0.1621x2 + -5.402e-10
Epoch 110 Loss=0.1769 Model: y = 0.5165x1 + 0.162x2 + -7.125e-10
Epoch 120 Loss=0.1714 Model: y = 0.5397x1 + 0.1606x2 + -5.076e-10
Epoch 130 Loss=0.1668 Model: y = 0.5609x1 + 0.1581x2 + -8.335e-10
Epoch 140 Loss=0.1629 Model: y = 0.5802x1 + 0.1549x2 + -9.22e-10
Epoch 150 Loss=0.1596 Model: y = 0.5979x1 + 0.1509x2 + -9.011e-10
Epoch 160 Loss=0.1567 Model: y = 0.6142x1 + 0.1465x2 + -3.399e-10
Epoch 170 Loss=0.1542 Model: y = 0.6292x1 + 0.1416x2 + -2.561e-11
Epoch 180 Loss=0.152 Model: y = 0.643x1 + 0.1364x2 + -2.491e-10
Epoch 190 Loss=0.15 Model: y = 0.6559x1 + 0.131x2 + 1.164e-11
Epoch 200 Loss=0.1483 Model: y = 0.6678x1 + 0.1255x2 + 2.654e-10
Epoch 210 Loss=0.1467 Model: y = 0.6789x1 + 0.1199x2 + 1.979e-10
Epoch 220 Loss=0.1453 Model: y = 0.6892x1 + 0.1142x2 + 2.34e-10
Epoch 230 Loss=0.144 Model: y = 0.6989x1 + 0.1085x2 + 1.409e-10
Epoch 240 Loss=0.1429 Model: y = 0.708x1 + 0.1029x2 + 6.252e-10
Epoch 250 Loss=0.1419 Model: y = 0.7165x1 + 0.09736x2 + 6.834e-10
Epoch 260 Loss=0.1409 Model: y = 0.7245x1 + 0.09189x2 + 1.127e-09
Epoch 270 Loss=0.14 Model: y = 0.732x1 + 0.08653x2 + 7.765e-10
Epoch 280 Loss=0.1393 Model: y = 0.7391x1 + 0.08128x2 + 8.126e-10
Epoch 290 Loss=0.1385 Model: y = 0.7458x1 + 0.07616x2 + 5.047e-10
Epoch 300 Loss=0.1379 Model: y = 0.7522x1 + 0.07118x2 + 9.93e-10
Epoch 310 Loss=0.1373 Model: y = 0.7582x1 + 0.06634x2 + 1.224e-09
Epoch 320 Loss=0.1367 Model: y = 0.7639x1 + 0.06165x2 + 1.169e-09
Epoch 330 Loss=0.1362 Model: y = 0.7693x1 + 0.0571x2 + 1.327e-09
Epoch 340 Loss=0.1358 Model: y = 0.7744x1 + 0.0527x2 + 1.409e-09
Epoch 350 Loss=0.1353 Model: y = 0.7793x1 + 0.04845x2 + 1.588e-09
Epoch 360 Loss=0.135 Model: y = 0.784x1 + 0.04435x2 + 1.63e-09
Epoch 370 Loss=0.1346 Model: y = 0.7884x1 + 0.0404x2 + 2.116e-09
Epoch 380 Loss=0.1343 Model: y = 0.7926x1 + 0.03658x2 + 1.931e-09
Epoch 390 Loss=0.134 Model: y = 0.7966x1 + 0.03291x2 + 1.914e-09
Epoch 400 Loss=0.1337 Model: y = 0.8004x1 + 0.02938x2 + 2.179e-09
Epoch 410 Loss=0.1335 Model: y = 0.8041x1 + 0.02598x2 + 2.181e-09
Epoch 420 Loss=0.1332 Model: y = 0.8076x1 + 0.02271x2 + 2.497e-09
Epoch 430 Loss=0.133 Model: y = 0.8109x1 + 0.01957x2 + 2.655e-09
Epoch 440 Loss=0.1328 Model: y = 0.8141x1 + 0.01655x2 + 3.158e-09
Epoch 450 Loss=0.1327 Model: y = 0.8171x1 + 0.01366x2 + 3.703e-09
Epoch 460 Loss=0.1325 Model: y = 0.82x1 + 0.01087x2 + 4.021e-09
Epoch 470 Loss=0.1323 Model: y = 0.8228x1 + 0.008204x2 + 3.739e-09
Epoch 480 Loss=0.1322 Model: y = 0.8254x1 + 0.005641x2 + 3.865e-09
Epoch 490 Loss=0.1321 Model: y = 0.828x1 + 0.003183x2 + 4.31e-09
Epoch 500 Loss=0.132 Model: y = 0.8304x1 + 0.0008239x2 + 4.303e-09
估計模型:y = 0.8304x1 + 0.0008239x2 + 4.303e-09
三、使用TensorBoard可視化模型數據流圖
1.TensorBoard使用流程
2.可視化數據流圖–工作流
3.可視化數據流圖製作
a.TensorBoard查看數據流圖
b.可視化損失值
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(context="notebook", style="whitegrid", palette="dark")
ax = sns.lineplot(x='epoch', y='loss', data=pd.DataFrame({'loss': loss_data, 'epoch': np.arange(epoch)}))
ax.set_xlabel('epoch')
ax.set_ylabel('loss')
plt.show()