Python TensorFlow 实现多元线性回归算法

Python TensorFlow实现多元线性回归

公式为:y = w1 * x1 + w2 * x2 + w3 * x3 + b

在数学中主要求解各个系数(w1,w2,w3)和偏置b

可转化为:y = (w0 * x0 + b0) + (w1 * x1 + b1) + (w2 * x2 + w3 * x3 + b3) + ......,其中x0=1,w0=0

转化后需要求解:[w0, w1, w2, w3, ......],[b0, b1, b2, b3, ......]

具体实现:

# encoding = utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 加载数据
def load_data(path):
    with open(path, "r", encoding="utf-8") as f:
        lines = f.readlines()
        x_data = []
        y_data = []
        for line in lines:
            data_list = line.split(",")
            d = [float(data.strip()) for data in data_list]
            x_data.append(d[:-1])
            y_data.append(d[-1:])
        print(x_data)
    return x_data, y_data


# 数据归一化
def normalize(X):
    mean = np.mean(X)
    std = np.std(X)
    X = (X - mean)/std
    return X


# 固定输入值将权重和偏置结合起来
def append_bias_reshape(features, labels):
    if str(type(features)) == "<class 'list'>":
        features = np.array(features)
    if str(type(labels)) == "<class 'list'>":
        labels = np.array(labels)
    # features是矩阵,features.shape[0]是获取矩阵的行数, features.shape[1]是矩阵的列数
    m = features.shape[0]
    n = features.shape[1]
    x = np.reshape(np.c_[np.ones(m), features], [m, n + 1])
    y = np.reshape(labels, [m, 1])
    return x, y

X_train, Y_train = load_data("data/test.txt")
n = len(X_train[0]) + 1
# X_train = normalize(X_train)
X_train, Y_train = append_bias_reshape(X_train, Y_train)
m = len(X_train)


# 为训练数据声明占位符
X = tf.compat.v1.placeholder(tf.float32, name="X", shape=[m, n])
Y = tf.compat.v1.placeholder(tf.float32, name="Y")
# 初始化权重
w = tf.Variable(tf.random.normal([n, 1]))
b = tf.Variable(tf.random.normal([n, 1]))
# 定义线性回归模型,现在用矩阵乘法来完成
Y_hat = tf.matmul(X, w)
# 定义损失函数
loss = tf.reduce_mean(tf.square(Y - Y_hat), name="loss")
# 定义优化器(学习率learning_rate可自定义为其它值)
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=0.0005).minimize(loss)
# 定义初始化操作符
init_op = tf.compat.v1.global_variables_initializer()
total_loss = []
w_ = []
b_ = []
min_loss = 0
# 开始计算图
with tf.compat.v1.Session() as sess:
    sess.run(init_op)
    tf.compat.v1.summary.FileWriter("graphs", sess.graph)
    # 迭代次数可以自定义为其它值
    for step in range(10000):
        _, my_loss = sess.run([optimizer, loss], feed_dict={X: X_train, Y: Y_train})
        total_loss.append(my_loss)
        w_value, b_value = sess.run([w, b])
        if step == 0:
            min_loss = my_loss
        if my_loss < min_loss:
            min_loss = my_loss
            w_ = w_value
            b_ = b_value
        if step % 100 == 0:
            print("Epoch {}: Loss {}".format(step, my_loss))
print("total_loss: {}".format(total_loss))
print("min_loss: {}".format(min_loss))
print("权重,w_: {}".format([d[0] for d in w_]))
r = 0
for d in b_:
    r += d[0]
print("偏置,b_: {}".format(r))

 data/test.txt文件的数据格式是:

0.00632,18.0,2.31,0.0,0.538,6.575,65.2,4.09,1.0,296.0,15.3,396.9,4.98,24.0
0.02731,0.0,7.07,0.0,0.469,6.421,78.9,4.9671,2.0,242.0,17.8,396.9,9.14,21.6
0.02729,0.0,7.07,0.0,0.469,7.185,61.1,4.9671,2.0,242.0,17.8,392.83,4.03,34.7
0.03237,0.0,2.18,0.0,0.458,6.998,45.8,6.0622,3.0,222.0,18.7,394.63,2.94,33.4
0.06905,0.0,2.18,0.0,0.458,7.147,54.2,6.0622,3.0,222.0,18.7,396.9,5.33,36.2
0.02985,0.0,2.18,0.0,0.458,6.43,58.7,6.0622,3.0,222.0,18.7,394.12,5.21,28.7
0.08829,12.5,7.87,0.0,0.524,6.012,66.6,5.5605,5.0,311.0,15.2,395.6,12.43,22.9
0.14455,12.5,7.87,0.0,0.524,6.172,96.1,5.9505,5.0,311.0,15.2,396.9,19.15,27.1
0.21124,12.5,7.87,0.0,0.524,5.631,100.0,6.0821,5.0,311.0,15.2,386.63,29.93,16.5
0.17004,12.5,7.87,0.0,0.524,6.004,85.9,6.5921,5.0,311.0,15.2,386.71,17.1,18.9
0.22489,12.5,7.87,0.0,0.524,6.377,94.3,6.3467,5.0,311.0,15.2,392.52,20.45,15.0
0.11747,12.5,7.87,0.0,0.524,6.009,82.9,6.2267,5.0,311.0,15.2,396.9,13.27,18.9
0.09378,12.5,7.87,0.0,0.524,5.889,39.0,5.4509,5.0,311.0,15.2,390.5,15.71,21.7
0.62976,0.0,8.14,0.0,0.538,5.949,61.8,4.7075,4.0,307.0,21.0,396.9,8.26,20.4
0.63796,0.0,8.14,0.0,0.538,6.096,84.5,4.4619,4.0,307.0,21.0,380.02,10.26,18.2
0.62739,0.0,8.14,0.0,0.538,5.834,56.5,4.4986,4.0,307.0,21.0,395.62,8.47,19.9
1.05393,0.0,8.14,0.0,0.538,5.935,29.3,4.4986,4.0,307.0,21.0,386.85,6.58,23.1
0.7842,0.0,8.14,0.0,0.538,5.99,81.7,4.2579,4.0,307.0,21.0,386.75,14.67,17.5
0.80271,0.0,8.14,0.0,0.538,5.456,36.6,3.7965,4.0,307.0,21.0,288.99,11.69,20.2
0.7258,0.0,8.14,0.0,0.538,5.727,69.5,3.7965,4.0,307.0,21.0,390.95,11.28,18.2
1.25179,0.0,8.14,0.0,0.538,5.57,98.1,3.7979,4.0,307.0,21.0,376.57,21.02,13.6
0.85204,0.0,8.14,0.0,0.538,5.965,89.2,4.0123,4.0,307.0,21.0,392.53,13.83,19.6
1.23247,0.0,8.14,0.0,0.538,6.142,91.7,3.9769,4.0,307.0,21.0,396.9,18.72,15.2
0.98843,0.0,8.14,0.0,0.538,5.813,100.0,4.0952,4.0,307.0,21.0,394.54,19.88,14.5
0.75026,0.0,8.14,0.0,0.538,5.924,94.1,4.3996,4.0,307.0,21.0,394.33,16.3,15.6

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值