scikit-learn超参数调优 (自动寻找模型最佳参数) 方法


  1. 网格搜索(Grid Search)

    • 原理:网格搜索通过预定义的参数组合进行穷举搜索,评估每一种参数组合的性能,选择性能最佳的参数组合。

    • 实现 :使用GridSearchCV类。

    • 示例代码

      python 复制代码
      from sklearn.model_selection import GridSearchCV
      from sklearn.svm import SVC
      
      param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
      grid_search = GridSearchCV(SVC(), param_grid, cv=5)
      grid_search.fit(X_train, y_train)
      print(grid_search.best_params_)
  2. 随机搜索(Randomized Search)

    • 原理:随机搜索在预定义的参数空间中随机选择参数组合进行评估,通常比网格搜索更快,特别是在参数空间较大时。

    • 实现 :使用RandomizedSearchCV类。

    • 示例代码

      python 复制代码
      from sklearn.model_selection import RandomizedSearchCV
      from sklearn.svm import SVC
      from scipy.stats import uniform
      
      param_dist = {'C': uniform(0.1, 10), 'kernel': ['linear', 'rbf']}
      random_search = RandomizedSearchCV(SVC(), param_dist, n_iter=10, cv=5)
      random_search.fit(X_train, y_train)
      print(random_search.best_params_)
  3. 贝叶斯优化(Bayesian Optimization)

    • 原理:贝叶斯优化通过构建一个代理模型(如高斯过程)来预测不同参数组合的性能,并选择最有希望的参数组合进行评估。

    • 实现 :可以使用skopt库中的BayesSearchCV类。

    • 示例代码

      python 复制代码
      from skopt import BayesSearchCV
      from sklearn.svm import SVC
      
      param_space = {'C': (0.1, 10), 'kernel': ['linear', 'rbf']}
      bayes_search = BayesSearchCV(SVC(), param_space, n_iter=10, cv=5)
      bayes_search.fit(X_train, y_train)
      print(bayes_search.best_params_)
  4. 遗传算法(Genetic Algorithms)

    • 原理:遗传算法模拟自然选择和遗传过程,通过交叉、变异等操作在参数空间中搜索最优解。

    • 实现 :可以使用deap库或其他遗传算法库。

    • 示例代码

      python 复制代码
      from deap import base, creator, tools, algorithms
      from sklearn.svm import SVC
      from sklearn.model_selection import cross_val_score
      
      def eval_params(params):
          model = SVC(**params)
          score = cross_val_score(model, X_train, y_train, cv=5).mean()
          return score,
      
      creator.create("FitnessMax", base.Fitness, weights=(1.0,))
      creator.create("Individual", list, fitness=creator.FitnessMax)
      
      toolbox = base.Toolbox()
      toolbox.register("attr_C", random.uniform, 0.1, 10)
      toolbox.register("attr_kernel", random.choice, ['linear', 'rbf'])
      toolbox.register("individual", tools.initCycle, creator.Individual,
                       (toolbox.attr_C, toolbox.attr_kernel), n=1)
      toolbox.register("population", tools.initRepeat, list, toolbox.individual)
      toolbox.register("evaluate", eval_params)
      toolbox.register("mate", tools.cxTwoPoint)
      toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
      toolbox.register("select", tools.selTournament, tournsize=3)
      
      population = toolbox.population(n=10)
      algorithms.eaSimple(population, toolbox, cxpb=0.5, mutpb=0.2, ngen=10)
      best_individual = tools.selBest(population, 1)[0]
      print(best_individual)
相关推荐
yvestine几秒前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
zzc9219 分钟前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
编程有点难24 分钟前
Python训练打卡Day43
开发语言·python·深度学习
2301_8050545630 分钟前
Python训练营打卡Day48(2025.6.8)
pytorch·python·深度学习
LjQ204038 分钟前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
郄堃Deep Traffic1 小时前
机器学习+城市规划第十三期:XGBoost的地理加权改进,利用树模型实现更精准的地理加权回归
人工智能·机器学习·回归·城市规划
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》第九章 迭代器和组合模式
python·学习·设计模式
sponge'1 小时前
opencv学习笔记2:卷积、均值滤波、中值滤波
笔记·python·opencv·学习
databook2 小时前
概率图模型:机器学习的结构化概率之道
python·机器学习·scikit-learn
拾回程序猿的圈圈∞2 小时前
实战二:开发网页端界面完成黑白视频转为彩色视频
python·ai编程