分别用最小二乘法和梯度下降法实现线性回归

下面代码中包含了两种方法

import numpy as np

np.random.seed(1234)

x = np.random.rand(500, 3)
# x为数据,500个样本,每个样本三个自变量
y = x.dot(np.array([4.2, 5.7, 10.8]))
# y为标签,每个样本对应一个y值

# 最小二乘法
class LR_LS():
    def __init__(self):
        self.w = None

    def fit(self, X, y):
        self.w = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)

    def predict(self, X):
        y_pred = X.dot(self.w)

        return y_pred

# 梯度下降法
class LR_GradientDescent():
    def __init__(self):
        self.w = None
        self.cnt = 0

    def fit(self, X, y, alpha = 0.02, loss = 1e-10):
        y = y.reshape(-1, 1)
        [m, d] = np.shape(X)
        self.w = np.zeros(d)
        tol = 1e5

        while tol > loss:
            h_f = X.dot(self.w).reshape(-1, 1)
            theta = self.w + alpha * np.mean(X * (y - h_f), axis = 0)
            tol = np.sum(np.abs(theta - self.w))
            self.w = theta
            self.cnt += 1

    def predict(self, X):
        return X.dot(self.w)


if __name__ == '__main__':
    lr_ls = LR_LS()
    lr_ls.fit(x, y)
    print("最小二乘法:")
    print("参数的估计值:%s" %(lr_ls.w))

    x_test = np.array([2, 3, 5]).reshape(1, -1)
    print("预测值为:%s" %(lr_ls.predict(x_test)))
    print()

    lr_gd = LR_GradientDescent()
    lr_gd.fit(x, y)
    print("梯度下降法:")
    print("参数的估计值:%s" % (lr_gd.w))

    x_test = np.array([2, 3, 5]).reshape(1, -1)
    print("预测值为:%s" % (lr_gd.predict(x_test)))
    print("梯度下降迭代次数:", lr_gd.cnt)

运行结果为:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小崔崔谁用的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值