UCI波士顿房价预测数据集
时间: 2024-03-06 14:43:12 浏览: 149
以下是对UCI波士顿房价预测数据集的分析和预测的步骤:
1. 数据集准备与理解
```python
from sklearn.datasets import load_boston
import pandas as pd
# 加载数据集
boston = load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['PRICE'] = boston.target
print(data.head())
```
2. 可视化分析
```python
import seaborn as sns
import matplotlib.pyplot as plt
# 绘制房价与各特征的相关性分析图
plt.figure(figsize=(20, 10))
for i, feature in enumerate(boston.feature_names):
plt.subplot(3, 5, i + 1)
sns.scatterplot(x=feature, y='PRICE', data=data)
plt.show()
```
3. 数据集划分
```python
from sklearn.model_selection import train_test_split
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=42)
```
4. 岭回归线性回归模型训练
```python
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error, r2_score
# 训练岭回归模型
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
```
5. 岭回归模型正则参数调优
```python
from sklearn.model_selection import GridSearchCV
# 正则参数调优
param_grid = {'alpha': [0.1, 1.0, 10.0]}
grid_search = GridSearchCV(Ridge(), param_grid, cv=5)
grid_search.fit(X_train, y_train)
print(grid_search.best_params_)
```
6. 测试模型性能
```python
# 测试模型性能
y_pred = ridge.predict(X_test)
rmse = mean_squared_error(y_test, y_pred, squared=False)
r2 = r2_score(y_test, y_pred)
print("RMSE:", rmse)
print("R2 Score:", r2)
```
阅读全文