复杂一点,并且提供数据源
时间: 2024-10-28 20:06:15 浏览: 7
让我们使用Python和上述库创建一个更复杂的数据清洗、探索性数据分析(EDA)、特征工程和简单线性回归模型的例子,我们将以UCI Machine Learning Repository的"波士顿房价数据集"为例,该数据集包含了1978年波士顿郊区房屋的各种属性与其销售价格。
首先,我们需要从`sklearn.datasets`导入数据并加载它:
```python
from sklearn.datasets import load_boston
import pandas as pd
import seaborn as sns
import numpy as np
# 加载波士顿房价数据集
boston = load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['PRICE'] = boston.target # 添加目标变量:房屋价格
print(data.head())
```
接下来,我们对数据进行清洗和探索:
```python
# 检查缺失值
null_counts = data.isnull().sum()
print(null_counts)
# 数据标准化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# EDA
sns.pairplot(data_scaled, vars=['RM', 'LSTAT', 'CRIM'], hue='PRICE')
plt.show()
# 特征选择(例如,选择RM和LSTAT作为输入特征)
X = data_scaled[['RM', 'LSTAT']]
y = data_scaled['PRICE']
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
最后,我们构建一个模型并评估性能:
```python
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# 训练模型
model = LinearRegression()
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 评估
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Mean Squared Error: {mse}')
print(f'R-squared Score: {r2}')
阅读全文