随机森林参数优化代码
时间: 2024-06-27 11:01:09 浏览: 127
随机森林代码
随机森林是一种集成学习方法,其参数优化对于获得最佳性能至关重要。以下是一个基本的随机森林模型在Python(sklearn库)中使用GridSearchCV进行参数优化的示例代码:
```python
# 导入必要的库
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
# 加载数据
iris = load_iris()
X, y = iris.data, iris.target
# 定义随机森林模型
rf = RandomForestClassifier()
# 预设要优化的参数网格
param_grid = {
'n_estimators': [50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# 使用GridSearchCV进行参数搜索
grid_search = GridSearchCV(rf, param_grid, cv=5, scoring='accuracy', return_train_score=True)
grid_search.fit(X, y)
# 获取最佳参数组合
best_params = grid_search.best_params_
best_model = grid_search.best_estimator_
# 训练模型并评估
best_model.fit(X, y)
y_pred = best_model.predict(X)
accuracy = accuracy_score(y, y_pred)
print(f"Best parameters: {best_params}")
print(f"Accuracy: {accuracy}")
阅读全文