1、请根据选择任一梯度下降算法(批量梯度下降、随机梯度下降或小批量梯度下降)求解多元线性回归的方程,并计算1、请根据选择任一梯度下降算法(批量梯度下降、随机梯度下降或小批量梯度下降)求解多元线性回归的方程,并计算相关系数,手动导入数据
时间: 2024-11-29 17:34:44 浏览: 27
在Python中,我们可以使用梯度下降算法来求解多元线性回归问题。这里我会分别介绍三种梯度下降方法如何应用到这个问题上:
1. 批量梯度下降 (Batch Gradient Descent):
这种方法在每次迭代时都会用整个训练集的数据来更新模型参数。对于每个训练样本,我们需要计算损失函数对所有权重的偏导数,然后取平均值进行权重更新。
```python
import numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
# 加载波士顿房价数据
boston = load_boston()
X, y = boston.data, boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 初始化权重
w = np.zeros(X.shape[1])
# 设置学习率和最大迭代次数
learning_rate = 0.01
max_iterations = 1000
for _ in range(max_iterations):
# 使用批量梯度下降更新权重
w -= learning_rate * np.dot((X_train.T @ (X_train @ w - y_train)), X_train) / X_train.shape[0]
# 建立模型并预测
batch_regression = LinearRegression(fit_intercept=False)
batch_regression.coef_ = w
predictions = batch_regression.predict(X_test)
# 计算R²分数(相关系数平方)
r2_batch = r2_score(y_test, predictions)
```
2. 随机梯度下降 (Stochastic Gradient Descent, SGD):
每次迭代只用一个随机选取的训练样本更新模型。这种方法更快,但可能会在收敛速度和稳定性之间有所妥协。
```python
def sgd(X, y, w, learning_rate, max_iterations):
for _ in range(max_iterations):
random_index = np.random.randint(0, len(X))
gradient = (np.dot((X[random_index] @ w - y[random_index]), X[random_index]))
w -= learning_rate * gradient
return w
# 使用随机梯度下降
w_sgd = sgd(X_train, y_train, w.copy(), learning_rate, max_iterations)
# 其他步骤同上
sgd_regression = LinearRegression(fit_intercept=False)
sgd_regression.coef_ = w_sgd
predictions_sgd = sgd_regression.predict(X_test)
r2_sgd = r2_score(y_test, predictions_sgd)
```
3. 小批量梯度下降 (Mini-batch Gradient Descent):
它结合了批量和随机梯度下降的优点,每次迭代使用一小批随机选取的样本更新权重。
```python
def mini_batch_gradient_descent(X, y, w, batch_size, learning_rate, max_iterations):
for _ in range(max_iterations):
for i in range(0, len(X), batch_size):
gradient = (np.dot((X[i:i+batch_size] @ w - y[i:i+batch_size]).T, X[i:i+batch_size]) / batch_size)
w -= learning_rate * gradient
return w
# 使用小批量梯度下降
batch_size = 10
w_mini_batch = mini_batch_gradient_descent(X_train, y_train, w.copy(), batch_size, learning_rate, max_iterations)
# 其他步骤同上
mini_batch_regression = LinearRegression(fit_intercept=False)
mini_batch_regression.coef_ = w_mini_batch
predictions_mini_batch = mini_batch_regression.predict(X_test)
r2_mini_batch = r2_score(y_test, predictions_mini_batch)
```
阅读全文