• 这次彻底学明白了:四种机器学习超参数搜索方法太详细了


    在建模时模型的超参数对精度有一定的影响,而设置和调整超参数的取值,往往称为调参

    在实践中调参往往依赖人工来进行设置调整范围,然后使用机器在超参数范围内进行搜素。本文将演示在sklearn中支持的四种基础超参数搜索方法:

    • GridSearch

    • RandomizedSearch

    • HalvingGridSearch

    • HalvingRandomSearch

    技术提升

    本文来自粉丝群小伙伴授权分享,想加入技术交流,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友

    方式①、添加微信号:dkl88191,备注:来自CSDN+机器学习
    方式②、微信搜索公众号:Python学习与数据挖掘,后台回复:加群

    原始模型

    作为精度对比,我们最开始使用随机森林来训练初始化模型,并在测试集计算精度:

    # 数据读取
    df = pd.read_csv('https://mirror.coggle.club/dataset/heart.csv')
    X = df.drop(columns=['output'])
    y = df['output']
    
    # 数据划分
    x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=y)
    
    # 模型训练与计算准确率
    clf = RandomForestClassifier(random_state=0)
    clf.fit(x_train, y_train)
    clf.score(x_test, y_test)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    模型最终在测试集精度为:0.802

    GridSearch

    GridSearch是比较基础的超参数搜索方法,中文名字网格搜索。其原理是在计算的过程中遍历所有的超参数组合,然后搜索到最优的结果。

    如下代码所示,我们对4个超参数进行搜索,搜索空间为 5 * 3 * 2 * 3 = 90组超参数。对于每组超参数还需要计算5折交叉验证,则需要训练450次。

    parameters = {
        'max_depth': [2,4,5,6,7],
        'min_samples_leaf': [1,2,3],
        'min_weight_fraction_leaf': [0, 0.1],
        'min_impurity_decrease': [0, 0.1, 0.2]
    }
    
    # Fitting 5 folds for each of 90 candidates, totalling 450 fits
    clf = GridSearchCV(
        RandomForestClassifier(random_state=0),
        parameters, refit=True, verbose=1,
    )
    clf.fit(x_train, y_train)
    clf.best_estimator_.score(x_test, y_test)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    模型最终在测试集精度为:0.815

    RandomizedSearch

    RandomizedSearch是在一定范围内进行搜索,且需要设置搜索的次数,其默认不会对所有的组合进行搜索。

    n_iter代表超参数组合的个数,默认会设置比所有组合次数少的取值,如下面设置的为10,则只进行50次训练。

    parameters = {
        'max_depth': [2,4,5,6,7],
        'min_samples_leaf': [1,2,3],
        'min_weight_fraction_leaf': [0, 0.1],
        'min_impurity_decrease': [0, 0.1, 0.2]
    }
    
    clf = RandomizedSearchCV(
        RandomForestClassifier(random_state=0),
        parameters, refit=True, verbose=1, n_iter=10,
    )
    
    clf.fit(x_train, y_train)
    clf.best_estimator_.score(x_test, y_test)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    模型最终在测试集精度为:0.815

    HalvingGridSearch

    HalvingGridSearchGridSearch非常相似,但在迭代的过程中是有参数组合减半的操作。

    最开始使用所有的超参数组合,但使用最少的数据,筛选其中最优的超参数,增加数据再进行筛选。

    HalvingGridSearch的思路和hyperband的思路非常相似,但是最朴素的实现。先使用少量数据筛选超参数组合,然后使用更多的数据验证精度。

    n_iterations: 3
    n_required_iterations: 5
    n_possible_iterations: 3
    min_resources_: 20
    max_resources_: 227
    aggressive_elimination: False
    factor: 3
    ----------
    
    iter: 0
    n_candidates: 90
    n_resources: 20
    Fitting 5 folds for each of 90 candidates, totalling 450 fits
    ----------
    
    iter: 1
    n_candidates: 30
    n_resources: 60
    Fitting 5 folds for each of 30 candidates, totalling 150 fits
    ----------
    
    iter: 2
    n_candidates: 10
    n_resources: 180
    Fitting 5 folds for each of 10 candidates, totalling 50 fits
    ----------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    模型最终在测试集精度为:0.855

    HalvingRandomSearch

    HalvingRandomSearchHalvingGridSearch类似,都是逐步增加样本,减少超参数组合。但每次生成超参数组合,都是随机筛选的。

    n_iterations: 3
    n_required_iterations: 3
    n_possible_iterations: 3
    min_resources_: 20
    max_resources_: 227
    aggressive_elimination: False
    factor: 3
    ----------
    
    iter: 0
    n_candidates: 11
    n_resources: 20
    Fitting 5 folds for each of 11 candidates, totalling 55 fits
    ----------
    
    iter: 1
    n_candidates: 4
    n_resources: 60
    Fitting 5 folds for each of 4 candidates, totalling 20 fits
    ----------
    
    iter: 2
    n_candidates: 2
    n_resources: 180
    Fitting 5 folds for each of 2 candidates, totalling 10 fits
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    模型最终在测试集精度为:0.828

    总结与对比

    HalvingGridSearchHalvingRandomSearch比较适合在数据量比较大的情况使用,可以提高训练速度。如果计算资源充足,GridSearchHalvingGridSearch会得到更好的结果。

    后续我们将分享其他的一些高阶调参库的实现,其中也会有数据量改变的思路。如在Optuna中,核心是参数组合的生成和剪枝、训练的样本增加等细节。

  • 相关阅读:
    大老耗时90天完成,带你从实践出发,深入学习Mycat中间件,拥抱Mycat
    PAM从入门到精通(六)
    JMeter-- token写入/读取csv文件+清除掉token的csv文件
    Jmter压测使用实践
    静态 NAT 配置
    【LaTex】如何输入英文字母的花体字?花体字最全总结
    kafka的 ack 应答机制
    计算机毕业设计ssm高校选课系统uu27m系统+程序+源码+lw+远程部署
    Java项目-苍穹外卖-Day12-Apache POI及Excel数据报表
    在业务开发中遇到的树形结构(部门、区域、职位),递归处理。
  • 原文地址:https://blog.csdn.net/qq_34160248/article/details/127717971