lasso回归与岭回归实现波斯顿房价预测代码
时间: 2024-10-13 17:16:26 浏览: 35
基于线性回归实现波士顿房价预测.zip
LASSO回归(Least Absolute Shrinkage and Selection Operator)和岭回归(Ridge Regression)都是用于解决线性回归模型过拟合问题的正则化方法。它们通过添加一个惩罚项来控制模型复杂度。下面是使用Python中的sklearn库,通过scikit-learn示例来实现这两种方法在波士顿房价预测的数据集上的简单代码:
首先,确保已经安装了`sklearn`库,如果未安装,可以使用`pip install scikit-learn`。
```python
# 导入所需库
import numpy as np
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso, Ridge
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
# 加载波士顿房价数据
boston = load_boston()
X = boston.data
y = boston.target
# 数据预处理,标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# LASSO回归实例并拟合
lasso_reg = Lasso(alpha=0.1) # 设置LASSO的正则化强度
lasso_reg.fit(X_train, y_train)
y_pred_lasso = lasso_reg.predict(X_test)
lasso_rmse = np.sqrt(mean_squared_error(y_test, y_pred_lasso))
print("LASSO RMSE:", lasso_rmse)
# 岭回归实例并拟合
ridge_reg = Ridge(alpha=0.1) # 设置岭回归的正则化强度
ridge_reg.fit(X_train, y_train)
y_pred_ridge = ridge_reg.predict(X_test)
ridge_rmse = np.sqrt(mean_squared_error(y_test, y_pred_ridge))
print("Ridge RMSE:", ridge_rmse)
阅读全文