机器学习算法-一元线性回归(最小二乘拟合 and 梯度下降)
先来看看理论知识吧
Python实现
import numpy as np
import sympy as spX = [1, 2, 3, 4, 5]
Y = [1, 3, 6, 7, 6]class LinerRegress:def __init__(self, learn_rate=0.001, gradient_times=100):# 训练特征数据self.train_x = []# 训练标签数据self.train_y = []# 线性回归斜率self.w = 0# 线性回归截距self.b = 0# 学习率self.learn_rate = learn_rate# 梯度下降次数self.gradient_times = gradient_timesdef fit(self, X, Y):self.train_x, self.train_y = np.array(X), np.array(Y)w,b = self.min_twicemultiply(self.train_x,self.train_y)self.w, self.b = self.gradient_descend(self.train_x, self.train_y, w, b, self.gradient_times)# 最小二乘拟合确定 w 和 bdef min_twicemultiply(self,X,Y):x0, x1 = np.ones(len(X)), np.array(X)x0x0, x1x1 = np.dot(x0, x0), np.dot(x1, x1)x1x0 = x0x1 = np.dot(x0, x1)yx0, yx1 = np.dot(self.train_y, x0), np.dot(self.train_y, x1)w = (x1x0 * yx0 - x0x0 * yx1) / (x0x1 * x1x0 - x0x0 * x1x1)b = (x1x1 * yx0 - x0x1 * yx1) / (x1x1 * x0x0 - x1x0 * x0x1)return w,b# 梯度下降调优参数 w 和 bdef gradient_descend(self, x, y, w, b, times):_w, _b = sp.symbols('_w _b')size = len(y)# 损失函数loss_f1 同时对w和b做偏导数求极小值# 这里损失函数loss_f2只对截距w做了"梯度下降"算法调整loss_f1, loss_f2 = 0.0, 0.0for xv, yv in zip(x, y):loss_f1 += (yv - _w * xv - _b) ** 2loss_f2 += (yv - _w * xv - b) ** 2loss_f1, loss_f2 = sp.simplify(loss_f1 / (2 * size)), sp.simplify(loss_f2 / (2 * size))res = sp.solve([sp.diff(loss_f1, _w), sp.diff(loss_f1, _b)], [_w, _b])# loss_f1 的w和b的极值点存在if len(res) != 0:return float(res.get(_w)), float(res.get(_b))else:next_w = wloss_f2_diff = sp.diff(loss_f2, _w)for _ in range(times):next_w -= loss_f2_diff.subs({_w: next_w}) * self.learn_ratereturn next_w, bdef predict(self, data):return self.w * data + self.bmodel = LinerRegress()
model.fit(X, Y)
print(model.predict(6))