【模型参数优化】网格搜索对随机森林分类模型进行参数寻优【附python实现代码】

写在前面:
首先感谢兄弟们的订阅,让我有创作的动力,在创作过程我会尽最大能力,保证作品的质量,如果有问题,可以私信我,让我们携手共进,共创辉煌。

路虽远,行则将至;事虽难,做则必成。只要有愚公移山的志气、滴水穿石的毅力,脚踏实地,埋头苦干,积跬步以至千里,就一定能够把宏伟目标变为美好现实。

历史文章回顾:
灰狼优化算法:【智能优化算法】灰狼优化算法【附python实现代码】
白鲸优化算法:【智能优化算法】白鲸优化算法【附python实现代码】
【智能优化算法】粒子群优化KNN分类算法【附python实现代码】
【智能优化算法】粒子群优化随机森林分类算法【附python实现代码】
【智能优化算法】粒子群优化LightGBM分类算法【附python实现代码】

在这里插入图片描述

1、介绍

网格搜索是一种在多个维度上搜索最优解的方法,主要用于解决多变量问题,特别是寻找极值(包括极小值和极大值)。以下是网格搜索在不同领域的应用和定义:

  • 化学领域:网格搜索被定义为在寻找多变量问题的所有极值中,以固定增量改变每个变量的搜索极值的方法。
    信息技术领域:网格搜索通过建立跨越Web的信息分布和集成应用程序逻辑,利用现有的网络基础设施、协议规范、Web和数据库技术,为用户提供一体化的智能信息平台。这个平台的目标是创建一种基于Internet的新一代信息平台和软件基础设施,实现全面的信息资源共享。
  • 机器学习和模式识别领域:网格搜索算法是一种数学方法,用于确定最优参数组合以实现最佳性能。这种方法的核心思想是通过枚举某个空间中的所有可能解,并以某种评价准则度量各种解,从而寻求最佳解。通常,可以使用函数的参数空间组成一个网格,并在每个网格点处测试样本,然后根据测试得出的性能结果进行比较,最终确定最有效的参数组合。
  • 模型超参数优化:网格搜索也是一项模型超参数优化技术,常用于优化三个或更少数量的超参数。对于每个超参数,使用者选择一个较小的有限集去探索,然后这些超参数的笛卡尔乘积得到若干组超参数。网格搜索使用每组超参数训练模型,并挑选验证集误差最小的超参数作为最好的超参数。

总的来说,网格搜索是一种强大的工具,可以在多个维度上搜索最优解,适用于各种领域和问题。
【From 大模型】

2、实战代码

使用网格搜索对随机森林分类模型进行参数寻优:

# -*- coding: utf-8 -*-
"""
Created on Fri May  3 21:55:32 2024

@author: 63454

https://zhuanlan.zhihu.com/p/647588686
"""


from sklearn.model_selection import GridSearchCV  
from sklearn.ensemble import RandomForestClassifier  
from sklearn.datasets import load_wine  
from sklearn.model_selection import train_test_split  
from sklearn.metrics import accuracy_score
import time

  
# 加载数据集  
wine = load_wine()  
X = wine.data  
y = wine.target  
  
# 划分训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)  

print("---------------------使用默认参数----------------------------")
# 初始化随机森林分类器  
model = RandomForestClassifier(random_state=99)
# 训练
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("默认参数 accuracy:", acc)
  
print("---------------------参数寻优----------------------------")
t1 = time.time()
# 定义参数网格  
param_grid = {  
    'n_estimators': [500, 600, 700, 800], #   range(500, 1000)
    'max_depth': [None, 5, 10, 15, 20],  
    'min_samples_split': [2, 5, 10],  
    'min_samples_leaf': [1, 2, 3, 4, 5, 6, 7, 8],  
    'max_features': ['auto', 'sqrt', 'log2'],  
    'bootstrap': [True, False],  
}  
  
# 初始化随机森林分类器  
model = RandomForestClassifier(random_state=99)  
# 初始化网格搜索对象  
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='accuracy', verbose=2)  
# 执行网格搜索  
grid_search.fit(X_train, y_train)  
t2 = time.time()
# 输出最优参数  
print("Best parameters:")  
print()  
print(grid_search.best_params_)
print("time:", t2-t1)

print("---------------------最优模型----------------------------")
model_best_params = grid_search.best_params_
model = grid_search.best_estimator_
# 训练
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("最优参数 accuracy:", acc)

终端输出:

