Sklearn-Boston Housing Dataset数据集: 包含了 506 个波士顿地区的房屋数据,其中每个数据点都有 13 个变量(例如犯罪率、房产税率、房间数量等)和一个目标变量(房屋价格的中位数)。 仿造课上模板程序diabetes_ridge_lasso_regression(见学习通“课程算法程序-Lasso_岭回归”)。对波士顿房屋数据进行ridge与lasso回归分析。 提示: from sklearn.datasets import load_boston boston = load_boston() X = boston.data y = boston.target
时间: 2024-02-15 13:04:47 浏览: 67
好的,我明白了。以下是对波士顿房屋数据进行ridge与lasso回归分析的程序:
```python
from sklearn.datasets import load_boston
from sklearn.linear_model import Ridge, Lasso
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
# 加载数据集
boston = load_boston()
X = boston.data
y = boston.target
# 数据标准化
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Ridge回归
ridge = Ridge(alpha=0.1)
ridge.fit(X_train, y_train)
ridge_pred = ridge.predict(X_test)
ridge_mse = mean_squared_error(y_test, ridge_pred)
print("Ridge回归的均方误差为:", ridge_mse)
# Lasso回归
lasso = Lasso(alpha=0.1)
lasso.fit(X_train, y_train)
lasso_pred = lasso.predict(X_test)
lasso_mse = mean_squared_error(y_test, lasso_pred)
print("Lasso回归的均方误差为:", lasso_mse)
```
在以上程序中,我们首先加载了波士顿房屋数据集,并将数据标准化。然后,我们将数据集划分为训练集和测试集,并使用Ridge和Lasso回归进行拟合。最后,我们使用均方误差作为评估指标来比较两种回归方法的性能。
阅读全文