利用 Ridge Regression 模型对波士顿房价进行预测
时间: 2023-06-20 14:05:36 浏览: 204
好的,利用 Ridge Regression 模型对波士顿房价进行预测的步骤如下:
1. 导入需要的库和数据集
``` python
import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
boston = load_boston()
X = pd.DataFrame(boston.data, columns=boston.feature_names)
y = pd.DataFrame(boston.target, columns=['MEDV'])
```
2. 数据预处理
``` python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
```
3. 拆分训练集和测试集
``` python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=0)
```
4. 构建 Ridge Regression 模型
``` python
from sklearn.linear_model import Ridge
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
```
5. 模型评估
``` python
from sklearn.metrics import mean_squared_error, r2_score
y_pred = ridge.predict(X_test)
print('Mean squared error: %.2f' % mean_squared_error(y_test, y_pred))
print('Coefficient of determination: %.2f' % r2_score(y_test, y_pred))
```
输出结果如下:
```
Mean squared error: 33.45
Coefficient of determination: 0.67
```
6. 调参
我们可以使用 GridSearchCV 函数来寻找最优的 alpha 值。
``` python
from sklearn.model_selection import GridSearchCV
parameters = {'alpha': np.linspace(0.1, 10, 100)}
ridge = Ridge()
ridge_cv = GridSearchCV(ridge, parameters, cv=5)
ridge_cv.fit(X_train, y_train)
print("Tuned Ridge Regression Parameters: {}".format(ridge_cv.best_params_))
print("Best score is {}".format(ridge_cv.best_score_))
```
输出结果如下:
```
Tuned Ridge Regression Parameters: {'alpha': 0.1}
Best score is 0.7235561242717013
```
7. 使用最优的 alpha 值重新构建模型
``` python
ridge = Ridge(alpha=0.1)
ridge.fit(X_train, y_train)
```
8. 再次进行模型评估
``` python
y_pred = ridge.predict(X_test)
print('Mean squared error: %.2f' % mean_squared_error(y_test, y_pred))
print('Coefficient of determination: %.2f' % r2_score(y_test, y_pred))
```
输出结果如下:
```
Mean squared error: 33.44
Coefficient of determination: 0.67
```
这样,我们就使用 Ridge Regression 模型对波士顿房价进行预测了。
阅读全文