房价预测实战

用到的模块也是pandas、numpy、tensorflow。

数据处理

1
2
3
4
5
6
7
8
9
10
import pandas as pd
import numpy as np
#归一化处理
def normalize_feature(df):
return df.apply(lambda column:(column-column.mean())/column.std())
df=pd.read_csv("data1.csv",names=["square","bedrooms","price"])
df=normalize_feature(df)
print(df.head())
X_data=np.array(df[df.columns[0:2]])
y_data=np.array(df[df.columns[-1]]).reshape(len(df),1)

为什么归一化?

最优解的寻优过程明显会变得平缓,更容易正确的收敛到最优解。

搭建神经网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tensorflow as tf
alpha = 0.01 # 学习率 alpha
epoch = 500 # 轮数
X = tf.placeholder(tf.float32, X_data.shape, name='X')
# 输出 y,形状[47, 1]
y = tf.placeholder(tf.float32, y_data.shape, name='y')
#W = tf.get_variable("weights",(X_data.shape[1], 1),initializer=tf.constant_initializer())
W=tf.Variable(tf.random_normal([2,1]))
b=tf.Variable(tf.random_normal([1]))
# y_pred 形状[47,1]
y_pred = tf.matmul(X, W, name='y_pred')+b
# tf.matmul(a,b,transpose_a=True) 表示:矩阵a的转置乘矩阵b,即 [1,47] X [47,1]
loss_op = 1 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
train_op = tf.train.GradientDescentOptimizer(learning_rate=alpha).minimize(loss_op)

全部代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import pandas as pd
import numpy as np
#归一化处理
def normalize_feature(df):
return df.apply(lambda column:(column-column.mean())/column.std())
df=pd.read_csv("data1.csv",names=["square","bedrooms","price"])
df=normalize_feature(df)
print(df.head())
X_data=np.array(df[df.columns[0:2]])
y_data=np.array(df[df.columns[-1]]).reshape(len(df),1)

import tensorflow as tf
alpha = 0.01 # 学习率 alpha
epoch = 500 # 轮数
X = tf.placeholder(tf.float32, X_data.shape, name='X')
# 输出 y,形状[47, 1]
y = tf.placeholder(tf.float32, y_data.shape, name='y')
#W = tf.get_variable("weights",(X_data.shape[1], 1),initializer=tf.constant_initializer())
W=tf.Variable(tf.random_normal([2,1]))
b=tf.Variable(tf.random_normal([1]))
# y_pred 形状[47,1]
y_pred = tf.matmul(X, W, name='y_pred')+b
# tf.matmul(a,b,transpose_a=True) 表示:矩阵a的转置乘矩阵b,即 [1,47] X [47,1]
loss_op = 1 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
train_op = tf.train.GradientDescentOptimizer(learning_rate=alpha).minimize(loss_op)
with tf.Session() as sess:
# 初始化全局变量
sess.run(tf.global_variables_initializer())
# 记录所有损失值
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"
print(log_str % (e, loss))
----本文结束,感谢您的阅读。如有错,请指正。----
大哥大嫂过年好!支持我一下呗
0%