二手房房价预测网网格搜索代码
时间: 2023-10-11 19:06:46 浏览: 82
以下是一个基于sklearn库的二手房房价预测网格搜索代码示例:
```python
# 导入必要的库和数据集
from sklearn.datasets import load_boston
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
# 加载数据集
boston = load_boston()
X = boston.data
y = boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 定义决策树回归模型和超参数空间
dt = DecisionTreeRegressor(random_state=0)
param_grid = {
'max_depth': [3, 5, 7, 9],
'min_samples_leaf': [1, 2, 4, 6]
}
# 使用网格搜索进行超参数优化
grid_search = GridSearchCV(dt, param_grid=param_grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
# 输出最佳超参数组合和对应的模型性能指标
print("Best parameters: {}".format(grid_search.best_params_))
print("Best cross-validation score: {:.2f}".format(-grid_search.best_score_))
# 使用最佳超参数组合对测试集进行预测,并计算模型性能指标
dt_best = DecisionTreeRegressor(max_depth=grid_search.best_params_['max_depth'],
min_samples_leaf=grid_search.best_params_['min_samples_leaf'],
random_state=0)
dt_best.fit(X_train, y_train)
print("Test set score: {:.2f}".format(dt_best.score(X_test, y_test)))
```
在上述代码中,我们首先通过train_test_split函数将数据集划分为训练集和测试集。然后,我们定义了决策树回归模型,并定义了超参数空间。接下来,我们使用GridSearchCV函数进行网格搜索,并传入模型、超参数空间、交叉验证折数、评分函数等参数。最后,我们输出了最佳超参数组合和对应的模型性能指标,并使用最佳超参数组合对测试集进行预测,计算模型性能指标。
阅读全文