用Python确定随机森林回归的最佳参数取值
时间: 2023-11-05 16:57:44 浏览: 88
确定随机森林回归的最佳参数取值可以使用网格搜索法,具体步骤如下:
1. 导入必要的模块和数据集
```python
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_boston
# 加载数据集
boston = load_boston()
X = boston.data
y = boston.target
```
2. 设置参数范围
```python
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [5, 10, 20, None],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
'max_features': ['auto', 'sqrt', 'log2']
}
```
在上述代码中,n_estimators表示树的数量,max_depth表示树的最大深度,min_samples_split表示一个节点需要分裂的最小样本数,min_samples_leaf表示叶子节点最小样本数,max_features表示每个节点在分裂时考虑的特征数。
3. 进行网格搜索
```python
rf = RandomForestRegressor(random_state=2021)
grid_search = GridSearchCV(rf, param_grid=param_grid, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X, y)
```
在上述代码中,cv表示交叉验证的折数,scoring表示评价指标。
4. 输出最佳参数
```python
print("Best parameters: ", grid_search.best_params_)
```
最终输出的结果为最佳参数组合,即可用于随机森林回归模型的训练。
阅读全文
相关推荐


















