<jupyter_start><jupyter_text>多元线性回归 波士顿房价问题<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
# 读取csv文件库
import pandas as pd
import tensorflow as tf
# 数据洗牌
from sklearn.utils import shuffle<jupyter_output><empty_output><jupyter_text>数据处理 |       |                       |         |                  ||:-----:|:---------------------:|:-------:|:----------------:|| CRIM  |        城镇人均犯罪率        |   AGE   | 1940年之前建成的自用房屋比例 ||  ZN   | 住宅用地超过25000 sq.ft 的比例 |   DIS   | 到波士顿5个中心区域的加权距离  || INDUS |     城镇非零售商用土地的比例      |   RAD   |    辐射性公路的靠近指数    || CHAS  |     边界是河流为1，否则为0      |   TAX   | 每10000美元的全值财产税率  ||  NOX  |        一氧化氮浓度         | PTRATIO |      城镇师生比例      ||  RM   |        住宅平均房间数        |  LSTAT  |   人口中地位低下者的比例    ||       |                       |  MEDV   | 自主房的平均房价，单位：千美元  |<jupyter_code># 读取数据文件
dataFile = pd.read_csv("Data/boston.csv", header=0)

# 显示数据摘要信息
print(dataFile.describe())
# 显示所有数据
print(dataFile)<jupyter_output>CRIM   ZN   INDUS   CHAS    NOX     RM   AGE     DIS  RAD  TAX  \
0    0.00632  18.0    2.31     0  0.538  6.575  65.2  4.0900    1  296   
1    0.02731   0.0    7.07     0  0.469  6.421  78.9  4.9671    2  242   
2    0.02729   0.0    7.07     0  0.469  7.185  61.1  4.9671    2  242   
3    0.03237   0.0    2.18     0  0.458  6.998  45.8  6.0622    3  222   
4    0.06905   0.0    2.18     0  0.458  7.147  54.2  6.0622    3  222   
..       ...   ...     ...   ...    ...    ...   ...     ...  ...  ...   
501  0.06263   0.0   11.93     0  0.573  6.593  69.1  2.4786    1  273   
502  0.04527   0.0   11.93     0  0.573  6.120  76.7  2.2875    1  273   
503  0.06076   0.0   11.93     0  0.573  6.976  91.0  2.1675    1  273   
504  0.10959   0.0   11.93     0  0.573  6.794  89.3  2.3889    1  273   
505  0.04741   0.0   11.93     0  0.573  6.030  80.8  2.5050    1  273   

     PTRATIO  LSTAT  MEDV  
0       15.3   4.98  24.0  
1       17.8   9.14  21.6  
2       17.8   4.03  34.7  [...]<jupyter_text>数据准备<jupyter_code># 获取df的值
dataSets = dataFile.values

print(dataSets)
# 把df转换为np的数组格式
# 输出结果看似与上面语句一样，但实际数据类型已被转化为适合进行numpy处理的数据类型
dataSets = np.array(dataFile)

print(dataSets)<jupyter_output>[[6.3200e-03 1.8000e+01 2.3100e+00 ... 1.5300e+01 4.9800e+00 2.4000e+01]
 [2.7310e-02 0.0000e+00 7.0700e+00 ... 1.7800e+01 9.1400e+00 2.1600e+01]
 [2.7290e-02 0.0000e+00 7.0700e+00 ... 1.7800e+01 4.0300e+00 3.4700e+01]
 ...
 [6.0760e-02 0.0000e+00 1.1930e+01 ... 2.1000e+01 5.6400e+00 2.3900e+01]
 [1.0959e-01 0.0000e+00 1.1930e+01 ... 2.1000e+01 6.4800e+00 2.2000e+01]
 [4.7410e-02 0.0000e+00 1.1930e+01 ... 2.1000e+01 7.8800e+00 1.1900e+01]]<jupyter_text>特征数据归一化<jupyter_code># 如有以下例子：
# 制作一份鸡汤，需要用到鸡、水、姜、葱、蒜、盐等材料，但材料间的取值范围均不相同
# 水的取值范围可能在2000克到3000克，但相比较之下盐可能仅需要1克2克，这样的取值范围在多元线性回归中是不合理的
#
# 为防止不同特征值取值范围之间的差异性，需要对特征数据进行归一化
# 归一化过程：[特征值 / max(特征值) - min(特征值)]

