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 ]