randomforestregressor 调参 代码
时间: 2023-09-24 15:14:22 浏览: 79
随机森林代码
可以通过GridSearchCV函数进行调参,具体代码如下:
```
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
# 创建随机森林模型
rf_reg = RandomForestRegressor()
# 设置调参范围
param_grid = {
'n_estimators': [100, 200, 300],
'criterion': ['mse', 'mae'],
'max_depth': [10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# 使用GridSearchCV进行调参
grid_search = GridSearchCV(estimator=rf_reg,
param_grid=param_grid,
cv=5)
# 输入数据并训练模型
grid_search.fit(X_train, y_train)
# 输出最佳参数
print(grid_search.best_params_)
```
阅读全文