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

news/2024/9/24 23:37:15/

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

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

历史文章回顾:
灰狼优化算法:【智能优化算法】灰狼优化算法【附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: 63454https://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


http://www.ppmy.cn/news/1452865.html

相关文章

STM32的外设总了解

1.NVIC—嵌套向量中断控制器 2.SysTick—系统滴答定时器: 它们是内核里的外设 3.RCC—复位和时钟控制 这个外设十分重要,因为其他的外设再上电的情况下默认是没有时钟的,那么不给时钟的情况下,操作其他外设是无效的,外设不会工作,因此我们需要…

Xcode 对应的 macOS、SDK 版本

最低要求和支持的 SDK 本表截取于 2024-05-04,更多更新可见:https://developer.apple.com/cn/support/xcode/ Xcode 版本要求的最低 OS 版本SDK架构部署目标模拟器SwiftXcode 15.3macOS Sonoma 14iOS 17.4 macOS 14.4 tvOS 17.4 watchOS 10.4 DriverKi…

全方位解析Node.js:从模块系统、文件操作、事件循环、异步编程、性能优化、网络编程等高级开发到后端服务架构最佳实践以及Serverless服务部署指南

Node.js是一种基于Chrome V8引擎的JavaScript运行环境,专为构建高性能、可扩展的网络应用而设计。其重要性在于革新了后端开发,通过非阻塞I/O和事件驱动模型,实现了轻量级、高并发处理能力。Node.js的模块化体系和活跃的npm生态极大加速了开发…

奈氏准则和香农定理

一、奈奎斯特和香农 哈里奈奎斯特(Harry Nyquist)(左) 克劳德艾尔伍德香农(Claude Elwood Shannon)(右) 我们应该在心里记住他们,记住所有为人类伟大事业做出贡献的人,因为他们我们的生活变得越来越精彩&…

【C++】---模板进阶

【C】---模板进阶 一、模版参数1、类型参数2、非类型参数 二、模板的特化1、函数模板的特化2、类模板特化(1)全特化(2)偏特化 三、模板分离编译1、模板支持分离编译吗?2、为什么模板不支持分离编译?3、如何…

VBA 批量处理Excel文件

目录 一. 批量创建Excel文件1.1 VBA的方式1.2 Powershell方式 二. 批量删除文件 一. 批量创建Excel文件 1.1 VBA的方式 Sub CreateFiles()Dim strPath As String, strFileName As StringDim i As Long, rDim pathSeparator As StringOn Error Resume Next 用户选择文件夹路径…

大模型引领NLP研究新范式:从统计机器学习到预训练语言模型

自然语言处理(NLP)研究范式经历了从浅层到深层、从局部到整体、从特定到通用的演进过程。下面我们来详细回顾这一过程。 一、早期的统计机器学习方法(20世纪90年代 - 21世纪初) 词袋模型(Bag-of-Words) 将…

微信小程序 uniapp家庭食谱菜谱食材网上商城系统小程序ko137

随着生活节奏的不断加快,越来越多的人因为工作忙而没有时间自己出去订购喜欢的菜品。随着Internet的飞速发展,网络已经成为我们日常生活中必不可少的部分,越来越多的人也接受了电子商务这种快捷、方便的交易方式。网上订餐其独有的便捷性和直…