- 单特征线性回归Linear Regression With One Feature
- 多特征线性回归Linear Regression With Multiple Features
线性回归LinearRegression假设函数(Multiple Features):
损失函数(平方差损失函数MSE):
优化器(梯度下降Gradient descent):
"""
@Title: linear_regression_with_multiple_features
@Time: 2024/2/22
@Author: Michael Jie
"""
import random
import numpy as np
from sklearn.linear_model import LinearRegression
print("----------手动实现LinearRegression----------")
# 数据集,h = 1.5 * x1 + 2.5 * x2 + 1.7
x1 = [2, 4.3, 2.7, 9, 0.5, 17, 26, 14.9, 20, 24.8]
x2 = [36.7, 11, 72.8, 21.3, 53.8, 91.1, 43, 64.4, 22, 56.9]
x = []
for inx, item in enumerate(x1):
x.append([item, x2[inx]])
y = [96.45, 35.65, 187.75, 68.45, 136.95, 254.95, 148.2, 185.05, 86.7, 181.15]
# 初始化训练参数
w1, w2, b = random.random(), random.random(), random.random()
print("初始化训练参数为:({w1}, {w2}, {b})".format(w1=w1, w2=w2, b=b))
x = np.array(x)
y = np.array(y)
m = len(x)
# 训练精度
epsilon = 1e-5
# 学习率
alpha = 67e-5
# 最大训练轮次
max_iter = 50000
num = 0
while True:
# 假设函数
h = w1 * x.T[0] + w2 * x.T[1] + b
# 损失
j = 1 / (2 * m) * np.sum((h - y) ** 2)
if j < epsilon or num > max_iter:
break
# 梯度下降
w1 -= alpha * (1 / m * np.sum((h - y) * x.T[0]))
w2 -= alpha * (1 / m * np.sum((h - y) * x.T[1]))
b -= alpha * (1 / m * np.sum(h - y))
num += 1
if num % 10000 == 0:
print("第{num}次训练,损失为:{j}".format(num=num, j=j))
print("训练后参数为:({w1}, {w2}, {b})".format(w1=w1, w2=w2, b=b))
print("输入(10, 20)的预测值为:{y}".format(y=w1 * 10 + w2 * 20 + b))
print("----------使用sklearn实现LinearRegression----------")
linear = LinearRegression()
linear.fit(x, y)
print("训练后参数为:({w}, {b})".format(w=linear.coef_, b=linear.intercept_))
print("输入(10, 20)的预测值为:{y}".format(y=linear.predict(np.array([[10, 20]]))))
"""
----------手动实现LinearRegression----------
初始化训练参数为:(0.530463331033288, 0.707705839880781, 0.28382978140974435)
第10000次训练,损失为:0.017031041255342046
第20000次训练,损失为:0.0018574900825865815
第30000次训练,损失为:0.0002025871087491673
第40000次训练,损失为:2.209515787784968e-05
训练后参数为:(1.500215312792436, 2.5001388410844028, 1.6890052437557956)
输入(10, 20)的预测值为:66.69393519336822
----------使用sklearn实现LinearRegression----------
训练后参数为:([1.5 2.5], 1.700000000000017)
输入(10, 20)的预测值为:[66.7]
"""