四种Python机器学习超参数搜索方法总结

作者:Python数据挖掘 时间:2022-03-19 17:29:22 

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

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

  • GridSearch

  • RandomizedSearch

  • HalvingGridSearch

  • HalvingRandomSearch

原始模型

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

# 数据读取
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)

模型最终在测试集精度为: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)

模型最终在测试集精度为: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)

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

HalvingGridSearch

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

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

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
----------

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

HalvingRandomSearch

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

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

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

总结与对比

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

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

来源:https://blog.csdn.net/qq_34160248/article/details/127717971

标签:Python,超参数,搜索
0
投稿

猜你喜欢

  • 天极网页版式设计的思考

    2008-01-18 12:44:00
  • php floor()函数案例详解

    2023-06-14 16:13:03
  • 客户端限制只能上传jpg格式图片的js代码

    2023-07-16 00:56:04
  • Python之修改图片像素值的方法

    2022-03-14 03:46:18
  • 基于python实现matlab filter函数过程详解

    2023-06-04 04:54:55
  • 复制链接到剪贴板,兼容Firefox Chrome IE

    2008-12-16 13:23:00
  • Python使用signal定时结束AsyncIOScheduler任务的问题

    2022-12-19 21:28:11
  • sqlserver通用的删除服务器上的所有相同后缀的临时表

    2012-06-06 20:07:34
  • python爬虫 urllib模块反爬虫机制UA详解

    2022-04-07 02:22:51
  • pycharm 实现光标快速移动到括号外或行尾的操作

    2023-07-17 19:52:31
  • 浏览器中的内存泄露(续)解决方案

    2008-05-03 17:14:00
  • JSP实现客户信息管理系统

    2023-06-30 05:32:36
  • 带你深入了解Access数据库的4种安全方式

    2008-11-28 14:34:00
  • 从客户端提升SQL Server数据库性能

    2009-03-06 14:27:00
  • Mootools 1.2教程(21)——类(二)

    2008-12-28 20:58:00
  • 网页HTTP header头信息详解

    2010-03-31 14:42:00
  • 下一站:HandlerSocket!

    2011-04-11 09:02:00
  • 用asp编写文档搜索页面

    2008-01-13 07:04:00
  • 完美解决Python matplotlib绘图时汉字显示不正常的问题

    2023-09-28 05:30:55
  • 快速解决Django关闭Debug模式无法加载media图片与static静态文件

    2023-05-28 02:54:43
  • asp之家 网络编程 m.aspxhome.com