[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=600; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=800; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=600; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=700; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=2, min_samples_split=10, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=2, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=700; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=700; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=800; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=3, min_samples_split=10, n_estimators=800; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=500; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=500; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=700; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=2, n_estimators=800; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=500; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=500; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=500; total time=   0.5s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=600; total time=   0.6s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=600; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=700; total time=   0.8s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=800; total time=   0.9s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=800; total time=   0.7s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=5, n_estimators=800; total time=   0.2s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=500; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=600; total time=   0.1s
[CV] END bootstrap=False, max_depth=15, max_features=auto, min_samples_leaf=4, min_samples_split=10, n_estimators=600; total time=   0.1s

3、总结

网格搜索(Grid Search)作为一种参数寻优技术,具有其独特的优点和缺点。

优点:

全面搜索:网格搜索是一种全面的搜索策略,能够穷尽所有参数的所有可能组合。这种策略可以确保在设定的参数范围内,找到最优的参数组合,从而达到最佳的效果,避免陷入局部最优解。
直观易懂:网格搜索的方法简单直接,易于理解和实现。它通过遍历所有可能的参数组合来找到最优的超参数集,对于初学者来说,是一种非常直观的超参数调优方法。
适用于参数量较少的情况:在所需设置的参数数目即参数维数较少的情况下,网格搜索算法的运算复杂度往往比较低,同时可以节省时间成本。此外,网格搜索算法可以并行计算,每组参数组合之间是相互独立没有相关联系的,因此可以在一定范围的区间内,从初始位置同时向多个方向出发搜索。

缺点:

计算成本高:网格搜索的主要缺点是计算成本非常高,尤其是当超参数空间很大或者模型训练时间很长时。因为需要尝试大量的参数组合,这会导致搜索时间过长,甚至在某些情况下变得不实际。
可能错过最优参数:网格搜索只能在有限的、预设的参数组合中进行搜索,因此可能会错过最优参数。如果预设的参数空间区域小,或者参数的取值范围设置不当,就有可能导致搜索不到最佳的参数值。
不适用于大规模数据集:对于中等或大规模数据量的搜索问题,网格搜索需要遍历所有参数的所有可能性,这会耗费过多的时间成本,搜索代价高昂。在大多数的设备中,对于几万个待寻优参数,每个参数有数千个候选值的情况,预计需要几天的时间来搜索最佳的参数组合。
因此,在选择是否使用网格搜索进行参数寻优时,需要根据实际问题的特点和需求进行权衡和选择。对于参数空间较小、计算资源充足的情况,网格搜索是一个不错的选择。然而,对于参数空间较大或计算资源有限的情况,可能需要考虑其他更为高效的参数寻优方法,如随机搜索(Random Search)等。

参考:
https://blog.csdn.net/qq_41076797/article/details/102755904
https://zhuanlan.zhihu.com/p/647588686

  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,下面是使用网络搜索来确定随机森林模型最优参数Python代码。在这个例子中,我们使用网格搜索算法来搜索参数空间。 ```python from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV # 定义随机森林模型 rfc = RandomForestClassifier() # 定义超参数搜索空间 param_grid = { "n_estimators": [10, 50, 100], # 决策树数量 "max_depth": [5, 10, 20], # 每个决策树的最大深度 "min_samples_split": [2, 5, 10], # 分裂内部节点所需的最小样本数 "min_samples_leaf": [1, 2, 5], # 叶子节点所需的最小样本数 "max_features": ["sqrt", "log2", None] # 每个决策树的最大特征数 } # 定义网格搜索算法 grid_search = GridSearchCV( estimator=rfc, param_grid=param_grid, cv=5, # 交叉验证次数 scoring="accuracy" # 评估指标 ) # 运行网格搜索 grid_search.fit(X_train, y_train) # 输出最佳超参数组合和性能 print("Best parameters: ", grid_search.best_params_) print("Best score: ", grid_search.best_score_) ``` 在这个例子中,我们使用了`RandomForestClassifier`实现随机森林分类器,并使用`GridSearchCV`实现网格搜索算法来搜索参数空间。定义的超参数搜索空间包括决策树数量、每个决策树的最大深度、节点最小样本数、叶子节点最小样本数和每个决策树的最大特征数等。我们设定了5次交叉验证,并使用准确率作为评估指标来评估模型性能。 运行后,您将获得最佳超参数组合和性能指标。根据这些结果,您可以使用最佳超参数组合来训练随机森林模型,并评估其性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器不学习我学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值