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)
相关推荐
小墙程序员11 分钟前
机器学习入门(七)PCA
机器学习
灏瀚星空12 分钟前
从基础到实战的量化交易全流程学习:1.3 数学与统计学基础——概率与统计基础 | 基础概念
笔记·python·学习·金融·概率论
伊织code13 分钟前
SKLearn - Biclustering
机器学习·支持向量机·聚类·sklearn·biclustering
Hellohistory17 分钟前
HOTP 算法与实现解析
后端·python
伊织code18 分钟前
cached-property - 类属性缓存装饰器
python·缓存·cache·装饰器·ttl·property·cached-property
明明跟你说过35 分钟前
深度学习常见框架:TensorFlow 与 PyTorch 简介与对比
人工智能·pytorch·python·深度学习·自然语言处理·tensorflow
搏博37 分钟前
专家系统的基本概念解析——基于《人工智能原理与方法》的深度拓展
人工智能·python·深度学习·算法·机器学习·概率论
yzx99101337 分钟前
决策树随机深林
人工智能·python·算法·决策树·机器学习
winner888144 分钟前
论文解读:迁移学习(A Survey on Transfer Learning)
人工智能·机器学习·迁移学习
梓羽玩Python1 小时前
月之暗面最新开源模型!Kimi-Audio:革新多模态音频处理,统一音频理解、生成与对话!
人工智能·python·github