基于决策树算法对房价预测包含数据清洗等完整过程(包含数据集和代码)
时间: 2024-01-12 18:02:28 浏览: 64
好的,以下是基于决策树算法对房价预测的完整流程,包括数据清洗和特征工程。
首先,我们需要准备数据集。此处使用的是scikit-learn库自带的波士顿房价数据集。
```python
from sklearn.datasets import load_boston
import pandas as pd
data = load_boston()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['price'] = data.target
```
接着,我们进行数据清洗,查看数据是否存在缺失值和异常值。
```python
# 查看数据是否存在缺失值
print(df.isnull().sum())
# 查看数据是否存在异常值
for feature in df.columns:
q1 = df[feature].quantile(0.25)
q3 = df[feature].quantile(0.75)
iqr = q3 - q1
outlier_min = q1 - 1.5 * iqr
outlier_max = q3 + 1.5 * iqr
outliers = df[(df[feature] < outlier_min) | (df[feature] > outlier_max)]
print(f"{feature}: {outliers.shape[0]}")
```
经过查看,我们发现数据集中不存在缺失值和异常值。
接着,我们进行特征工程,从数据集中选择合适的特征,并进行特征缩放。
```python
from sklearn.preprocessing import StandardScaler
# 选择合适的特征
X = df[['RM', 'LSTAT', 'PTRATIO']]
# 进行特征缩放
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
y = df['price']
```
最后,我们使用sklearn库中的DecisionTreeRegressor类建立决策树模型,并训练模型:
```python
from sklearn.tree import DecisionTreeRegressor
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=42)
# 建立决策树模型并训练
model = DecisionTreeRegressor(max_depth=5)
model.fit(X_train, y_train)
```
最后,我们使用测试集进行模型评估:
```python
from sklearn.metrics import mean_squared_error
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("MSE:", mse)
```
完整代码如下:
```python
from sklearn.datasets import load_boston
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 准备数据集
data = load_boston()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['price'] = data.target
# 数据清洗
print(df.isnull().sum())
for feature in df.columns:
q1 = df[feature].quantile(0.25)
q3 = df[feature].quantile(0.75)
iqr = q3 - q1
outlier_min = q1 - 1.5 * iqr
outlier_max = q3 + 1.5 * iqr
outliers = df[(df[feature] < outlier_min) | (df[feature] > outlier_max)]
print(f"{feature}: {outliers.shape[0]}")
# 特征工程
X = df[['RM', 'LSTAT', 'PTRATIO']]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
y = df['price']
# 建立决策树模型并训练
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
model = DecisionTreeRegressor(max_depth=5)
model.fit(X_train, y_train)
# 模型评估
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("MSE:", mse)
```
希望能对你有所帮助!
阅读全文