# 对特征数据【0到11】列进行归一化（缩小差异区间至0~1）处理
for i in range(12):
    dataSets[:, i] = dataSets[:, i] / (dataSets[:, i].max() - dataSets[:, i].min())

# x_data 为前12列特征数据
x_data = dataSets[:, :12]  # [:, :12]表示从0~11列

# y_data 为最后1列标签数据
y_data = dataSets[:, 12]  # [:, 12]表示第12列
print(x_data, "\n shape =", x_data.shape)
print(y_data, "\n shape =", y_data.shape)<jupyter_output>[24.  21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 15.  18.9 21.7 20.4
 18.2 19.9 23.1 17.5 20.2 18.2 13.6 19.6 15.2 14.5 15.6 13.9 16.6 14.8
 18.4 21.  12.7 14.5 13.2 13.1 13.5 18.9 20.  21.  24.7 30.8 34.9 26.6
 25.3 24.7 21.2 19.3 20.  16.6 14.4 19.4 19.7 20.5 25.  23.4 18.9 35.4
 24.7 31.6 23.3 19.6 18.7 16.  22.2 25.  33.  23.5 19.4 22.  17.4 20.9
 24.2 21.7 22.8 23.4 24.1 21.4 20.  20.8 21.2 20.3 28.  23.9 24.8 22.9
 23.9 26.6 22.5 22.2 23.6 28.7 22.6 22.  22.9 25.  20.6 28.4 21.4 38.7
 43.8 33.2 27.5 26.5 18.6 19.3 20.1 19.5 19.5 20.4 19.8 19.4 21.7 22.8
 18.8 18.7 18.5 18.3 21.2 19.2 20.4 19.3 22.  20.3 20.5 17.3 18.8 21.4
 15.7 16.2 18.  14.3 19.2 19.6 23.  18.4 15.6 18.1 17.4 17.1 13.3 17.8
 14.  14.4 13.4 15.6 11.8 13.8 15.6 14.6 17.8 15.4 21.5 19.6 15.3 19.4
 17.  15.6 13.1 41.3 24.3 23.3 27.  50.  50.  50.  22.7 25.  50.  23.8
 23.8 22.3 17.4 19.1 23.1 23.6 22.6 29.4 23.2 24.6 29.9 37.2 39.8 36.2
 37.9 32.5 26.4 29.6 50.  32.  29.8 34.9 37.  30.5 36.4 31.1 29.1 50.
 33.3 3[...]<jupyter_text>定义模型<jupyter_code># 定义特征数据和标签数据的占位符

# shape 中的 None 表示行的数量未知，在实际训练中决定一次带入多少行样本，从一个样本随机的随机SGD到批量SGD都可以

# 12个特征数值（12列）
x = tf.placeholder(tf.float32, [None, 12], name="X")

# 1个标签数据（1列）
y = tf.placeholder(tf.float32, [None, 1], name="Y")
# 创建变量、定义模型
# 定义一个命名空间
with tf.name_scope("Model"):
    # w 初始值为 shape = （12，1）的随机数
    w = tf.Variable(tf.random_normal([12, 1], stddev=0.01), name="W")

    # b 初始值为1.0
    b = tf.Variable(1.0, name="b")


    # Y = (x1 * w1 * x) + (x2 * w2 * x) + (x3 * w3 * x) + ... + (xn * w * nx) + b
    # w和x 是矩阵相乘，用 matmul，不能用 普通乘法multiple 或者 乘法符号“*”
    # [[x1, x2, x3 ... xn]] * [[w1], + b = [x1 * w1 + x2 * w2 + x3 * w3 + ... xn * wn] + b
    #                          [w2],
    #                          [w3],
    #                          ....,
    #                          [wn]]
    def model(x, w, b):
        return tf.matmul(x, w) + b


    # 预测计算操作，前向计算节点
    pred = model(x, w, b)<jupyter_output><empty_output><jupyter_text>模型训练准备<jupyter_code># 设置超参数
train_epochs = 200

# 学习率
learning_rate = 0.01
# 定义均方差损失函数
with tf.name_scope("LossFunction"):
    # 均方误差
    loss_function = tf.reduce_mean(tf.pow(y - pred, 2))
# 选择优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)
# 定义会话
session = tf.Session()

# 初始化变量
init = tf.global_variables_initializer()

session.run(init)<jupyter_output><empty_output><jupyter_text>TensorBoard 可视化准备<jupyter_code># 为TensorBoard 准备数据
logdir = "Log/2-Multiple-Linear-Regression/"

# 创建一个操作，记录损失值loss，后面在TensorBoard中Scales栏可见
sum_loss_op = tf.summary.scalar("loss", loss_function)

# 把所有需要记录摘要日志文件合并，方便一次性写入
merged = tf.summary.merge_all()

# 创建摘要writer，将计算图写入摘要文件，后面在TensorBoard中Graph栏可见
writer = tf.summary.FileWriter(logdir, session.graph)<jupyter_output><empty_output><jupyter_text>模型训练<jupyter_code># 用于保存 loss 值的列表
loss_list = []

for epoch in range(train_epochs):
    loss_sum = 0.0
    for xs, ys in zip(x_data, y_data):
        # Feed数据必须和Placeholder的shape一致
        xs = xs.reshape(1, 12)
        ys = ys.reshape(1, 1)

        # 下划线 _ 指接收结果，本例中暂且用不到该值，代表优化器返回值
        _, summary_str, loss = session.run([optimizer, sum_loss_op, loss_function], feed_dict={x: xs, y: ys})

        writer.add_summary(summary_str, epoch)
        loss_sum = loss_sum + loss

    # 每次训练都打乱数据，防止假学习（只记顺序不真正学习）
    xvalues, yvalues = shuffle(x_data, y_data)

    b0temp = b.eval(session=session)
    w0temp = w.eval(session=session)

    loss_average = loss_sum / len(y_data)

    loss_list.append(loss_average)

    print("Epoch = ", epoch + 1, "loss = ", loss_average, "b = ", b0temp, "w = ", w0temp)

    plt.plot(loss_list)<jupyter_output>Epoch =  1 loss =  44.33288477397004 b =  3.6080515 w =  [[-0.6244204 ]
 [ 1.3728793 ]
 [-0.7885526 ]
 [ 0.5028598 ]
 [ 2.526639  ]
 [ 7.163674  ]
 [-0.05246669]
 [ 0.79951257]
 [ 0.3668376 ]
 [ 0.3302319 ]
 [ 2.316032  ]
 [-4.4112563 ]]
Epoch =  2 loss =  32.03669964120209 b =  3.9965024 w =  [[-1.1736755 ]
 [ 1.956714  ]
 [-1.5183268 ]
 [ 0.85548747]
 [ 2.9007814 ]
 [10.601796  ]
 [-0.81918085]
 [ 0.36003157]
 [ 0.61607265]
 [-0.26048142]
 [ 1.1530914 ]
 [-8.126037  ]]
Epoch =  3 loss =  27.340508418493283 b =  4.2777243 w =  [[ -1.6711925 ]
 [  2.174651  ]
 [ -1.968007  ]
 [  1.0639511 ]
 [  3.1802802 ]
 [ 13.198544  ]
 [ -1.1951933 ]
 [ -0.17257729]
 [  0.9231658 ]
 [ -0.76126105]
 [  0.22155768]
 [-10.980836  ]]
Epoch =  4 loss =  24.67355934962585 b =  4.49927 w =  [[ -2.1281948 ]
 [  2.2017665 ]
 [ -2.2445512 ]
 [  1.157981  ]
 [  3.3628757 ]
 [ 15.189049  ]
 [ -1.3569542 ]
 [ -0.71638983]
 [  1.2479167 ]
 [ -1.192887  ]
 [ -0.51228255]
 [-13.200746  ]]
Epoch =  5 loss =  23.106[...]<jupyter_text>使用TensorBoard查看损失值```bashtensorboard --logdir="Log/"```  利用模型进行预测<jupyter_code># 由于本例中将所有数据都作为训练集进行训练，此处暂无新数据可供预测，故随机抽取一条训练集中的数据进行预测
n = np.random.randint(506)
print(n)

x_test = x_data[n]

x_test = x_test.reshape(1, 12)
predict = session.run(pred, feed_dict={x: x_test})
print("预测值%f" % predict)

target = y_data[n]
print("目标值%f" % target)<jupyter_output>419
预测值13.284087
目标值8.400000
{'fraction_non_alphanumeric': 0.16462897954242828, 'fraction_numerical': 0.22043336157850138, 'mean_word_length': 2.1973684210526314, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '132656', 'license': 'MIT', 'n_tokens_mistral': 5442, 'n_tokens_neox': 4618, 'n_words': 766, 'path': 'Course/1.0/Example/2-Multiple-Linear-Regression.ipynb', 'repo_name': 'PhoenixNest/Hello-Tensorflow', 'size': '8261'}