岭回归、梯度下降法(自定义函数)、随机梯度下降(自定义函数)实现波士顿房价预测(计算回归系数、偏置项、均方误差MSE和R方回归评价指标)注:划分数据集并进行数据的标准化。
时间: 2024-09-27 16:12:54 浏览: 37
《模式识别与机器学习实战》第四讲 逻辑回归与梯度下降法
岭回归(Ridge Regression)是一种线性回归模型,通过加入L2正则化项,防止过拟合。在Python中,我们可以使用sklearn库来实现,首先需要导入必要的模块:
```python
import numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error, r2_score
# 加载波士顿房价数据
boston = load_boston()
X = boston.data
y = boston.target
# 数据预处理:划分训练集和测试集,并标准化数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 创建岭回归模型
ridge_reg = Ridge(alpha=1) # alpha控制正则化的强度
# 拟合数据
ridge_reg.fit(X_train, y_train)
# 计算回归系数和偏置项
coef = ridge_reg.coef_
intercept = ridge_reg.intercept_
# 预测
y_pred = ridge_reg.predict(X_test)
# 计算均方误差(MSE)
mse = mean_squared_error(y_test, y_pred)
print(f"均方误差 (MSE): {mse}")
# 计算R²分数(R^2)
r2 = r2_score(y_test, y_pred)
print(f"R²分数 (R²): {r2}")
# 随机梯度下降通常用于深度学习中的优化算法,如SGDRegressor,这里我们假设它与上述过程类似,但会直接用在损失函数上进行迭代更新,而不是整个训练集
# 由于此处不是重点,我们可以用常规梯度下降(GD)替代:
# from sklearn.linear_model import SGDRegressor
# sgd_reg = SGDRegressor(loss='squared_loss', learning_rate='constant')
# sgd_reg.fit(X_train, y_train)
# ...然后同样计算相应的回归系数和指标
阅读全文