使用LightGBM构建回归模型并利用网格搜素寻找最优超参数,给出代码
时间: 2024-11-09 18:18:32 浏览: 20
波士顿房价预测 交叉验证:寻找最优超参数.rar
在Python中,可以使用`lightgbm`库和`sklearn.model_selection`模块中的`GridSearchCV`来构建回归模型并找到最佳超参数。下面是一个简单的示例,假设我们有一个名为`data`的训练数据集,目标变量为`target`,特征列包含`features`:
```python
# 导入所需库
import lightgbm as lgb
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import LabelEncoder
# 数据预处理(如果需要)
le = LabelEncoder()
if isinstance(data['your_target_column'], str):
data['your_target_column'] = le.fit_transform(data['your_target_column'])
# 划分特征和目标变量
X = data[features]
y = data[target]
# 定义LGBMRegressor模型
model_lgb = lgb.LGBMRegressor()
# 定义超参数搜索空间
param_grid = {
'n_estimators': [50, 100, 200],
'learning_rate': [0.01, 0.1, 0.3],
'max_depth': [3, 5, 7],
'colsample_bytree': [0.5, 0.7, 1.0]
}
# 创建GridSearchCV实例
grid_search = GridSearchCV(model_lgb, param_grid, cv=5, scoring='neg_mean_squared_error', return_train_score=True)
# 训练模型并找到最优参数
grid_search.fit(X, y)
# 打印最优参数和对应的交叉验证分数
best_params = grid_search.best_params_
best_score = grid_search.best_score_
print(f"Best parameters: {best_params}")
print(f"Best cross-validation score: {-best_score}")
# 使用最优参数对测试数据进行预测
test_data = ... # 加载你的测试数据
predictions = grid_search.predict(test_data)
# 结果评估
mse = mean_squared_error(y_test, predictions)
print(f"Mean Squared Error on test set: {mse}")
阅读全文