利用python numpy库手动实现多变量线性回归算法

import numpy as np

# 特征矩阵(包含两个特征)
X = np.array([[1, 2],
        [2, 3],
        [3, 4],
        [4, 5]])
# 目标变量
y = np.array([5, 7, 9, 11])

# 2. 添加截距项
X_b = np.c_[np.ones((X.shape[0], 1)), X]  # 将常数项设置为1并添加到特征矩阵前

# 3. 定义模型参数
class LinearRegression:
    def __init__(self, learning_rate=0.01, n_iterations=1000):
        self.learning_rate = learning_rate
        self.n_iterations = n_iterations
        self.theta = None

    def fit(self, X, y):
        m = len(y)
        self.theta = np.random.randn(X.shape[1])  # 随机初始化权重
        # 梯度下降算法
        for iteration in range(self.n_iterations):
           predictions = X.dot(self.theta)  # 预测值
           gradients = self.theta - self.learning_rate * (1/m) * X.T.dot(predictions - y)  # 计算梯度
           self.theta =  gradients # 更新权重


    def predict(self, X):
        return X.dot(self.theta)  # 进行预测

# 创建并训练模型
model = LinearRegression(learning_rate=0.01, n_iterations=1000)
model.fit(X_b, y)

# 进行预测
predictions = model.predict(X_b)

# 打印结果
print(f"parameters: theta = {model.theta}")
print("预测结果:", predictions)

输出结果:

parameters: theta = [2.13812387 1.46697664 0.61760409]
预测结果: [ 4.8403087   6.92488943  9.00947017 11.0940509 ]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值