Tensorflow 房价预测

原文链接:https://blog.csdn.net/Mind_programmonkey/article/details/89606115
数据处理

import os
import numpy as np
import pandas as pd

# 定义标准化的函数
def normalize_feature(df):
    return df.apply(lambda column:(column-column.mean())/column.std())

# 读取csv
csv_data = normalize_feature(pd.read_csv("F:/NoteBookProject/Demo2.csv")) 
print(csv_data.head())

ones = pd.DataFrame({"ones": np.ones(len(csv_data))})
csv_data = pd.concat([ones,csv_data],axis=1)
print(csv_data.head())

X_data = np.array(csv_data[csv_data.columns[0:3]])
y_data = np.array(csv_data[csv_data.columns[-1]]).reshape(len(csv_data),1)

print(X_data.shape, type(X_data))
print(y_data.shape, type(y_data))

输出

     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
   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
(47, 3) <class 'numpy.ndarray'>
(47, 1) <class 'numpy.ndarray'>

训练

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代表bias
    # 推理值 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),transpose_a=True矩阵a进行转置 表示:矩阵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)
    
with tf.Session() as sess:
    # 初始化全局变量
    sess.run(tf.global_variables_initializer())
    # 开始训练模型
    # 因为训练集较小,所以采用批梯度下降优化算法,每次都使用全量数据训练
    for e in range(1, epoch + 1):
        _, loss, w = sess.run([train_op, loss_op, W], feed_dict={X: X_data, y: y_data})
        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]))

输出

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.103e-10
Epoch 80 	 Loss=0.2018 	 Model: y = 0.4313x1 + 0.1574x2 + -6.985e-10
Epoch 90 	 Loss=0.1917 	 Model: y = 0.4626x1 + 0.1607x2 + -4.936e-10
Epoch 100 	 Loss=0.1835 	 Model: y = 0.4909x1 + 0.1621x2 + -6.147e-10
Epoch 110 	 Loss=0.1769 	 Model: y = 0.5165x1 + 0.162x2 + -7.87e-10
Epoch 120 	 Loss=0.1714 	 Model: y = 0.5397x1 + 0.1606x2 + -5.821e-10
Epoch 130 	 Loss=0.1668 	 Model: y = 0.5609x1 + 0.1581x2 + -9.08e-10
Epoch 140 	 Loss=0.1629 	 Model: y = 0.5802x1 + 0.1549x2 + -9.965e-10
Epoch 150 	 Loss=0.1596 	 Model: y = 0.5979x1 + 0.1509x2 + -9.756e-10
Epoch 160 	 Loss=0.1567 	 Model: y = 0.6142x1 + 0.1465x2 + -4.144e-10
Epoch 170 	 Loss=0.1542 	 Model: y = 0.6292x1 + 0.1416x2 + -1.001e-10
Epoch 180 	 Loss=0.152 	 Model: y = 0.643x1 + 0.1364x2 + -3.236e-10
Epoch 190 	 Loss=0.15 	 Model: y = 0.6559x1 + 0.131x2 + -6.286e-11
Epoch 200 	 Loss=0.1483 	 Model: y = 0.6678x1 + 0.1255x2 + 2.119e-10
Epoch 210 	 Loss=0.1467 	 Model: y = 0.6789x1 + 0.1199x2 + -1.956e-10
Epoch 220 	 Loss=0.1453 	 Model: y = 0.6892x1 + 0.1142x2 + -1.758e-10
Epoch 230 	 Loss=0.144 	 Model: y = 0.6989x1 + 0.1085x2 + -4.307e-11
Epoch 240 	 Loss=0.1429 	 Model: y = 0.708x1 + 0.1029x2 + 3.376e-10
Epoch 250 	 Loss=0.1419 	 Model: y = 0.7165x1 + 0.09736x2 + 2.841e-10
Epoch 260 	 Loss=0.1409 	 Model: y = 0.7245x1 + 0.09189x2 + 3.295e-10
Epoch 270 	 Loss=0.14 	 Model: y = 0.732x1 + 0.08653x2 + -8.033e-11
Epoch 280 	 Loss=0.1393 	 Model: y = 0.7391x1 + 0.08128x2 + 1.141e-10
Epoch 290 	 Loss=0.1385 	 Model: y = 0.7458x1 + 0.07616x2 + 1.321e-10
Epoch 300 	 Loss=0.1379 	 Model: y = 0.7522x1 + 0.07118x2 + 5.087e-10
Epoch 310 	 Loss=0.1373 	 Model: y = 0.7582x1 + 0.06634x2 + 7.398e-10
Epoch 320 	 Loss=0.1367 	 Model: y = 0.7639x1 + 0.06165x2 + 6.845e-10
Epoch 330 	 Loss=0.1362 	 Model: y = 0.7693x1 + 0.0571x2 + 8.423e-10
Epoch 340 	 Loss=0.1358 	 Model: y = 0.7744x1 + 0.0527x2 + 9.252e-10
Epoch 350 	 Loss=0.1353 	 Model: y = 0.7793x1 + 0.04845x2 + 1.104e-09
Epoch 360 	 Loss=0.135 	 Model: y = 0.784x1 + 0.04435x2 + 1.145e-09
Epoch 370 	 Loss=0.1346 	 Model: y = 0.7884x1 + 0.0404x2 + 1.631e-09
Epoch 380 	 Loss=0.1343 	 Model: y = 0.7926x1 + 0.03658x2 + 1.446e-09
Epoch 390 	 Loss=0.134 	 Model: y = 0.7966x1 + 0.03291x2 + 1.429e-09
Epoch 400 	 Loss=0.1337 	 Model: y = 0.8004x1 + 0.02938x2 + 1.694e-09
Epoch 410 	 Loss=0.1335 	 Model: y = 0.8041x1 + 0.02598x2 + 1.697e-09
Epoch 420 	 Loss=0.1332 	 Model: y = 0.8076x1 + 0.02271x2 + 2.125e-09
Epoch 430 	 Loss=0.133 	 Model: y = 0.8109x1 + 0.01957x2 + 2.292e-09
Epoch 440 	 Loss=0.1328 	 Model: y = 0.8141x1 + 0.01655x2 + 2.913e-09
Epoch 450 	 Loss=0.1327 	 Model: y = 0.8171x1 + 0.01366x2 + 3.412e-09
Epoch 460 	 Loss=0.1325 	 Model: y = 0.82x1 + 0.01087x2 + 3.749e-09
Epoch 470 	 Loss=0.1323 	 Model: y = 0.8228x1 + 0.008204x2 + 3.499e-09
Epoch 480 	 Loss=0.1322 	 Model: y = 0.8254x1 + 0.005641x2 + 3.663e-09
Epoch 490 	 Loss=0.1321 	 Model: y = 0.828x1 + 0.003183x2 + 4.2e-09
Epoch 500 	 Loss=0.132 	 Model: y = 0.8304x1 + 0.0008239x2 + 4.138e-09
Tensorflow 房价预测Tensorflow 房价预测 取名为猫的狗 发布了71 篇原创文章 · 获赞 7 · 访问量 2万+ 私信 关注
上一篇:Keras模型训练的断点续训、早停、效果可视化


下一篇:RNN循环神经网络处理MNSIT手写数字识别